Python can work with different types of data: numbers, text, boolean values. Let’s cover all the fundamental types!
📊 Core Data Types
1. int — Integers
age = 25
year = 2026
temperature = -15
big_number = 1000000
print(type(age)) # <class 'int'>
Where it’s used: counters, ages, quantities, years
2. float — Floating-Point Numbers
height = 1.75
price = 99.99
pi = 3.14159
temperature = -5.5
print(type(height)) # <class 'float'>
Where it’s used: heights, prices, percentages, coordinates
3. str — Strings (Text)
name = 'Anna'
city = "New York"
message = '''Multi-line
text'''
print(type(name)) # <class 'str'>
Where it’s used: names, addresses, messages, any text
4. bool — Boolean Type
is_student = True
has_license = False
print(type(is_student)) # <class 'bool'>
Where it’s used: condition checks, state flags
🔄 Type Conversion
String → Number
# String → Integer
age_str = '25'
age_int = int(age_str)
print(age_int + 5) # 30
# String → Float
price_str = '99.99'
price_float = float(price_str)
print(price_float * 2) # 199.98
# ⚠️ Error if the string is not a number!
int('abc') # ValueError!
Number → String
age = 25
age_str = str(age)
print('I am ' + age_str + ' years old') # I am 25 years old
# Or use f-strings (easier!)
print(f'I am {age} years old') # I am 25 years old
To Boolean
# Any non-zero number → True
bool(1) # True
bool(100) # True
bool(-5) # True
bool(0) # False
# Any non-empty string → True
bool('text') # True
bool('') # False (empty string)
🎯 Common Mistakes
Mistake 1: Concatenating a string and a number
# ❌ TypeError!
age = 16
print('I am ' + age + ' years old') # Error!
# ✅ Convert the number to a string
print('I am ' + str(age) + ' years old')
# ✅ Or use an f-string
print(f'I am {age} years old')
# ✅ Or separate with a comma
print('I am', age, 'years old')
Mistake 2: Dividing integers always yields a float
print(10 / 2) # 5.0 (float!)
print(10 // 2) # 5 (int)
# Even when the division is exact
result = 10 / 2
print(type(result)) # <class 'float'>
Mistake 3: True/False must be capitalized
# ❌ Error!
is_active = true # NameError!
# ✅ Correct
is_active = True
is_ready = False
💡 Checking Types
age = 25
# Get the type
print(type(age)) # <class 'int'>
# Check the type
if isinstance(age, int):
print('This is an integer')
if isinstance('text', str):
print('This is a string')
🔢 Operations with Different Types
int + int → int
a = 10
b = 5
print(a + b) # 15 (int)
print(type(a + b)) # <class 'int'>
int + float → float
a = 10 # int
b = 5.5 # float
print(a + b) # 15.5 (float)
print(type(a + b)) # <class 'float'>
str + str → str
first = 'Hello'
second = 'world'
print(first + ' ' + second) # Hello world
str * int → str
print('🔥' * 5) # 🔥🔥🔥🔥🔥
print('ha' * 3) # hahaha
🎨 String Specifics
Quotes
# Single quotes
name = 'Anna'
# Double quotes
city = "New York"
# Triple quotes (multi-line text)
message = '''
First line
Second line
Third line
'''
Escape Characters
# Newline
print('First\nSecond')
# Tab
print('Name:\tAnna')
# Quote inside a string
print('He said: \'Hello!\'')
Indexing
text = 'Python'
print(text[0]) # P (first character)
print(text[1]) # y (second character)
print(text[-1]) # n (last)
print(text[-2]) # o (second-to-last)
🚀 Practical Examples
Example 1: Type Calculator
num1 = int(input('First number: '))
num2 = int(input('Second number: '))
print(f'{num1} + {num2} = {num1 + num2}')
print(f'Type: {type(num1 + num2)}')
num3 = float(input('Floating-point number: '))
print(f'{num1} + {num3} = {num1 + num3}')
print(f'Type: {type(num1 + num3)}')
Example 2: Converter
# String → Number → String
age_str = input('Age: ')
age_int = int(age_str)
doubled = age_int * 2
result = str(doubled)
print(f'Doubled age: {result}')
Example 3: Type Checking
value = input('Enter something: ')
# Check what was entered
if value.isdigit():
print('That is a number!')
number = int(value)
print(f'Doubled: {number * 2}')
elif value.isalpha():
print('That is text!')
print(f'Uppercase: {value.upper()}')
else:
print('That is something else')
📋 Cheat Sheet
# Data types
int # Integer: 10, -5, 0
float # Float: 3.14, -0.5, 99.99
str # String: 'text', "text"
bool # Boolean: True, False
# Conversions
int('123') # '123' → 123
float('3.14') # '3.14' → 3.14
str(456) # 456 → '456'
bool(1) # 1 → True
# Checks
type(x) # Get the type
isinstance(x, int) # Check the type
value.isdigit() # String of digits?
value.isalpha() # String of letters?
🎓 Summary
- int — integers (10, -5, 0)
- float — floating-point numbers (3.14, 99.99)
- str — text (‘hello’, “world”)
- bool — True or False
- Convert types with: int(), float(), str(), bool()
- Python infers the type when a variable is created
- You cannot add numbers and strings directly!
Understanding data types is the key to writing error-free code! 💪
💬 Comments (0)
No comments yet
Be the first to share your opinion about this article!