DEV Community

Cover image for Why Data Types Exist in Python: and Building Logic Through Finding Perfect number
Kathirvel S
Kathirvel S

Posted on

Why Data Types Exist in Python: and Building Logic Through Finding Perfect number

Introduction

When we start learning Python, one of the first concepts we hear about is data types. Many beginners ask:

"Why do data types even exist?"

To understand this, let's begin with a simple story.

A Story Behind Data Types

Computers don't understand letters, numbers, images, or sounds directly. Everything inside a computer is stored as binary digits (0s and 1s).

Consider the letter 'A'.

The ASCII value of A is 65.

A = 65
Enter fullscreen mode Exit fullscreen mode

Inside the computer, 65 is stored in binary form:

100001
Enter fullscreen mode Exit fullscreen mode

Now imagine another situation where:

a = 100001
Enter fullscreen mode Exit fullscreen mode

The binary representation of the number 65 and the number 100001 may look similar to the computer at the storage level.

So how does the computer know whether it should treat the value as:

  • A character ('A')
  • A number (65)
  • A string ("65")

This is where data types become important.

Data types tell the computer:

"How should this piece of data be interpreted and processed?"

Without data types, the computer would struggle to distinguish between characters, numbers, text, and other kinds of information.

For example:

char = 'A'
num = 65
text = "65"
Enter fullscreen mode Exit fullscreen mode

Even though they may be related, Python treats them differently because each has a different data type.


Common Data Types in Python

According to the official Python documentation:

"The following sections describe the standard types that are built into the interpreter." (Python documentation)

Python provides several built-in data types that help the interpreter understand how data should be stored and manipulated.

Official Documentation:

Python Built-in Types Documentation

Integer (int)

Used for whole numbers.

age = 20
Enter fullscreen mode Exit fullscreen mode

Float (float)

Used for decimal numbers.

price = 99.99
Enter fullscreen mode Exit fullscreen mode

String (str)

Used for text.

name = "Python"
Enter fullscreen mode Exit fullscreen mode

Boolean (bool)

Used for True or False values.

is_active = True
Enter fullscreen mode Exit fullscreen mode

Problem 1: Sum of Odd and Even Numbers

Before writing code, let's think like a programmer.

Suppose I ask:

What happens when we add an odd number and an even number?

Let's try manually.

3 + 4 = 7
Enter fullscreen mode Exit fullscreen mode

7 is Odd.

Another example:

5 + 8 = 13
Enter fullscreen mode Exit fullscreen mode

13 is Odd.

Again:

9 + 2 = 11
Enter fullscreen mode Exit fullscreen mode

11 is Odd.

After observing multiple examples, we can identify a pattern:

Odd + Even = Odd
Enter fullscreen mode Exit fullscreen mode

Now let's create the logic ourselves.

Building the Logic

Step 1:

Take two numbers.

3 and 4
Enter fullscreen mode Exit fullscreen mode

Step 2:

Add them.

3 + 4 = 7
Enter fullscreen mode Exit fullscreen mode

Step 3:

Check whether the result is divisible by 2.

If a number leaves remainder 1 after division by 2:

Number is Odd
Enter fullscreen mode Exit fullscreen mode

Otherwise:

Number is Even
Enter fullscreen mode Exit fullscreen mode

Flowchart

Start
  |
Take Two Numbers
  |
Add Numbers
  |
Check sum % 2
  |
  +---- remainder != 0 ----> ODD
  |
  +---- remainder == 0 ----> EVEN
  |
 End
Enter fullscreen mode Exit fullscreen mode

Python Code

no1 = 3
no2 = 4

sum = no1 + no2

if sum % 2 != 0:
    print("ODD")
else:
    print("EVEN")
Enter fullscreen mode Exit fullscreen mode

Code Explanation

no1 = 3
no2 = 4
Enter fullscreen mode Exit fullscreen mode

Stores two numbers.

sum = no1 + no2
Enter fullscreen mode Exit fullscreen mode

Adds both numbers and stores the result.

sum % 2
Enter fullscreen mode Exit fullscreen mode

Checks the remainder after dividing by 2.

If the remainder is not zero:

if sum % 2 != 0:
Enter fullscreen mode Exit fullscreen mode

The number is Odd.

Otherwise:

else:
Enter fullscreen mode Exit fullscreen mode

The number is Even.

Output

ODD
Enter fullscreen mode Exit fullscreen mode

Problem 2: Perfect Number

Let's build the logic slowly before coding.

What is a Perfect Number?

