Online course

PYTHON

TRAINING 1

1/ Let’s consider the following programs written in Python language.

Write the results displayed by Python.

2/ Create program that diplays the sum of the square of the multiples of 3 higher than 0 and smaller than 18.

TRAINING 2

Create a Python programme which displays the sum of the first n integers after the user has entered an integer n.

Suggestion : you can use a sum variable S, a counter variable I, and the « for » loop.

Training 1 - solution

1/ a/ It displays : 0 1 2 4 9 16

b/ It displays : 25 36 49 64 81

c/ It displays : 25 49 81 (here i in {5,7,9})

d/ It displays 30 (sum of the square of integers from 0 to 4)

2/ S=0

for i in range(0,19,3):

S=S+i**2

print(S)

Training 2 - solution

S=0

for i in range(0,19,3):

S=S+i**2

print(S)

RESOURCES

INVOLVED LESSONS