Data Types in Python
Data types define the kind of value a variable can hold. Python automatically determines the data type based on the value assigned.
π‘ Python is dynamically typed β you donβt need to specify data types explicitly.
Common Built-in Data Types
- int β Integer numbers (e.g.
10) - float β Decimal numbers (e.g.
3.14) - str β Text or strings (e.g.
"Python") - bool β True or False
Example
x = 10
price = 99.5
name = "Khushi"
is_active = True
print(type(x))
print(type(price))
print(type(name))
print(type(is_active))
Type Conversion
Python allows converting one data type to another using built-in functions.
x = "100"
y = int(x)
z = float(y)
print(y, z)
Common Mistakes
β Mixing incompatible data types
β Forgetting to convert user input (string) to numbers
β Forgetting to convert user input (string) to numbers
Practice Exercises
- Create variables of type int, float, and string
- Print their values and data types