Now that you've learned about variables, different data types like numbers and strings, how to use operators, convert between types, and get input from the user, it's time to put these pieces together. This practice section provides hands-on examples to solidify your understanding of these fundamental building blocks. We'll create small programs that perform calculations and interact with the user.
Let's create a script that calculates the area of a rectangle. It will ask the user for the length and width, perform the calculation, and display the result.
input()
function returns text (a string).float()
.# Get input for the length of the rectangle
length_str = input("Enter the length of the rectangle: ")
# Get input for the width of the rectangle
width_str = input("Enter the width of the rectangle: ")
# Convert the input strings to floating-point numbers
length = float(length_str)
width = float(width_str)
# Calculate the area using the arithmetic multiplication operator
area = length * width
# Display the result using an f-string for formatted output
print(f"The length is: {length}")
print(f"The width is: {width}")
print(f"The area of the rectangle is: {area}")
Try running this code. When prompted, enter numerical values for the length and width. Observe how the program takes your text input, converts it to numbers suitable for calculation, performs the multiplication, and then prints a clear message with the computed area. What happens if you enter text instead of numbers? You should encounter a ValueError
, highlighting the importance of type conversion and anticipating potential input issues (which we'll learn to handle more gracefully later).
This example focuses on working with strings and user input to create a personalized message.
# Get the user's name
user_name = input("What is your name? ")
# Get the user's favorite color
favorite_color = input("What is your favorite color? ")
# Create a personalized greeting using an f-string
greeting = f"Hello, {user_name}! It's nice to meet you. {favorite_color} is a great color!"
# Print the greeting
print(greeting)
# Example of a simple string method
print(f"Your name in uppercase is: {user_name.upper()}")
Run this script. Enter your name and favorite color when asked. Notice how the input()
function captures your text directly into string variables. The f-string provides an easy way to embed these variables within a larger string to form the final greeting
. We also included an example of using a string method, .upper()
, to modify the case of the stored name.
Let's practice using comparison operators. This script will take two numbers and show the results of comparing them.
>
, <
, ==
, !=
) to compare the numbers.True
or False
) of each comparison.# Get the first number
num1_str = input("Enter the first number: ")
num1 = float(num1_str)
# Get the second number
num2_str = input("Enter the second number: ")
num2 = float(num2_str)
# Perform comparisons
is_greater = num1 > num2
is_less = num1 < num2
is_equal = num1 == num2
is_not_equal = num1 != num2
# Display the results
print(f"\nComparing {num1} and {num2}:")
print(f"Is {num1} greater than {num2}? {is_greater}")
print(f"Is {num1} less than {num2}? {is_less}")
print(f"Is {num1} equal to {num2}? {is_equal}")
print(f"Is {num1} not equal to {num2}? {is_not_equal}")
Execute this code. Enter two different numbers, then try entering the same number twice. Observe the output. Comparison operators evaluate the relationship between values and produce a boolean result (True
or False
), which is fundamental for controlling program logic in later chapters.
These examples cover the core concepts of this chapter: obtaining user input, storing it in variables, performing basic arithmetic and string operations, understanding data types and their conversion, and using comparison operators. Experiment with these scripts. Try different inputs, modify the calculations, or change the output messages. The more you practice, the more comfortable you'll become with these essential Python basics.
© 2025 ApX Machine Learning