Python Tutorial: Print Output and Work with Variables
This tutorial will guide you through the basics of printing output and using variables in Python. We’ll build three examples, progressively increasing in complexity, to solidify your understanding.
Example 1: Basic Print Statements
The `print()` function is fundamental for displaying information in Python. You can print strings, numbers, and even the results of calculations.
- Step 1: Understand the `print()` function: The `print()` function takes one or more arguments (values) and displays them on the console.
- Step 2: Print a simple string: Let’s start by printing a greeting message.
# This code defines a string variable and prints it to the console.
message = "Hello, world!"
print(message)
In this example, we first define a string variable named `message` and assign it the value “Hello, world!”. Then, we use the `print()` function to display the value of `message` on the console.
Input: None – This is a simple print statement.
Output:
Hello, world!
Example 2: Printing Numbers and Performing Calculations
We’ll now work with numerical values and perform simple calculations before printing the results.
- Step 1: Assign numerical values: We’ll create variables to store numbers.
- Step 2: Perform calculations: We’ll add these numbers together.
- Step 3: Print the result: We’ll use `print()` to display the sum.
# This code calculates the sum of two numbers and prints the result.
num1 = 10
num2 = 5
sum_result = num1 + num2
print("The sum is:", sum_result)
In this example, we assign the integer values 10 and 5 to the variables `num1` and `num2`, respectively. We then calculate their sum and store it in the variable `sum_result`. Finally, we use the `print()` function to display the calculated sum along with a descriptive string.
Input: num1 = 10, num2 = 5
Intermediate values: num1 = 10, num2 = 5, sum_result = 15
Output:
The sum is: 15
Example 3: Variable Assignment and Formatting Output
Let’s create a more complex output with formatted strings. We’ll calculate the area of a rectangle.
- Step 1: Define variables: We’ll store the length and width of the rectangle.
- Step 2: Calculate the area: Area = length width.
- Step 3: Format the output: We’ll use f-strings to display the length, width, and area with descriptive labels.
# This code calculates the area of a rectangle and prints the result.
length = 7
width = 4
area = length width
print(f"The length of the rectangle is: {length}")
print(f"The width of the rectangle is: {width}")
print(f"The area of the rectangle is: {area}")
Here, we assign the values 7 and 4 to the variables `length` and `width`. We calculate the area by multiplying these two values. Then, we use f-strings (formatted string literals) to create a multi-line output, displaying the length, width, and area with appropriate labels. F-strings are a concise way to embed expressions directly within string literals.
Input: length = 7, width = 4
Intermediate values: length = 7, width = 4, area = 28
Output:
The length of the rectangle is: 7
The width of the rectangle is: 4
The area of the rectangle is: 28
Congratulations! You’ve completed the tutorial on printing output and working with variables in Python. These are fundamental building blocks for writing more complex programs.



Leave a Reply