Swipe
SwipeSwipe
Swipe Apps

Paths

Motivation

Information

Booleans & Type Conversion · Lesson 11 of 11

Type Conversion

A free lesson from Swipe Coding · Python, level 2 · updated October 2026

By the Swipe team, founded by Colin · how we make lessons

🤖
Nothing converts behind your back here — you always have to ask for it explicitly. 🐍

Type conversion means turning a value of one type into another, and in Python you always do it explicitly by calling a function: int(), float(), or str(). Nothing converts automatically behind your back — you have to ask for it.

age_text = "25"
age_number = int(age_text)
print(age_number + 1)   # 26
print(type(age_number)) # <class 'int'>
int() converts a string of digits into an actual int, which can then be used in arithmetic.

float() works the same way but produces a decimal number, and str() goes the opposite direction, turning any value — a number, a boolean, anything — into its text representation.

price_text = "4.99"
price = float(price_text)
print(price * 2)  # 9.98

count = 5
message = "You have " + str(count) + " items"
print(message)
float() parses decimal text into a number; str() turns a number into text so it can be concatenated with +.
🤖
This is the gotcha that trips up almost everyone the first time. ⚠️

Converting between numeric types has one important trap: int() cannot directly parse a string that contains a decimal point. Calling int("3.5") raises a ValueError, because int() expects a string that looks like a whole number. To handle text like "3.5", you must convert it to a float first, and then, if you really want a whole number, convert that float to an int — which truncates (cuts off) the decimal part rather than rounding.

# int("3.5")        # raises ValueError: invalid literal for int()

value = float("3.5")  # 3.5, works fine
whole = int(value)     # 3, truncated — not rounded
print(whole)
int() rejects decimal-looking text directly, but converting to float first, then to int, works — and truncates.
Fill in the blank

Calling int("3.5") directly raises a ____, because int() cannot parse a decimal point in text.

Quick check

What is the correct way to turn the text "3.5" into the int 3?

True or false

str() can convert values of any type, including numbers and booleans, into their text representation.

Chapter quiz

Booleans & Type Conversion

10 questions · pass with 60% — just like in the app.

Quick check

How are the boolean values written in Python?

True or false

Comparison operators like == and >= always produce a bool result.

Fill in the blank

The number ____ is considered falsy in Python.

Quick check

Which of these values is falsy?

True or false

The value None is falsy in Python.

Fill in the blank

The ____ function converts a string like "25" into an actual whole number.

Quick check

What happens when you call int("3.5") directly?

True or false

Converting "3.5" to an int works if you convert it to a float first, then to an int.

Fill in the blank

The ____ function converts a value like 42 or True into its text representation.

Quick check

What does int(float("3.5")) evaluate to?

Keep your streak going in Swipe Coding

Swipe through every level of Python on your iPhone — with XP, stars, streaks, a personal advisor and the full track beyond these free levels.

Get the Swipe apps →