Python basic

The print() Function

Once we understand variables, it’s important to see how we can display the values they store. This is where the print() function comes in. The print() function is one of the most commonly used functions in Python, and it allows you to output information to the screen.


How print() works?

The print() function takes one or more values (like variables, numbers, or text) and displays them in the output.

Example:
print("Hello, world!")  # This will output: Hello, world!

But what if you want to display the value of a variable? You can pass the variable inside print() to show its contents.

age = 25
print(age)  # This will output: 25

In this example, the value stored in the variable age is displayed on the screen.


Combining text and variables:

You can also combine text and variables to make the output more informative. This is done by using commas or formatted strings (f-strings).

1. Using commas:

You can separate different items in print() with commas. Python will automatically add spaces between them.

name = "Alice"
age = 25
print("Name:", name, "Age:", age)
  
 Output:
Name: Alice Age: 25


2. Using f-Strings (formatted strings):

Python also allows you to insert variables directly inside a string by using an f before the string. This makes the code easier to read and cleaner.

name = "Alice"
age = 25
print(f"My name is {name} and I am {age} years old.")
  
Output:
My name is Alice and I am 25 years old.


More examples of using print():

Let’s look at a few more examples to understand how print() can be used with variables:

price = 100
tax_rate = 0.2
total = price + (price * tax_rate)

# Displaying variables
print("Price:", price)
print("Tax Rate:", tax_rate)
print("Total Price:", total)

# Using f-string
print(f"The total price after tax is: {total}")

Output:
Price: 100
Tax Rate: 0.2
Total Price: 120.0
The total price after tax is: 120.0

In this example:
- We use print() to show the values stored in price, tax_rate, and total.
- The f-string allows us to create a more readable output that combines text and variables.


Why use print()?

When writing programs, it's important to see what your code is doing. Using print() helps you debug by showing the current values of your variables.
By printing values, you can test and learn how Python handles different types of data.

It's time to take a quiz!

Test your knowledge and see what you've just learned.

What is the primary purpose of the print() function in Python?

To output information to the screen
To calculate a value
To store data in variables
To read user input
Check Answer

Which line of code will correctly print the value stored in the variable age?

print("age")
print(age)
age.print()
output(age)
Check Answer

What is the output of the following code: name = "Alice"; age = 25; print("Name:", name, "Age:", age)?

"Name: Alice Age: 25"
"Name Alice Age 25"
"Name: Alice Age: 25"
"Alice 25"
Check Answer

Which line of code correctly uses an f-string to print a variable?

print(f("age is {age}"))
print(f"age is {age}")
print("age is f{age}")
print("age is" + age)
Check Answer

How can the print() function help in debugging your code?

By displaying the current values of variables
By stopping the program if there is an error
By modifying the values of variables
By checking for syntax errors
Check Answer