Data Type Conversion
In the last lesson, we saw that Python refuses to add a Number to a String (like 9 + '9'). To fix this, we need to convert one data type into another. In Python, this is done using built-in functions.
The str() Function
The str() function takes an integer or a float and converts it into a string. It essentially "wraps" quotes around a number so it can be handled like text.
# Converting an integer to a string
age = 25
age_string = str(age)
# Now Python sees "25" instead of 25
Why is this useful?
This is most helpful when you want to use concatenation "the action of linking things together in a series." to include a number in a sentence. Without str(), the following code would crash:
score = 100
print("Your total score is: " + str(score))
By using str(score), you turn the integer 100 into the text "100", allowing Python to "glue" them together perfectly.
Nested Functions vs. Sequential Steps
In Python, you can perform multiple conversions in a single line. However, the way you write the code changes how readable it is.
-
Nested Functions
When you "nest" functions, Python executes them from the inside out.
print(type(int(str(199))))- str(100): The innermost layer runs first, turning the number into "100".
- int(...): The next layer turns that string back into an integer.
- type(...): The third layer identifies the data type.
- print(...): The outermost layer finally displays the result.
-
Sequential Steps (Using Variables)
This version does the exact same work but breaks it down into clear, readable steps using variables.
a = str(199) # Step 1: Convert to string b = int(a) # Step 2: Convert to integer c = type(b) # Step 3: Check the type print(c) # Step 4: Display result
Which one should students use?
Nested Functions are great for quick tasks and keeping code concise, but they can be harder to read if there are too many layers.
Sequential Steps are much better for beginners because they make it easy to debug. If something goes wrong, you can check the value of a or b to find exactly where the error happened.
Time to experiment!
Coding Exercises (VS Code) Instructions:
- Create a file named type_conversion.py in your part11 folder.
- Complete the tasks and use # comments to explain your answers.
Exercise 1:
The Birthday Card
Instructions
In this exercise, you need to join a name (string) with an age (integer) to create a message.
- Create a variable called user_name and set it to your name.
- Create a variable called age and set it to any number (e.g., 15).
- Try to print: "Happy Birthday [user_name]! You are [age] today!"
Exercise 2:
The Scoreboard Debugger
Instructions
Your job is to fix the following broken code in your editor. Enter this into VS Code and run it:
points_earned_str = 500
total_message = "Your final score is: " + points_earned
print(total_message)
Exercise 3:
The Step-by-Step Chain
Instructions
This exercise practices the "sequential steps" method we discussed. You will transform a piece of data through three different forms.
- Step A: Create a variable named start_value and set it to the string "150".
- Step B: Create a new variable as_integer that converts start_value into an integer.
- Step C: Create a final variable check_type that uses the type() function to check as_integer.
- Step D: Print check_type.
The Challenge: Once you have that working, try to write the same logic on a single line using nested functions.
Don't Forget to commit and Push!