Side Quest: Effective Printing & F-Strings¶
In this bonus section, we'll explore how to use print statements to check how code changes variables, understand what the code does, and how f-strings can be used to achieve this task. This is a crucial skill for debugging and learning how your code works.
Using Print Statements to Track Changes¶
Print statements are a simple yet powerful tool to understand how your code is working. By printing the value of variables at different points in your code, you can see how they change over time.
Example: Tracking a Number¶
Let's start with a simple example where we print a number, perform an operation on it, and then print it again to see the output.
Example Code¶
number = 10
print("Initial number:", number)
# Perform an operation
number += 5
print("After adding 5:", number)
# Perform another operation
number *= 2
print("After multiplying by 2:", number)
Reflection¶
- What did the output look like?
- Did you see how the number changed after each operation?
Using F-Strings for Effective Printing¶
F-strings (formatted string literals) are a more readable and concise way to include variable values in your print statements. They were introduced in Python 3.6 and have become a popular way to format strings.
Example: Using F-Strings¶
Let's rewrite the previous example using f-strings.
Example Code¶
number = 10
print(f"Initial number: {number}")
# Perform an operation
number += 5
print(f"After adding 5: {number}")
# Perform another operation
number *= 2
print(f"After multiplying by 2: {number}")
Reflection¶
- How does the use of f-strings improve the readability of the code?
- Did you find it easier to understand the output with f-strings?
Using Print Statements in Conditional Statements and Loops¶
Print statements can also be used within conditional statements and loops to track how your code is changing your data.
Example: Print Statements in an If Statement¶
Let's see how we can use print statements to understand the flow of an if statement.
Example Code¶
number = 10
if number > 5:
print(f"{number} is greater than 5")
number -= 3
print(f"After subtracting 3: {number}")
if number < 10:
print(f"{number} is less than 10")
Reflection¶
- What did the output look like?
- Did you see how the value of
number
changed within the if statements?
Example: Print Statements in a Loop¶
Now, let's use print statements within a loop to track changes.
Example Code¶
for i in range(5):
print(f"Loop iteration {i}")
i_squared = i ** 2
print(f"{i} squared is {i_squared}")
Reflection¶
- What did the output look like?
- Did you see the pattern in the loop iterations and the squared values?
By using print statements and f-strings, you can gain a deeper understanding of how your code works and how variables change over time. This is an essential skill for debugging and learning to code effectively.