You finished the last post. You stored your name in a variable. You stored your age. Both worked.
But here's something that probably confused you. When you stored your name, you used quotes. When you stored your age, you didn't. And when you tried to combine them in a print statement, Python complained unless you added that str() thing.
Why? Your name and your age are both just information. Why does Python care about the difference?
Because to Python, they are not the same kind of thing at all. And understanding this, really understanding it, will save you from dozens of confusing errors in the future.
What You'll Actually Get From This Post
By the end, you'll know:
- What data types are and why they exist
- The four main types you'll use constantly: strings, integers, floats, and booleans
- How to check what type something is
- How to convert between types
- Why type errors happen and how to read them
Why Types Exist
Think about a calculator. You can add two numbers. You get a number back.
Now try to add a name and a number. What's "Alex" plus 25? It makes no sense. There's no meaningful answer.
Your calculator doesn't let you do that because it knows numbers and text are different kinds of things. Python thinks the same way.
When Python stores a value, it doesn't just store the value. It also remembers what kind of value it is. A number behaves differently from text. A yes/no value behaves differently from a decimal. Tracking this is what types are for.
There are four types you'll use in almost every program you ever write. Learn these four well before worrying about anything else.
Type 1: Strings (Text)
A string is any text. A name. A sentence. A word. An email address. A tweet. Even a single letter.
name = "Akhil"
city = "Mumbai"
message = "I am learning Python"
single_letter = "A"
number_as_text = "42"
The quotes are what make something a string. Single quotes or double quotes, both work. Pick one style and stick with it.
name1 = "Akhil" # double quotes
name2 = 'Akhil' # single quotes
# both are identical strings
Strings can be combined with +. This is called concatenation. You're just sticking text together.
first = "Good"
second = "morning"
combined = first + " " + second
print(combined)
Output:
Good morning
You can also repeat a string with *.
line = "-" * 20
print(line)
Output:
--------------------
Silly example but you'll actually use this to format output in real programs.
One more thing. Strings have a length. len() tells you how many characters are in a string.
name = "Jack"
print(len(name))
Output:
4
Type 2: Integers (Whole Numbers)
An integer is any whole number. No decimals. No quotes.
age = 25
score = 0
year = 2024
number_of_posts = 130
temperature = -5
Negative numbers work fine. Just put a minus sign.
Integers support all the math you'd expect.
a = 10
b = 3
print(a + b) # addition: 13
print(a - b) # subtraction: 7
print(a * b) # multiplication: 30
print(a / b) # division: 3.3333...
print(a // b) # floor division: 3 (drops the decimal)
print(a % b) # modulo: 1 (the remainder)
print(a ** b) # power: 1000 (10 to the power of 3)
Output:
13
7
30
3.3333333333333335
3
1
1000
The // operator is useful when you need a whole number result from division. The % operator gives you the remainder after division. You'll use both more than you think.
Type 3: Floats (Decimal Numbers)
A float is any number with a decimal point.
price = 99.99
height = 5.11
pi = 3.14159
temperature = 36.6
small = 0.001
Floats work with all the same math operators as integers.
price = 99.99
discount = 0.10
final = price - (price * discount)
print(f"Final price: {final}")
Output:
Final price: 89.991
One thing to know. Computers store decimals in a way that sometimes causes tiny rounding errors.
print(0.1 + 0.2)
Output:
0.30000000000000004
That's not a bug in your code. That's how computers handle decimal math internally. For most purposes it doesn't matter. If you're building something that needs precise decimal handling, like financial software, Python has tools for that. But don't worry about it now.
Type 4: Booleans (True or False)
A boolean has exactly two possible values: True or False. Capital T and capital F. That's important.
is_raining = True
has_finished = False
is_logged_in = True
Booleans seem too simple to be useful. They're actually one of the most important types in programming. Every decision your code makes comes down to a boolean. Every if statement (which you'll learn in the next post) checks whether something is True or False.
You can create booleans directly like above, or they come from comparisons.
age = 25
print(age > 18) # True
print(age == 30) # False
print(age != 30) # True
print(age < 18) # False
Output:
True
False
True
False
Note: == checks if two things are equal. One = assigns a value. Two == compares values. This trips up beginners constantly.
Checking the Type of Something
Python has a built-in function called type() that tells you what type something is.
name = "Akhil"
age = 22
height = 5.11
is_student = True
print(type(name))
print(type(age))
print(type(height))
print(type(is_student))
Output:
<class 'str'>
<class 'int'>
<class 'float'>
<class 'bool'>
str means string. int means integer. float means float. bool means boolean.
You'll use type() a lot when debugging. When something behaves unexpectedly, checking what type it is often reveals the problem immediately.
Converting Between Types
This is where a lot of beginner errors come from. Python won't automatically convert between types when it doesn't know what you want. You have to tell it.
The conversion functions are str(), int(), and float().
age = 25 # integer
age_as_text = str(age) # convert to string: "25"
price_text = "99.99" # string
price = float(price_text) # convert to float: 99.99
score_text = "42" # string
score = int(score_text) # convert to integer: 42
A very common situation: you have a number you want to put inside a sentence.
age = 25
message = "I am " + age + " years old" # this breaks
Error:
TypeError: can only concatenate str (not "int") to str
Python says: you're trying to combine text and a number with +. I don't know what you want. I won't guess.
Fix:
age = 25
message = "I am " + str(age) + " years old" # works
print(message)
Output:
I am 25 years old
Or, the easier way, use an f-string. f-strings handle the conversion automatically.
age = 25
print(f"I am {age} years old") # works, no str() needed
Output:
I am 25 years old
Get comfortable with f-strings early. They prevent this whole category of errors.
The Most Common Type Mistakes
Mistake 1: Forgetting that numbers in quotes are strings
age = "25" # this is a string, not a number
next_year = age + 1 # breaks
Error:
TypeError: can only concatenate str (not "int") to str
The number 25 is in quotes, so Python treats it as text. You can't do math with text.
Fix:
age = 25 # no quotes, it's a number now
next_year = age + 1 # works: 26
Mistake 2: Using = instead of == when comparing
age = 25
print(age = 25) # wrong
Error:
SyntaxError: invalid syntax
= assigns. == compares. Use == to check if two things are equal.
age = 25
print(age == 25) # correct: True
Mistake 3: Wrong case on True and False
is_done = true # wrong
is_done = True # correct
Python is case-sensitive. true is not the same as True. True with a capital T is the boolean. true with a lowercase t is just a variable name Python doesn't know about.
Putting It Together
Here's a short program that uses all four types.
name = "Akhil"
age = 25
height = 5.9
is_student = True
print(f"Name: {name}")
print(f"Age: {age}")
print(f"Height: {height} feet")
print(f"Is a student: {is_student}")
print(f"Type of name: {type(name)}")
print(f"Type of age: {type(age)}")
next_age = age + 1
print(f"Next year I'll be {next_age}")
greeting = "Hello, " + name + "!"
print(greeting)
Output:
Name: Akhil
Age: 25
Height: 5.9 feet
Is a student: True
Type of name: <class 'str'>
Type of age: <class 'int'>
Next year I'll be 26
Hello, Akhil!
Type everything. Run it. Change the values. Break it on purpose. See what errors come up. Fix them.
Try This Yourself
Create a file called types_practice.py.
Make variables for all of this:
- Your full name (string)
- Your age (integer)
- Your height in any unit (float)
- Whether you have a pet (boolean)
- The year you were born (integer)
Then:
- Print each one with a label using f-strings
- Calculate how many years until you turn 50 and print it
- Print the type of each variable using
type()
Expected output should look something like:
Name: Akhilesh Yadav
Age: 22
Height: 5.9 feet
Has a pet: False
Birth year: 1999
Years until 50: 25
Type of name: <class 'str'>
Type of age: <class 'int'>
Hints: subtraction works with -. The year you turn 50 is your birth year plus 50.
What's Next
You can store information now. The next step is making your code take different actions depending on what that information is. That's what if/else statements do, and it's where programming starts to feel like actual decision-making.
Top comments (0)