Displays

Contents

Displays#

In the following sheets, we will need to display information to trace the execution of a program step by step.

We give you here the minimum necessary for this.

We will come back to it in more detail later.

Displaying on the screen is one way to communicate between the program and the user.

As we have seen so far, when we execute a cell in Jupyter, the value of the last expression of the cell is displayed:

1+2
3+4
5+6
11

The other calculations were performed correctly by the machine, but without showing the values

obtained.

We will now see how to display information. The following cell

displays the result of the calculation:

print(1+2)
3

We can display several values in a row:

print(1+2)
print(3+4)
print(5+6)
3
7
11

You can also display text (character strings) using quotation marks:

print("Bonjour, comment allez-vous ?")
Bonjour, comment allez-vous ?

You can display the value of an expression obtained by evaluating a combination

of operators and variables:

a = 1
print(a + 2)
3

You can pass several arguments to the print function by separating them with a comma,

print will display them by concatenation using a blank space by default as a

separator.

Definition: Concatenation

In programming, the concaténation (concatenation) of two strings is called the

string formed by these two strings placed end to end. The term

concaténation (concatenation) also refers to the operation of concatenating strings.

age = 32
print("J'ai ", age, " ans.") 
J'ai  32  ans.

Exercise ♣

  • Adapt the previous example to display your age:

### BEGIN SOLUTION
age = 46
print("J'ai ", age, " ans.")
### END SOLUTION;
J'ai  46  ans.
  • Use the annee variable below to display “I was born in ****” where the **** is your birth year:

annee = 2000
### BEGIN SOLUTION
annee = 1985
print("Je suis née en ", annee)
### END SOLUTION;
Je suis née en  1985

Summary#

In this sheet, we have seen the minimum required to display values in

Python. This will be used later in the lab to trace the intermediate steps in

the execution of a loop.