What You Will Learn
Beginner
- All Python data types and their categories
- How to check and convert types
- When to use each type
Python data types organized by category
The Built-in Types
| Type | Example | Use case |
|---|---|---|
int | 42 | Whole numbers |
float | 3.14 | Decimal numbers |
str | "hello" | Text |
bool | True, False | True/false values |
list | [1, 2, 3] | Ordered, mutable collection |
tuple | (1, 2, 3) | Ordered, immutable collection |
dict | {"k": "v"} | Key-value pairs |
set | {1, 2, 3} | Unique unordered items |
NoneType | None | Represents nothing/null |
Checking Types
Terminalpython
x = 42
print(type(x)) #
print(isinstance(x, int)) # True Type Conversion
Terminalpython
# String to int
age_str = "28"
age = int(age_str)
print(age + 2) # 30
# Int to string
num = 42
text = str(num)
print("Number: " + text)
# String to float
price = float("19.99")
print(price) # 19.99
# Int to float (automatic)
result = 10 / 3 # 3.333 (always float in Python 3)Type conversion errors
int("hello") raises ValueError. int("3.14") also raises ValueError (use float() first). Always wrap in try/except for user input.Practical Exercise
Create one variable of each type: int, float, str, bool, list, dict
Print the type of each: print(type(var))
Convert: int("42"), str(100), float("3.14")
Try: int("hello") - see the error
Key Takeaways
- 9 built-in types: int, float, str, bool, list, tuple, dict, set, None.
- type() checks the type. isinstance() is safer for checks.
- Convert with int(), float(), str(), bool().
- int("hello") raises ValueError - always handle user input.
- 10 / 3 always returns float in Python 3.
Comments
Comments
Post a Comment