Everyone Can Learn Program
中文 Start learning
Course 2 · Python BasicsLesson 1 of 14Free to read

print() and calculations · Python from Zero

How do you make Python show something, and how does it calculate?

Course contents

Why you need it

Before a program can do anything useful, you need to see what it is doing. print() is your window into the program: you'll use it in every lesson from now on. And computers are, at heart, calculators, so arithmetic is the first thing worth trying.

The intuition

print(...) is like asking a friend to read something out loud. What's inside the brackets gets shown on its own line.

  • Text goes inside quotes: print("Hello") shows Hello. Quoted text is called a string.
  • Numbers don't need quotes: print(3 + 4) shows 7. Python works out the sum first.
  • print("3 + 4") shows 3 + 4: the quotes tell Python "this is just text, don't calculate".

Arithmetic symbols: + - * (times) / (divide), plus // (whole-number division), % (remainder) and ** (power).

Definition

print()

print() displays the values given to it, separated by spaces, followed by a new line.

String

A string is a sequence of characters written between quotes, such as "Hello".

Examples

Your first program

Try it yourself

Three lines, three outputs, in order.

print("Hello, world!")
print("I am learning Python.")
print(3 + 4)
Step by step
  1. L1Show the text between the quotes.Hello, world!
  2. L2Each print() starts a new line.I am learning Python.
  3. L3No quotes: Python calculates 3 + 4 first, then shows 7.7

The arithmetic toolkit

Try it yourself

Change the numbers and run it again. Several values in one print() are separated by spaces.

print(7 / 2)
print(7 // 2, 7 % 2)
print(2 ** 10)
print("3" + "4", 3 + 4)
Step by step
  1. L1/ always gives a decimal number: 3.5.3.5
  2. L2// keeps the whole part (3), % gives the remainder (1).3 1
  3. L3** is power: 2 to the 10th is 1024.1024
  4. L4"3" + "4" joins two strings: 34. 3 + 4 adds numbers: 7.34 7

Common mistakes

Text without quotes

print(Hello) is an error: without quotes Python looks for something named Hello. Text needs quotes.

Quotes around a calculation

print("2 * 5") prints the characters 2 * 5, not 10. Leave calculations unquoted.

Quick quiz

1. What does this print?

print("2 + 3")
  1. A
    5
  2. B
    2 + 3
  3. C
    23
Show the answer

Answer: B

The quotes make it a string, so Python shows it exactly as written.

2. What does this print?

print(10 // 3, 10 % 3)
  1. A
    3 1
  2. B
    3.33 1
  3. C
    3 0
Show the answer

Answer: A

10 ÷ 3 = 3 remainder 1. // gives 3, % gives 1, separated by a space.

Practice

3 exercises in three steps: predict what a program prints, change a program to meet a new requirement, then write one from scratch. Your code runs in the browser and is checked the moment you submit.

  1. PredictPredict three lines

    Type exactly what this program prints.

  2. ModifyHours in a week

    Change the program so it prints Hello, Python! on the first line and, on the second line, the number of hours in a week. Let Python calculate it (7 days × 24 hours).

  3. WriteA small receipt

    Notebooks cost 4 each and pens cost 1.5 each. You buy 3 notebooks and 2 pens. Print exactly:

    12
    3.0
    15.0

    (notebook cost, pen cost, total). Let Python do every calculation.

Practise this lesson

Open the interactive lesson to edit and run these examples and solve the exercises with instant feedback. Free to start, no sign-up.

Terms in this lesson

TermChineseDefinition
print()print() 输出函数A built-in function that displays values on the screen.
String字符串A sequence of characters written between quotes.
Floor division //整除 //Division that keeps only the whole-number part of the result.
Modulo %取余 %The remainder left over after whole-number division.