Prev
Data Types & Converting Between Them
Challenge In Progress
0
20
5
P

Working with Numbers, Text, True/False & Type Conversion

In everyday life, you handle different kinds of information differently: you do math with numbers, you read words in a book, and you answer questions with yes or no. Python does the exact same thing using data types. Every piece of data in Python has a type that tells the computer what operations make sense. You can add two numbers together, but trying to subtract a word from a number doesn't make sense!
The four most fundamental data types you will use constantly are: integers (int) for whole numbers like 4 or -12; floating-point numbers (float) for numbers with a decimal point like 19.50 or 3.14; strings (str) for text wrapped in quotation marks like "apple" or "Hello"; and booleans (bool) for truth values, which can only be either True or False.
A very common situation in programming is receiving numbers that are disguised as text. For example, when a user types into a website form or an app reads data from a file, numbers often arrive as strings wrapped in quotes, like "4" or "19.50". If you try to multiply "4" * "19.50", Python raises a TypeError because it refuses to multiply text together like numbers. Python protects you by requiring you to be clear about what you intend.
To solve this, Python gives you converter tools called casting functions: int() converts compatible text into a whole number, float() converts text into a decimal number, and str() converts numbers back into text. For example, writing int("4") gives you the real number 4 that you can safely calculate with. Converting data from one type to another is called type conversion or casting, and it is a core skill you will use in almost every program you write.
Real-World Analogy
Imagine raw materials entering a kitchen: raw whole grains (strings) cannot be eaten straight out of the sack. You have to run them through a grinder and convert them into clean flour (numbers) before you can bake them into a cake!
Code ExamplePython 3
# Python has four fundamental data types:
# 1. Whole numbers (int)
item_count = 5

# 2. Decimal numbers (float)
price = 19.99

# 3. Text (str)
customer_name = "Jordan"

# 4. Yes/No decisions (bool)
in_stock = True

# Converting between types:
# Turning text into an integer for math:
user_entry = "42"
number = int(user_entry)
print("Calculation:", number + 8)

# Turning a number into text:
print("Receipt: " + str(price))
Key Rules & Concepts
  • Every value in Python belongs to a data type, such as int (whole number), float (decimal), str (text), or bool (True/False).
  • Python prevents accidental errors by refusing to mix incompatible types (such as adding a word to a number).
  • Use int() to turn text containing whole numbers into real integers.
  • Use float() to turn text containing decimals into decimal numbers.
  • Use type(variable).__name__ to inspect the name of any variable's data type.

Challenge

Beginner

Convert incoming string numbers from an online shopping order into real numbers, calculate the order total, and display the detected data types.

What to do:

  1. Given item_qty_str = "4" and unit_price_str = "19.50"
  2. Cast item_qty_str to an int named qty, and unit_price_str to a float named price
  3. Compute subtotal = qty * price
  4. Print f"Qty Type: {type(qty).__name__}"
  5. Print f"Price Type: {type(price).__name__}"
  6. Print f"Subtotal: ${subtotal
Reference Solution Locked
0 Attempts

Solve the challenge above and run your code to verify it. If you are stuck, you can unlock the reference solution after running your code at least once.

Previous Lesson
Python
Write Code
1
2
3
4
5
6
7
8
9
10
item_qty_str = "4"
unit_price_str = "19.50"

# TODO: Cast item_qty_str to integer variable qty
# TODO: Cast unit_price_str to float variable price
# TODO: Calculate subtotal = qty * price
# TODO: Print f"Qty Type: {type(qty).__name__}"
# TODO: Print f"Price Type: {type(price).__name__}"
# TODO: Print f"Subtotal: ${subtotal:.2f}"

TEST CASESCONSOLE
Output
// Press "Run Code" to execute
Expected Output
Qty Type: int Price Type: float Subtotal: $78.00