When you start learning Python, you will work with different kinds of values. Sometimes you will store a person's name, sometimes an age, and sometimes a percentage or a logical value such as True or False.
But how does Python distinguish between these values?
The answer is data types.
Data types tell us what kind of value we are working with. Understanding them is important because different types of data support different operations.
For example, adding two numbers is different from joining two strings.
In this article, we will explore the six fundamental types listed in our syllabus: int, float, complex, str, bool, and None. We will also learn how to identify data types using Python's built-in type() function.
1. What Is a Data Type in Python?
A data type describes the kind of value represented by an object in a Python program.
Consider the following example:
age = 20
price = 99.50
name = "Rahul"
is_student = True
Here, the variables contain different types of values:
agecontains an integer.pricecontains a floating-point number.namecontains a string.is_studentcontains a Boolean value.
Python automatically determines the type of each value when you assign it to a variable.
This means you generally do not need to declare a variable's type before using it.
2. Why Are Data Types Important?
Data types help Python determine which operations are appropriate for a value.
For example:
a = 10
b = 20
print(a + b)
Output:
30
Both variables contain integers, so Python adds their numeric values.
Now consider this example:
first_name = "Rahul"
last_name = "Sharma"
print(first_name + last_name)
Output:
RahulSharma
Here, the + operator joins the two strings instead of performing numerical addition.
If you want a space between the names, you can write:
first_name = "Rahul"
last_name = "Sharma"
print(first_name + " " + last_name)
Output:
Rahul Sharma
These examples demonstrate why understanding data types is essential.
3. Integer Data Type (int)
An integer is a whole number without a fractional or decimal part.
Python uses the int data type for integers.
Examples include:
age = 20
marks = 85
temperature = -5
number = 0
All these variables contain integer values.
Example: Student Age
age = 19
print(age)
print(type(age))
Output:
19
<class 'int'>
The type() function tells us that the value belongs to the int type.
Example: Adding Two Integers
a = 15
b = 25
total = a + b
print("Total =", total)
Output:
Total = 40
Important Points About Integers
Integers can be positive, negative, or zero.
They do not contain a decimal point.
Python integers can represent very large whole numbers, subject to available memory.
4. Floating-Point Data Type (float)
A floating-point number represents a number that can have a fractional part.
Python uses the float data type for these values.
Examples:
price = 99.50
percentage = 87.5
temperature = -2.5
pi = 3.14159
Example: Student Percentage
percentage = 85.75
print(percentage)
print(type(percentage))
Output:
85.75
<class 'float'>
Example: Calculating an Average
Suppose a student scores 80 and 90 in two subjects.
marks1 = 80
marks2 = 90
average = (marks1 + marks2) / 2
print("Average =", average)
Output:
Average = 85.0
Notice that the result is 85.0, not 85.
In Python, the / operator performs true division and returns a floating-point result for ordinary finite numeric inputs, even when the division is exact.
Integer vs Float
Consider:
a = 10
b = 10.0
print(type(a))
print(type(b))
Output:
<class 'int'>
<class 'float'>
Although both values represent the number ten, their data types are different.
Remember: 10 is an integer literal, while 10.0 is a floating-point literal.
5. Complex Data Type (complex)
Python supports complex numbers through the complex data type.
A complex number has a real part and an imaginary part.
For example:
z = 3 + 4j
Here:
3is the real part.4is the imaginary part.jrepresents the imaginary unit in Python.
Complex numbers are used in mathematics, engineering, signal processing, and scientific computing.
Example: Creating a Complex Number
number = 3 + 4j
print(number)
print(type(number))
Output:
(3+4j)
<class 'complex'>
Example: Accessing Real and Imaginary Parts
number = 3 + 4j
print("Real part:", number.real)
print("Imaginary part:", number.imag)
Output:
Real part: 3.0
Imaginary part: 4.0
Python provides the .real and .imag attributes to access these parts.
Another Example
a = 2 + 3j
b = 4 + 5j
print(a + b)
Output:
(6+8j)
Python supports arithmetic operations on complex numbers.
6. String Data Type (str)
A string is a sequence of characters used to represent text.
Python uses the str data type for strings.
Examples:
name = "Rahul"
city = "Kolkata"
message = "Welcome to Python"
Strings can contain letters, numbers, spaces, and special characters.
For example:
student_id = "12345"
Although the value contains digits, it is a string because it is enclosed in quotation marks.
Creating Strings
Python allows you to create strings using single or double quotation marks.
name = 'Rahul'
city = "Kolkata"
Both are valid.
You can also use triple quotation marks for strings that span multiple lines.
message = """Welcome to Python.
This is a beginner-friendly course."""
print(message)
Output:
Welcome to Python.
This is a beginner-friendly course.
Example: Joining Strings
first_name = "Rahul"
last_name = "Sharma"
full_name = first_name + " " + last_name
print(full_name)
Output:
Rahul Sharma
Example: Finding the Length of a String
Python provides the built-in len() function to find the number of characters in a string.
message = "Python"
print(len(message))
Output:
6
Important Point
Compare these two statements:
a = 100
b = "100"
print(type(a))
print(type(b))
Output:
<class 'int'>
<class 'str'>
The first value is an integer, while the second is a string.
Quotation marks make a significant difference.
7. Boolean Data Type (bool)
The Boolean data type represents one of two logical values:
True
False
Python uses the bool data type for these values.
Boolean values are commonly used in conditions, comparisons, and decision-making statements.
Example: Boolean Variable
is_student = True
print(is_student)
print(type(is_student))
Output:
True
<class 'bool'>
Example: Comparing Two Numbers
a = 10
b = 20
result = a < b
print(result)
Output:
True
The expression a < b is true because ten is less than twenty.
Now try:
a = 10
b = 20
result = a > b
print(result)
Output:
False
The expression is false because ten is not greater than twenty.
Common Comparison Operators
| Operator | Meaning | Example | Result |
|---|---|---|---|
== | Equal to | 10 == 10 | True |
!= | Not equal to | 10 != 20 | True |
> | Greater than | 20 > 10 | True |
< | Less than | 10 < 20 | True |
>= | Greater than or equal to | 10 >= 10 | True |
<= | Less than or equal to | 10 <= 5 | False |
These operators are useful when you begin learning conditional statements.
Important: In Python, True and False must be written with their first letters capitalized.
8. The Special Value None
Python provides a special value called None.
It represents the absence of a value.
For example:
result = None
print(result)
print(type(result))
Output:
None
<class 'NoneType'>
The type of None is called NoneType.
Why Is None Useful?
Sometimes a program has not yet calculated a result or does not currently have a meaningful value to store.
In such situations, None can be useful.
Example:
student_name = None
print(student_name)
Output:
None
Later, the variable can be assigned a meaningful value:
student_name = None
student_name = "Rahul"
print(student_name)
Output:
Rahul
None Is Different from Zero
Consider:
a = None
b = 0
print(a)
print(b)
Output:
None
0
These values are not the same.
Nonerepresents the absence of a value.0is an integer representing the number zero.
An empty string, "", is also different from None.
9. How to Check the Data Type Using type()
Python provides a built-in function called type().
It allows you to determine the type of a value or object.
Example
a = 10
b = 10.5
c = 3 + 4j
d = "Python"
e = True
f = None
print(type(a))
print(type(b))
print(type(c))
print(type(d))
print(type(e))
print(type(f))
Output:
<class 'int'>
<class 'float'>
<class 'complex'>
<class 'str'>
<class 'bool'>
<class 'NoneType'>
This example demonstrates the six types covered in this article.
Why Should You Learn type()?
The type() function is especially helpful when you are learning Python because it allows you to check what kind of value a variable contains.
It can also help you identify mistakes when working with numbers, strings, and user input.
10. Python Automatically Determines Data Types
Python is a dynamically typed language.
This means that you do not generally need to declare the data type before assigning a value to a variable.
For example:
value = 10
print(value)
print(type(value))
Output:
10
<class 'int'>
You can then assign a string to the same variable name:
value = "Python"
print(value)
print(type(value))
Output:
Python
<class 'str'>
The variable name value now refers to a string object rather than the integer object it referred to previously.
This flexibility is one of the features that makes Python convenient for beginners.
However, changing the type of a value does not mean that every operation will work with every type. You must still understand the data you are using.
11. Difference Between the Six Data Types
Let's compare the six types covered in our syllabus.
| Data Type | Purpose | Example | Output of type() |
|---|---|---|---|
int | Whole numbers | 25 | <class 'int'> |
float | Floating-point numbers | 25.5 | <class 'float'> |
complex | Complex numbers | 3 + 4j | <class 'complex'> |
str | Text | "Python" | <class 'str'> |
bool | Logical values | True | <class 'bool'> |
NoneType | Type of None | None | <class 'NoneType'> |
Understanding these differences will make it easier to write calculations, process text, take user input, and create conditions.
12. Common Beginner Mistakes
Mistake 1: Confusing an integer with a string
Incorrect assumption:
age = "20"
A beginner might assume that age contains an integer.
However, "20" is a string because it is enclosed in quotation marks.
Check it:
age = "20"
print(type(age))
Output:
<class 'str'>
If you need an integer, write:
age = 20
Or convert the string when appropriate:
age = int("20")
print(type(age))
Output:
<class 'int'>
Mistake 2: Using lowercase true instead of True
Incorrect:
is_student = true
Python does not recognize lowercase true as its Boolean literal. Unless you have defined a name called true, this will raise a NameError.
Correct:
is_student = True
Mistake 3: Expecting None to behave like zero
Consider:
result = None
print(result + 10)
This produces a TypeError because Python does not support adding None to an integer.
If you intend to perform a calculation, assign an appropriate numeric value first.
Mistake 4: Forgetting that / returns a floating-point result
Example:
a = 10
b = 2
print(a / b)
Output:
5.0
The result is a float.
If you need floor division, Python provides the // operator:
print(10 // 2)
Output:
5
13. Practice Programs
Try these small programs to test your understanding.
Program 1: Identify the Data Types
a = 50
b = 25.75
c = 2 + 3j
d = "Python"
e = False
f = None
print(type(a))
print(type(b))
print(type(c))
print(type(d))
print(type(e))
print(type(f))
Task: Predict the output before running the program.
Program 2: Student Information
student_name = "Rahul"
age = 20
percentage = 85.5
is_passed = True
print("Name:", student_name)
print("Age:", age)
print("Percentage:", percentage)
print("Passed:", is_passed)
Task: Identify the data type of each variable.
Program 3: Add Two Numbers
a = 15
b = 25
total = a + b
print("Total:", total)
print("Data type:", type(total))
Task: Change the values of a and b and observe the output.
Program 4: Compare Two Numbers
a = 50
b = 30
result = a > b
print("Result:", result)
print("Data type:", type(result))
Task: Change the values so that the result becomes False.
Program 5: Compare an Integer and a String
a = 100
b = "100"
print(type(a))
print(type(b))
Task: Explain why the two data types are different even though their contents look similar.
14. Frequently Asked Questions
Q1. What are data types in Python?
Data types describe the kind of value represented by an object, such as a number, text, Boolean value, or None.
Q2. What is the difference between int and float?
int represents integers such as 10 and -5. float represents floating-point values such as 10.5 and 3.14.
Q3. What is the data type of "123"?
It is str because the value is enclosed in quotation marks.
Q4. What is the data type of True?
Its data type is bool.
Q5. What is the data type of None?
Its data type is NoneType.
Q6. How can I check the data type of a variable?
Use the built-in type() function.
Example:
number = 25
print(type(number))
Output:
<class 'int'>
Q7. Does Python require us to declare variable types?
No. Python generally determines the type automatically from the value assigned to a variable.
Conclusion
Data types are one of the most important foundations of Python programming.
In this article, we explored integers, floating-point numbers, complex numbers, strings, Boolean values, and None. We also learned how to check data types using type() and saw how Python determines types automatically.
As you continue learning Python, you will use these concepts in almost every program, from simple calculators to database applications and AI projects.
The best way to remember data types is to practise creating variables with different values and checking their types yourself.
Keep practising, and build your Python knowledge one concept at a time!
Comments
Post a Comment