Python Basics
Python Type Conversions
1. What is Type Conversion?
- Converting a value from one data type to another
- Also called type casting
- Not all conversions are possible — invalid ones raise an error
- Common need:
input()always returns a string, but you may need a number
2. The 4 Conversion Functions
| Function | Converts to | Example |
|---|---|---|
int() |
Integer | int("4") → 4 |
float() |
Float | float("4.5") → 4.5 |
bool() |
Boolean | bool("hello") → True |
str() |
String | str(45) → "45" |
These functions return the converted value — they do not change the original variable
3. Converting to int
x = "4"
y = int(x) # y = 4 (int), x is still "4" (str)
print(y + 4) # 8 ✅
What works and what doesn't:
int("4") # ✅ 4
int("4 ") # ✅ 4 (strips whitespace)
int(4.7) # ✅ 4 (truncates decimal — no rounding)
int("4hello") # ❌ ValueError — not a valid int
int("4.5") # ❌ ValueError — use float() instead
int()on a float truncates (chops off decimals), it does NOT round
4. Converting to float
float("4.5") # ✅ 4.5
float("4") # ✅ 4.0
float(4) # ✅ 4.0
float("4.7x") # ❌ ValueError
5. Converting to bool
Strings → bool
| Value | Result |
|---|---|
Any non-empty string ("hello", "0", " ") |
True |
Empty string "" |
False |
bool("hello") # True
bool("0") # True ← still True! It's a non-empty string
bool(" ") # True ← even a space is True
bool("") # False ← only empty string is False
Numbers → bool
| Value | Result |
|---|---|
Any non-zero number (1, -9, 0.2) |
True |
0 or 0.0 |
False |
bool(2) # True
bool(-9) # True
bool(0.2) # True
bool(0) # False
bool(0.0) # False
6. Converting to str
str(0) # "0"
str(23) # "23"
str(True) # "True"
str(4.5) # "4.5"
Verify it's a string via concatenation:
str(23) + "0" # "230" ← proves it's a string, not int (23+0 would be 23)
7. Practical Example — Fixing input() Type
# ❌ Crashes — input returns a string, can't add int to string
number = input("Enter a number: ")
result = number + 5 # TypeError
# ✅ Option 1 — convert at input
number = int(input("Enter a number: "))
result = number + 5
# ✅ Option 2 — convert when using
number = input("Enter a number: ")
result = int(number) + 5
Use float() instead of int() if the user might enter a decimal:
number = float(input("Enter a number: "))
result = number + 5 # works for both 4 and 4.3
If the user types a non-number (e.g.
"hello"), the program will crash — error handling is covered in a later lesson
8. Conversion Compatibility Cheat Sheet
| From → To | int() |
float() |
bool() |
str() |
|---|---|---|---|---|
str (numeric) |
✅ | ✅ | ✅ | ✅ |
str (non-numeric) |
❌ | ❌ | ✅ | ✅ |
int |
✅ | ✅ | ✅ | ✅ |
float |
✅ (truncates) | ✅ | ✅ | ✅ |
bool |
✅ | ✅ | ✅ | ✅ |
9. Key Takeaways & Recap
- Use
int(),float(),bool(),str()to convert between types - Conversion functions return the new value — original variable is unchanged
int("4.5")fails — convert tofloat()first, or usefloat()directlyint(4.7)→4— truncates, never roundsbool()isFalseonly for0,0.0, or""— everything else isTrue- Always convert
input()result before using it in math operations - Invalid conversions raise a
ValueError— no workaround except error handling