Introduction:
The print()
function is one of the first things you'll learn in Python. It allows you to display information on the screen, like text (also known as strings) and numbers. This is super useful for showing messages or results of calculations. You'll be using print()
a lot, whether you want to say "Hello, World!" or display the answer to a maths problem.
Real-World Example:
When you're using a mobile app, you might see a welcome message or the result of a calculation. Behind the scenes, the app could be using a command like print()
to show that information on the screen.
Example:
print("Hello, World!") print(42) print("The result is", 3 + 4)
In this example, we use print()
to display a greeting, a number, and the result of adding two numbers.
PREDICT:
Before you move ahead, let's test your understanding of print()
. Take a look at the code below and try to predict what will happen when you run it.
print('Good Morning!') print(5 * 3)
Think about the output. What will the first line display? What about the second line?
Run:
Now, it’s time to see the print()
function in action! Let’s run this piece of code and observe how Python can print both text and numbers.
Press the “Run” button above to see how the print()
function displays text and numbers on the screen. You should see that the calculations are done first, and the result is then printed.
Investigate:
Let’s dig a little deeper. Look at the code below and run it. Focus on how text and numbers are combined in the output.
Instructions:
- Run the code.
- Notice how Python combines text and numbers in the same print statement. For example, it prints both the words "5 + 3 =" and the result of
5 + 3
on the same line. - Try changing the numbers in the calculations. What happens if you change
5 + 3
to8 * 2
or12 / 4
to9 - 2
?
Modify:
Now, let's challenge your skills by fixing an error in the following code. It has a small mistake – can you spot it?
Hint: You need to insert the correct operator between the numbers so the program multiplies 7
and 4
correctly. What operator is missing?