Flat_Eric Contemplating Data Type Conversions

Type Conversion

Type Conversion

In Python, every piece of data has a "type"—like a string for text or an integer for whole numbers. Usually, Python is smart enough to know what you mean. However, when you start taking information from users (like asking for their birth year), Python sees everything they type as text.

If you try to do math with text, Python gets confused. Type Conversion is the process of changing a value from one data type to another so you can perform the right operations on it.

Building a Student Profile

To understand how data works in Python, let’s pretend we are building a student database. We need a way to store information and update it when things change.

Creating the Profile (Variables & F-Strings):

First, we create three variables to store our student's information: your_name, age, and course. To see this information in the terminal, we use an f-string. This allows us to "plug" our variables directly into a sentence.

 
# Initial student profile
your_name = 'Kevin Spencer'
age = 14
course = 'Python Essentials'

print(f'{your_name} is {age} years old and is enrolled in: {course}')
    
 
Kevin Spencer is 14 years old and is enrolled in: Python Essentials
    
 
# If we want to update the database with new data,
# we can change the course variable and then print the data to see the change.
your_name = 'Kevin Spencer'
age = 14
course = 'Intro to Arduino'

print(f'{your_name} is {age} years old and is enrolled in: {course}')
    
 
Kevin Spencer is 14 years old and is enrolled in: Intro to Arduino
    

Time to experiment!

Coding Exercises (VS Code) Instructions:

Exercise 1: The Birthday Calculator:

Write a program that asks the user for the year they were born. Subtract that year from the current year (2026) and print a message telling them how old they are.

Hint: You might see an error when you run the program. Read the error message carefully and try to solve the problem using type conversion.

Exercise 2: The Pizza Party Splitter:

Ask the user for two pieces of information:

  1. The total number of pizza slices.
  2. The number of people sharing. Divide the slices by the people and print: "Each person gets X slices!"

Hint: You might see an error when you run the program. Read the error message and try to solve it using type conversion.

Exercise 3: The Grocery Receipt:

Ask the user for the price of a loaf of bread and the price of a carton of milk (use decimals like 2.50). Add them together and print the total bill.

Hint: If your total is missing the cents, or you see an error, try using a different type of conversion like float().

Exercise 4: The Lucky Number Multiplier :

Ask the user for their "Lucky Number." Take that number and multiply it by 10, then print the result as a math calculation.

Hint: If the number just prints ten times in a row instead of doing math, you need to use type conversion!

Exercise 5: The "Whole Number" Shaver:

Ask a user to enter their height in centimeters using a decimal (e.g., 165.5). Your program should convert that number into a whole number (integer) to "shave off" the decimal part and print the result.

Don't Forget to commit and Push!

This course was built by DevSTEM - we turn teaching materials into interactive web courses like this one.

Build your own course