In Python, data types determine what kind of value a variable holds. Understanding data types is essential because they define how data behaves and how you can interact with it.
| Type | Description | Example |
|---|---|---|
int |
Integer (whole number) | x = 10 |
float |
Decimal number | pi = 3.14 |
str |
String (text) | name = "Yunus" |
bool |
Boolean (True or False) | is_active = True |
age = 30 # int
price = 19.99 # float
first_name = "Yunus" # str
is_logged_in = False # boolYou can use the type() function to check the type of a value:
print(type(age)) # <class 'int'>
print(type(price)) # <class 'float'>
print(type(first_name)) # <class 'str'>
print(type(is_logged_in)) # <class 'bool'>You can convert values from one type to another:
x = 5 # int
x_str = str(x) # "5"
price = "19.99"
price_float = float(price)Built-in functions:
int()→ to integerfloat()→ to floatstr()→ to stringbool()→ to boolean
user_input = "42"
number = int(user_input)
print("Value:", number)
print("Type:", type(number))x = "5"
y = 2
result = x + y # ❌ TypeError: cannot concatenate str and int✅ Fix:
result = int(x) + y- Python has dynamic typing: variables can hold different types
- Use
type()to inspect data types - Use casting functions to convert between types
- Common types:
int,float,str,bool
Understanding data types is crucial for writing bug-free, efficient Python code. 🧠🐍