View Categories

Data Types in Python

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.

int_datatype.py
age = 15
year = 2026
temperature = -5
2. Float (float)

Numbers that have a decimal point.

float_datatype.py
Copy to clipboard
price = 99.50
pi_value = 3.14159
height = 5.6
3. String (str)

Text. Anything wrapped in single or double quotes.

str_datatype.py
Copy to clipboard
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.

bool_datatype.py
Copy to clipboard
is_student = True
has_passed = False

Checking 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.

checking_datatype.py
Copy to clipboard
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:

Copy to clipboard
num1 = "10"
num2 = 5
result = num1 + num2

Run 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

Copy to clipboard
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

  • Why does “5” + 5 throw an error but “5” + “5” doesn’t?

    Because + means something different depending on the type. Between two strings, it joins them together ("55"). Between a string and an integer, Python doesn’t know what you want, so it refuses rather than guessing wrong. 

  • What’s the difference between int and float?

    An int has no decimal point (10), a float does (10.0). Even 10.0 is treated as a float, not an int, despite looking like a whole number. 

  • Can a variable change its data type later?

    Yes, and this actually happens a lot in real code. If x=5 and later you write x=”five”, Python doesn’t complain. x is now a string. This flexibility is convenient, but it can also cause bugs if you lose track of what a variable currently holds. 

  • Do I need to tell Python what data type a variable is?

    No. Python figures it out automatically based on the value you assign. This is called dynamic typing. Languages like C or Java make you declare the type upfront; Python doesn’t. 

Practice Questions

  1. What will be the output of type(25.0)?
  2. Identify the data type of each: "Nepal", 100, False, 4.5
  3. Will this code run without error? Why or why not? a = "20" b = 10 print(a + b)
  4. What is the output of print(10 == 10.0)?
  5. 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.
  6. What does print(type(True)) return?

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to Top