Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

math208orsay

Project: math208
Views: 112
Kernel: Python 3 (Ubuntu Linux)

Formatage de chaînes de caractères en Python

for n in range(11): print(n, n**2, n**3)
0 0 0 1 1 1 2 4 8 3 9 27 4 16 64 5 25 125 6 36 216 7 49 343 8 64 512 9 81 729 10 100 1000
for n in range(11): print("{} {} {}".format(n, n**2, n**3))
0 0 0 1 1 1 2 4 8 3 9 27 4 16 64 5 25 125 6 36 216 7 49 343 8 64 512 9 81 729 10 100 1000
for n in range(11): print("{:3} {:5} {:7}".format(n, n**2, n**3))
0 0 0 1 1 1 2 4 8 3 9 27 4 16 64 5 25 125 6 36 216 7 49 343 8 64 512 9 81 729 10 100 1000
for n in range(11): print("{:10.2f} {:10.2f} {:10.2f}".format(n, n**2, n**3))
0.00 0.00 0.00 1.00 1.00 1.00 2.00 4.00 8.00 3.00 9.00 27.00 4.00 16.00 64.00 5.00 25.00 125.00 6.00 36.00 216.00 7.00 49.00 343.00 8.00 64.00 512.00 9.00 81.00 729.00 10.00 100.00 1000.00
for n in range(11): print("{2:7} {0:7} {1:7}".format(n, n**2, n**3))
0 0 0 1 1 1 8 2 4 27 3 9 64 4 16 125 5 25 216 6 36 343 7 49 512 8 64 729 9 81 1000 10 100
import math
print("pi vaut environ {:.5f}".format(math.pi))
pi vaut environ 3.14159
for n in range(10): print(f"{n**2-n:3} est divisible par {n-1:2}")
0 est divisible par -1 0 est divisible par 0 2 est divisible par 1 6 est divisible par 2 12 est divisible par 3 20 est divisible par 4 30 est divisible par 5 42 est divisible par 6 56 est divisible par 7 72 est divisible par 8
import numpy as np
def affiche_signe(T): """ Affiche le signe des éléments du tableau. """ signe = np.where(T >=0, '+', '-') for i in range(T.shape[0]): print(" ".join(signe[i, :]))
a = np.array([[np.random.randint(-5, 6) for i in range(4)] for i in range(4)])
print(a)
[[-5 -1 1 3] [-3 -4 4 4] [-5 1 -5 -2] [-4 -4 -2 2]]
affiche_signe(a)
- - + + - - + + - + - - - - - +