In the last lesson, you learned how to create variables and store values in them. But Python needs to know what kind of value you’re storing, a number, a word, a true/false answer, before it can work with it correctly. That’s what a data type is: a label that tells Python how to treat a piece of data.
Get this wrong, and your program breaks in confusing ways. Try to add the number 5 to the text “5” and Python will stop you cold. So before going further, it’s worth spending a few minutes actually understanding the main types you’ll use every day.
The Main Data Types in Python
Python has several built-in data types, but as a beginner, you really only need to know these four well:
1. Integer (int)
Whole numbers, positive or negative, with no decimal point.
age = 15
year = 2026
temperature = -52. Float (float)
Numbers that have a decimal point.
price = 99.50
pi_value = 3.14159
height = 5.63. String (str)
Text. Anything wrapped in single or double quotes.
name = "Sujan"
city = 'Kathmandu'
message = "I am learning Python"4. Boolean (bool)
Only two possible values: True or False. Used constantly in decision-making, which you’ll see when we get to conditional statements.
is_student = True
has_passed = FalseChecking a Data Type
Python gives you a built-in function,type(), to check what type a variable is. This is genuinely useful when you’re debugging and not sure why something isn’t working.
x = 10
y = "Hello"
z = 3.14
w = True
print(type(x)) # <class 'int'>
print(type(y)) # <class 'str'>
print(type(z)) # <class 'float'>
print(type(w)) # <class 'bool'>A Common Mistake Beginners Make
This one trips up almost everyone starting:
num1 = "10"
num2 = 5
result = num1 + num2Run this, and Python throws a TypeError. Why? Because num1 is a string, not a number, even though it looks like a number. Python won’t guess what you meant. You’d need to convert num1 into an integer first using,int(num1), something we’ll cover properly in the next lesson on type conversion.
Quick Example: Putting It Together
name = "Priya"
age = 16
gpa = 3.8
is_school_topper = True
print(name, "is", age, "years old.")
print("GPA:", gpa)
print("School topper:", is_school_topper)Nothing fancy here, but notice how each variable holds a different type, and Python handles printing all of them together without complaint, as long as you’re not trying to combine a string and a number with +.
Frequently Asked Questions
Data Types in Python
Practice Questions
- What will be the output of
type(25.0)? - Identify the data type of each:
"Nepal",100,False,4.5 - Will this code run without error? Why or why not?
a = "20" b = 10 print(a + b) - What is the output of
print(10 == 10.0)? - Write a program that stores your name, age, and whether you are a student (True/False) in three variables, then prints all three using a single
print()statement. - What does
print(type(True))return?
