Type Conversion and Checking
Learn to change data types and check what type data is
What You'll Learn
- What type conversion is
- Converting between types
- Checking data types
- Avoiding common errors
What is Type Conversion?
Changing data from one type to another:
- Number → Text
- Text → Number
- Number → True/False
Converting to String (Text)
Use str() to make anything into text:
# Number to string
age = 25
age_text = str(age)
print(age_text) # "25"
# Use in sentences
message = "I am " + str(age) + " years old"
print(message)
# Shows: I am 25 years old# Float to string
price = 19.99
price_text = str(price)
print(price_text) # "19.99"Converting to Integer (Whole Number)
Use int() to make whole numbers:
# String to integer
age_text = "25"
age = int(age_text)
print(age + 5) # 30# Float to integer (removes decimal)
price = 19.99
whole_price = int(price)
print(whole_price) # 19Warning: Text must look like a number!
# This works
num = int("123") # 123
# This gives ERROR
num = int("hello") # Error!Converting to Float (Decimal Number)
Use float() to make decimal numbers:
# String to float
price_text = "19.99"
price = float(price_text)
print(price) # 19.99# Integer to float
age = 25
age_float = float(age)
print(age_float) # 25.0Converting to Boolean (True/False)
Use bool() to get True or False:
# Numbers
print(bool(1)) # True
print(bool(0)) # False
print(bool(-5)) # True
print(bool(100)) # True
# Strings
print(bool("Hello")) # True
print(bool("")) # False (empty = False)
# Lists
print(bool([1, 2])) # True
print(bool([])) # False (empty = False)Rule: Empty = False, Everything else = True
Checking Data Types
Use type() to see what type something is:
age = 25
name = "John"
price = 19.99
is_student = True
print(type(age)) # <class 'int'>
print(type(name)) # <class 'str'>
print(type(price)) # <class 'float'>
print(type(is_student)) # <class 'bool'>Practice Examples
Example 1: User input (always comes as text)
# Get age from user
age_text = input("Enter your age: ")
# Convert to number
age = int(age_text)
# Now you can do math
next_year = age + 1
print("Next year you will be:", next_year)Example 2: Calculate with text numbers
num1_text = "10"
num2_text = "5"
# Convert to integers
num1 = int(num1_text)
num2 = int(num2_text)
# Now calculate
total = num1 + num2
print("Total:", total)
# Shows: Total: 15Example 3: Format prices
price = 19.99
# Round to whole number
rounded = int(price)
print("Rounded price:", rounded)
# Shows: Rounded price: 19Example 4: Check if empty
name = input("Enter name: ")
if bool(name):
print("Hello", name)
else:
print("You didn't enter a name!")Common Conversions
# String to number
age = int("25")
price = float("19.99")
# Number to string
age_text = str(25)
price_text = str(19.99)
# To boolean
is_active = bool(1) # True
is_empty = bool("") # FalseSafe Conversion
Check before converting to avoid errors:
text = "123"
# Check if it's a digit
if text.isdigit():
number = int(text)
print("Converted:", number)
else:
print("Not a valid number!")Common Mistakes
Mistake 1: Adding text and numbers
# Wrong
age = 25
print("Age: " + age) # Error!
# Correct
print("Age: " + str(age)) # Age: 25Mistake 2: Converting invalid text
# This causes error
number = int("hello") # Error!
# Only convert number-like text
number = int("123") # Works!Mistake 3: Losing decimal precision
price = 19.99
whole = int(price)
print(whole) # 19 (lost .99!)Quick Reference
Convert to string:
str(value)Convert to integer:
int(value)Convert to float:
float(value)Convert to boolean:
bool(value)Check type:
type(value)When to Use Each
Use str():
- Combining with text
- Displaying numbers
Use int():
- Doing math with whole numbers
- User input calculations
Use float():
- Working with decimals
- Precise calculations
Use bool():
- Making decisions
- Checking if empty
Summary
str()converts to textint()converts to whole numberfloat()converts to decimalbool()converts to True/Falsetype()checks what type data is- Always convert when mixing types
What's Next?
Now let's learn how to make decisions in code with if statements!