Learn Without Walls
← Previous Lesson Lesson 4 of 4 Practice Problems →

Booleans and Type Conversion

What You'll Learn

1. Boolean Values: True and False

Boolean (bool): A data type that can only have two values: 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 False <class 'bool'>
Important: 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")
True False True True

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'>
<class 'int'> <class 'float'> <class 'str'> <class 'bool'> <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
<class 'str'>

3. Type Conversion Functions

Python provides built-in functions to convert between data types:

FunctionConverts toExampleResult
int()Integerint("42")42
float()Floatfloat("3.14")3.14
str()Stringstr(42)"42"
bool()Booleanbool(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)
30 The price is $19.99 10.0 9
Careful: 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
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")
I am 25 years old I am 25 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).

Falsy values: 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!)
False False False False True True True True

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))
Student: Maria Garcia Age: 19 GPA: 3.85 Full-time: True Data types: <class 'str'> <class 'int'> <class 'float'> <class 'bool'>

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!

  1. Use type() to check the type of: 42, 42.0, "42", True.
  2. Convert the string "100" to an integer, add 50 to it, then convert the result back to a string.
  3. Test bool() on several values: 0, 1, -5, "", "0", 0.0.

Key Takeaways

← Previous: Strings Practice Problems →