Python Data Types · Exercises
Beginner

Python Data Types Exercises

4 exercises that build on the Python Data Types lesson, from easy to hard. Each one is checked in your browser when you press Check. Stuck? Use the hint before peeking at the solution.

What this lesson covers: A data type tells Python what kind of value something is — a whole number, a decimal, text, True/False or nothing at all — and therefore what you can do with it. By the end you can name Python's basic data types, check a value's type with type(), and convert values with int(), float(), str() and bool(). You will practise: Fix the fruit total; Predict the types; Build a receipt line; Fix the sign-up form.

0 / 4 completed

Python Data Types practice exercises

1. Fix the fruit total

Fix the bug Easy

The fruit counts arrive as text. This code should print Total fruit: 20, but it prints Total fruit: 128. Fix it so that total is the number 20.

Your code
apples = "12"
oranges = "8"
total = apples + oranges
print("Total fruit:", total)

2. Predict the types

Predict the output Medium

Read the code and type exactly what it prints. Then press Check to run it and compare.

Read this code
print(type(7 / 2))
print(int(7.9))
print("7" * 2)
print(type("False"))
print(bool(0.0), bool(" "))

3. Build a receipt line

Write code Medium

A café till stores an item name, a price and the number of cups — but cups arrived as text. Create:

  • total — a float: the price times the number of cups,
  • line — a str exactly like Coffee x3 = 7.5, built from the variables using + and str().

Then print line.

Your code
item = "Coffee"
price = 2.5
cups = "3"

# create total and line here

4. Fix the sign-up form

Fix the bug Hard

A sign-up form sends every value as text. This program should print only Leo will be 17 next year. It crashes — and once the crash is fixed, it also wrongly claims Leo is a member.

Fix it so that age holds the int 16 and is_member holds the bool False.

Your code
name = "Leo"
age = "16"
is_member = "False"

next_age = age + 1
if is_member:
    print(name, "is a member")
print(name, "will be", next_age, "next year")

Next lessonPython Numbers

More Python Data Types examples · All Python exercises