Question 1

In [2]:
L=list(range(41))
print(sum(L))
820

Question 2

In [3]:
for i in range(0,41,2):
	L[i] *= L[i]
print(sum(L))
11880

Question 3

In [4]:
L = []
for n in range(2000,3001):
	if n % 7 == 0 and n % 3 != 0:
		L.append(n)
print(L)
print(len(L))
[2002, 2009, 2023, 2030, 2044, 2051, 2065, 2072, 2086, 2093, 2107, 2114, 2128, 2135, 2149, 2156, 2170, 2177, 2191, 2198, 2212, 2219, 2233, 2240, 2254, 2261, 2275, 2282, 2296, 2303, 2317, 2324, 2338, 2345, 2359, 2366, 2380, 2387, 2401, 2408, 2422, 2429, 2443, 2450, 2464, 2471, 2485, 2492, 2506, 2513, 2527, 2534, 2548, 2555, 2569, 2576, 2590, 2597, 2611, 2618, 2632, 2639, 2653, 2660, 2674, 2681, 2695, 2702, 2716, 2723, 2737, 2744, 2758, 2765, 2779, 2786, 2800, 2807, 2821, 2828, 2842, 2849, 2863, 2870, 2884, 2891, 2905, 2912, 2926, 2933, 2947, 2954, 2968, 2975, 2989, 2996]
96

Question 4

In [5]:
n = int(input())
if n%2 == 0:
	print(n, "est pair")
else:
	print(n, "est impair")
2
2 est pair

Question 5

In [6]:
def Sum(N):
	x = 0
	for i in range(1,N+1):
		x += 1./(i*i);
	return x


print(Sum(10))
print(Sum(20))
print(Sum(100))

from math import pi
print(pi*pi/6.)
1.5497677311665408
1.5961632439130233
1.6349839001848923
1.6449340668482264

Question 6

In [7]:
def EnleverDoubles(L):
	Lf = []
	for x in L:
		if not x in Lf:
			Lf.append(x)
	return Lf

print(EnleverDoubles([1,1,5,3,3,1,3,5,1]))
[1, 5, 3]

Question 7

In [8]:
def VoyelleouConsonne(c):
	if c in ['a', 'e', 'i', 'o', 'u', 'y']:
		print("Voyelle")
	else:
		print("Consonne")

VoyelleouConsonne('a')
VoyelleouConsonne('b')
VoyelleouConsonne('c')
VoyelleouConsonne('d')
VoyelleouConsonne('e')
Voyelle
Consonne
Consonne
Consonne
Voyelle

Question 8

In [9]:
def derive(f,x,eps):
	return ( f(x+eps)-f(x-eps) )/(2*eps)
	
from math import exp

print(derive(exp,1.,0.000001))
print(exp(1.))
2.718281828295588
2.718281828459045

Question 9

In [10]:
def Palindrome(s):
	test = True
	for i in range(len(s)//2):
		if s[i] != s[-i-1]:
			test = False
	return test

print(Palindrome("kayak"))
print(Palindrome("truc"))
True
False

Question 10

In [11]:
def Temps(t1,t2):
	nb = (t2[0] - t1[0])*3600 +  (t2[1] - t1[1])*60 + (t2[2] - t1[2])
	print("Entre", t1[0], "h", t1[1], "min", t1[2], "s et", t2[0], "h", t2[1], "min", t2[2], "s il s'est ecoule",nb, "s")
	
t1 = (10, 20, 10)
t2 = (12, 35, 15)

Temps(t1, t2)
Entre 10 h 20 min 10 s et 12 h 35 min 15 s il s'est ecoule 8105 s

Question 11

In [12]:
def Binomial(n,k):
	if k == 0 or k==n :
		return 1
	elif k < 0 or k > n:
		return 0
	else:
		return Binomial(n-1,k-1) + Binomial(n-1,k)

print(Binomial(10,5))
252

Question 12

In [13]:
def Diviseurs(n):
	Ld = []
	for x in range(1,n+1):
		if n % x == 0:
			Ld.append(x)
	return Ld


print(Diviseurs(1995))
print(Diviseurs(1999))


def EstPremier(n):
	Ld = Diviseurs(n)
	if len(Ld) == 2:
		return True
	else:
		return False

print(EstPremier(1995))
print(EstPremier(1999))
[1, 3, 5, 7, 15, 19, 21, 35, 57, 95, 105, 133, 285, 399, 665, 1995]
[1, 1999]
False
True
In [ ]: