Exercice 1

In [1]:
2018**2018 % 10
Out[1]:
4
In [2]:
%timeit (2018**2018 % 10)
19 ns ± 0.701 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
In [3]:
%timeit (pow(2018, 2018) % 10)
113 µs ± 1.69 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
In [4]:
%timeit (pow(2018, 2018, 10))
1.29 µs ± 111 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
In [5]:
help(pow)
Help on built-in function pow in module builtins:

pow(x, y, z=None, /)
    Equivalent to x**y (with two arguments) or x**y % z (with three arguments)
    
    Some types, such as ints, are able to use a more efficient algorithm when
    invoked using the three argument form.

Exercice 2

In [6]:
1.5 - 1
Out[6]:
0.5
In [7]:
(1 - 2).
  File "<ipython-input-7-44bffd759976>", line 1
    (1 - 2).
            ^
SyntaxError: invalid syntax
In [8]:
.5 - .3
Out[8]:
0.2
In [9]:
4 / (9 - 3 ** 2)
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
<ipython-input-9-eaedadc4527a> in <module>()
----> 1 4 / (9 - 3 ** 2)

ZeroDivisionError: division by zero
In [10]:
float(7 // 2)
Out[10]:
3.0

Exercice 3

Méthode naïve (et fausse !) :

In [11]:
x, y, z = "x", "y", "z"
In [12]:
z = x
y = z
x = y
In [13]:
(x, y, z)
Out[13]:
('x', 'x', 'x')

Utilisation de variables temporaires :

In [14]:
x, y, z = "x", "y", "z"
In [15]:
tmp_z = z
tmp_y = y
z = x
y = tmp_z
x = tmp_y
In [16]:
(x, y, z)
Out[16]:
('y', 'z', 'x')

Avec une seule variable temporaire :

In [17]:
x, y, z = "x", "y", "z"
In [18]:
tmp_z = z
z = x
x = y
y = tmp_z
In [19]:
(x, y, z)
Out[19]:
('y', 'z', 'x')

Exercice 4

In [20]:
 8.5 / 2.5
Out[20]:
3.4
In [21]:
int(8.5) / int(2.5)
Out[21]:
4.0
In [22]:
int(8.5 / 2.5)
Out[22]:
3

Exercice 5

In [23]:
int(-3 / 2)
Out[23]:
-1
In [24]:
-3 // 2
Out[24]:
-2
In [25]:
import math
math.floor(-3 / 2)
Out[25]:
-2

Exercice 6

Cf. corrigé du TP n°4

Exercice 7

Cf. corrigé du TP n°4