In Python you print with print, putting the text in quotes inside brackets. Try it:
print("Hello")
print("Good morning")Hello Good morning
To print the result of a calculation, write it without quotes; anything inside quotes is printed exactly as written:
print("5+2")
print(5+2)
print(7*6)
print(48/6)5+2 7 42 8.0
| Operation | Symbol | Example | Result |
|---|---|---|---|
| Add | + | 278+422 | 700 |
| Subtract | - | 3000-850 | 2150 |
| Multiply | * | 7*6 | 42 |
| Divide | / | 48/6 | 8.0 |
/ in Python always gives a decimal: 48/6 = 8.0.Joining text and numbers
You join texts with +, but you cannot join text and a number directly. Turn the number into text first with str():
print("2 crepes " + 350 * 2)TypeError: can only concatenate str (not "int") to str
print("2 crepes " + str(350 * 2) + " pounds")2 crepes 700 pounds
Comments
A line that starts with # is a comment the computer ignores; use it for notes or to switch a line off.
# Groceries
print("Bread")
# print("Grapes")
print("Yogurt")Bread Yogurt
Practice
Print Good night.
Press Run to see the result. Fix the program so it prints Let's do our best.
Hint
Use double quotes " because the text contains '.print(Let's do our best)
Press Run to see the result. Make the computer calculate 1200 + 2800 and print the result.
Press Run to see the result. Fill in the blank to print 3 crepes 1050 pounds (350 × 3).
print("3 crepes " + ___ + " pounds")Press Run to see the result. Turn the line that prints Fish into a comment.
print("Mercury")
print("Fish")
print("Venus")Press Run to see the result. A plate of koshary costs 45 pounds. Print Total: 135 for three plates, and let the computer calculate 45 × 3.
Press Run to see the result. Key points
print("text")prints text;print(5+2)prints the result.- Operators:
+ - * /; division gives a decimal. str()turns a number into text before joining with+;#starts a comment.