Booleans and Type Conversion
What You'll Learn
- What Booleans are and when to use them
- How to check a value's type with
type() - Converting between integers, floats, strings, and Booleans
- What values are "truthy" and "falsy" in Python
- Common type conversion errors and how to avoid them
1. Boolean Values: True and False
True or False. Booleans represent yes/no, on/off, or true/false conditions.
Booleans are the foundation of decision-making in programming. You will use them extensively when you learn about if statements in a future module.
is_student = True has_permission = False print(is_student) print(has_permission) print(type(is_student))
True and False must be capitalized. Writing true or false (lowercase) will cause a NameError in Python.
Booleans often come from comparisons:
print(5 > 3) # True (5 is greater than 3) print(10 == 20) # False (10 does not equal 20) print(7 <= 7) # True (7 is less than or equal to 7) print("a" != "b") # True ("a" is not equal to "b")
2. Checking Types with type()
The type() function tells you what kind of data a value or variable holds:
print(type(42)) # <class 'int'> print(type(3.14)) # <class 'float'> print(type("hello")) # <class 'str'> print(type(True)) # <class 'bool'> print(type(None)) # <class 'NoneType'>
Why Does Type Matter?
Knowing the type of your data matters because different types support different operations. You can add two numbers, but adding a number and a string causes an error. The type() function helps you debug when things go wrong.
value = "42" print(type(value)) # <class 'str'> -- it's a string, not a number! # value + 10 would cause a TypeError
3. Type Conversion Functions
Python provides built-in functions to convert between data types:
| Function | Converts to | Example | Result |
|---|---|---|---|
int() | Integer | int("42") | 42 |
float() | Float | float("3.14") | 3.14 |
str() | String | str(42) | "42" |
bool() | Boolean | bool(1) | True |
# String to number age_text = "25" age_number = int(age_text) print(age_number + 5) # Now we can do math! # Number to string price = 19.99 message = "The price is $" + str(price) print(message) # Integer to float and back print(float(10)) # 10.0 print(int(9.7)) # 9 (truncates, does NOT round)
int() truncates (cuts off) the decimal part. It does NOT round. int(9.9) gives 9, not 10. Use round() if you want rounding.
4. Common Conversion Errors
Not every conversion is possible. Python will raise an error if the conversion does not make sense:
# These will cause ValueError: # int("hello") # Cannot convert "hello" to a number # int("3.14") # Cannot convert "3.14" directly to int # float("abc") # Cannot convert "abc" to a float # Solution for "3.14" to int: convert to float first, then to int result = int(float("3.14")) print(result) # 3
A Common Beginner Mistake
# This is a very common mistake! age = 25 # print("I am " + age + " years old") # TypeError! # The fix: convert age to a string print("I am " + str(age) + " years old") # Or even better, use an f-string (you'll learn this in Module 3!) print(f"I am {age} years old")
5. Truthy and Falsy Values
Every value in Python can be treated as a Boolean. The bool() function shows you whether a value is considered "truthy" (converts to True) or "falsy" (converts to False).
0, 0.0, "" (empty string), None, False. Everything else is truthy.
# Falsy values print(bool(0)) # False print(bool(0.0)) # False print(bool("")) # False (empty string) print(bool(None)) # False # Truthy values print(bool(42)) # True (any non-zero number) print(bool(-1)) # True (negative numbers too!) print(bool("hello")) # True (any non-empty string) print(bool(" ")) # True (even a space is not empty!)
This concept becomes very useful when you start writing if statements. For now, just remember the pattern: zero and empty are False, everything else is True.
6. Putting It All Together
Let us look at a practical example that uses variables, different data types, and type conversion:
# A simple student info program student_name = "Maria Garcia" student_age = 19 gpa = 3.85 is_full_time = True # Display info using type conversion print("Student: " + student_name) print("Age: " + str(student_age)) print("GPA: " + str(gpa)) print("Full-time: " + str(is_full_time)) # Check all the types print("\nData types:") print(type(student_name)) print(type(student_age)) print(type(gpa)) print(type(is_full_time))
Check Your Understanding
Question 1: What does int(7.9) return?
Answer: 7
int() truncates (cuts off) the decimal, it does not round. So 7.9 becomes 7.
Question 2: Which of these will bool() convert to False?
"False", 0, "", " ", -1, None
Falsy: 0, "", None
Truthy: "False" (it is a non-empty string!), " " (a space is still a character), -1 (any non-zero number)
Try It Yourself!
- Use
type()to check the type of:42,42.0,"42",True. - Convert the string
"100"to an integer, add 50 to it, then convert the result back to a string. - Test
bool()on several values:0,1,-5,"","0",0.0.
Key Takeaways
- Booleans have exactly two values:
TrueandFalse(capitalized!). type()tells you the data type of any value or variable.- Use
int(),float(),str(), andbool()to convert between types. int()truncates decimals (does not round).- Falsy values:
0,0.0,"",None,False. Everything else is truthy. - Not all conversions are valid --
int("hello")will cause an error.