A perfect number is a number whose factors (excluding itself) add up exactly to the number.

Let's take:

6
Enter fullscreen mode Exit fullscreen mode

Find all factors except 6 itself.

1
2
3
Enter fullscreen mode Exit fullscreen mode

Now add them.

1 + 2 + 3 = 6
Enter fullscreen mode Exit fullscreen mode

The sum becomes equal to the original number.

So:

6 is a Perfect Number
Enter fullscreen mode Exit fullscreen mode

Let's try another number.

8
Enter fullscreen mode Exit fullscreen mode

Factors:

1
2
4
Enter fullscreen mode Exit fullscreen mode

Sum:

1 + 2 + 4 = 7
Enter fullscreen mode Exit fullscreen mode

7 is not equal to 8.

So:

8 is NOT a Perfect Number
Enter fullscreen mode Exit fullscreen mode

Building the Logic

Step 1:

Take a number.

6
Enter fullscreen mode Exit fullscreen mode

Step 2:

Check all numbers from 1 up to the number before it.

1 2 3 4 5
Enter fullscreen mode Exit fullscreen mode

Step 3:

Find which numbers divide 6 completely.

1 ✓
2 ✓
3 ✓
4 ✗
5 ✗
Enter fullscreen mode Exit fullscreen mode

Step 4:

Add all valid factors.

1 + 2 + 3 = 6
Enter fullscreen mode Exit fullscreen mode

Step 5:

Compare the total with the original number.

6 == 6
Enter fullscreen mode Exit fullscreen mode

Perfect Number.


Flowchart

Start
  |
Take Number
  |
Find Factors
  |
Add Factors
  |
Compare Sum and Number
  |
  +---- Equal ------> Perfect Number
  |
  +---- Not Equal --> Not Perfect Number
  |
 End
Enter fullscreen mode Exit fullscreen mode

Python Program

no = 6
total = 0
div = 1

while div < no:
    if no % div == 0:
        total = total + div
    div += 1

if no == total:
    print('Perfect Number')
else:
    print('Not Perfect Number')
Enter fullscreen mode Exit fullscreen mode

Code Explanation

no = 6
Enter fullscreen mode Exit fullscreen mode

Stores the number we want to check.

total = 0
Enter fullscreen mode Exit fullscreen mode

Used to store the sum of factors.

div = 1
Enter fullscreen mode Exit fullscreen mode

Starting divisor.

while div < no:
Enter fullscreen mode Exit fullscreen mode

Runs from 1 up to 5.

if no % div == 0:
Enter fullscreen mode Exit fullscreen mode

Checks whether the divisor is a factor.

For example:

6 % 2 = 0
Enter fullscreen mode Exit fullscreen mode

So 2 is a factor.

total = total + div
Enter fullscreen mode Exit fullscreen mode

Adds the factor to the total.

div += 1
Enter fullscreen mode Exit fullscreen mode

Moves to the next divisor.

After the loop completes:

if no == total:
Enter fullscreen mode Exit fullscreen mode

Compares the factor sum with the original number.

If both are equal:

Perfect Number
Enter fullscreen mode Exit fullscreen mode

Otherwise:

Not Perfect Number
Enter fullscreen mode Exit fullscreen mode

Output

Perfect Number
Enter fullscreen mode Exit fullscreen mode

Conclusion

Data types are one of the most important concepts in Python because they help the computer understand what kind of data it is working with. Without data types, the computer would not know whether a value should be treated as a character, number, text, or logical value.

However, learning Python is not only about understanding data types and syntax. It is also about learning how to think logically.

In this article, we started by understanding why data types exist and how computers interpret information internally. We then explored some fundamental Python data types and saw how they help the interpreter process different kinds of data correctly.

After building that foundation, we worked through two logical problems:

  • Determining whether the sum of two numbers is Odd or Even.
  • Checking whether a number is a Perfect Number.

Instead of jumping directly into code, we first analyzed the problem, observed patterns, built the logic step by step, designed simple flowcharts, and then translated that logic into Python code. This is the same approach followed by experienced programmers when solving real-world problems.

A good programmer does not start with code.

A good programmer starts with:

  1. Understanding the problem.
  2. Breaking it into smaller steps.
  3. Building a logical solution.
  4. Creating a flow of execution.
  5. Converting that logic into code.

The more logical problems you solve, the stronger your problem-solving skills become. Python provides the tools, but logic is what transforms those tools into solutions.

Keep practicing, keep questioning, and most importantly, keep building logic before writing code.

Top comments (0)