-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcours python.py
More file actions
146 lines (127 loc) · 4.4 KB
/
cours python.py
File metadata and controls
146 lines (127 loc) · 4.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/python3.6
# -*- coding: utf-8 -*-
def showArgs (argPositionnalA, argPositionnalB, argDefault=10):
messageRed = 'bonjour {}, voici {}. vous allez séjourner {} jours ensemble.'
print (messageRed.format (argPositionnalA, argPositionnalB, argDefault))
showArgs ('david', 'jonathan')
showArgs (argPositionnalB='david', argDefault=3, argPositionnalA='jonathan')
def listMethodsSort_va (itemA, itemO):
voyelles = 'aeiouy'
if itemA in voyelles and itemO in voyelles: return itemA > itemO
elif itemA not in voyelles and itemO not in voyelles: return itemA > itemO
elif itemA in voyelles and itemO not in voyelles: return True
else: return False
def listMethodsSort (itemA, itemO):
voyelles = 'aeiouy'
if itemA == itemO: return 0
if itemA in voyelles and itemO in voyelles:
if itemA > itemO: return 1 # itemA est après itemO
else: return -1
elif itemA not in voyelles and itemO not in voyelles:
if itemA > itemO: return 1
else: return -1
elif itemA in voyelles and itemO not in voyelles: return -1
else: return 1
def listMethods():
from functools import cmp_to_key
myList =[ 'a', 'b', 'c', 'd' ]
b= myList.pop (1)
myList.remove ('c')
myList.append ('e')
myList.extend ([ 'i', 'j', 'k' ])
myList = myList + [ 'f', 'g', 'h' ]
myList.sort()
myList.insert (2, 'l')
print (myList)
myList = sorted (myList, key=cmp_to_key (listMethodsSort))
print (myList)
yList =[]
for l in myList: yList.append (l+'y')
print (yList)
zList =[ l+ 'z' for l in myList ]
print (zList)
for (a,b) in yList: print (a, ':', b)
def stringMethods():
# template avec paramètres nommés
urlRef = 'https://www.euronews.com/%(year)d/%(month)02d/%(day)02d'
urlFull = urlRef % { 'year': 2025, 'month': 1, 'day': 6, 'page': 2 }
# le formattage
goutRef = "j'aime le {} au {} depuis mes {} ans"
goutFull = goutRef.format ('gateau', 'chocolat', 3)
print (goutFull)
colorRef = "le {1} est ma couleur préférée depuis mes {0:02d} ans"
colorFull = colorRef.format (8, 'violet')
print (colorFull)
musiqueRef = "j'aime le {styleMusic}, surtout le groupe {groupeMusic}"
musiqueFull = musiqueRef.format (groupeMusic='kyo', styleMusic='rock')
print (musiqueFull)
ClassReference = list # nécessaire pour faire tourner le scipt. ClassReference peut-être n'importe quelle classe pré-existente
class Custom (ClassReference):
# https://realpython.com/operator-function-overloading/#the-internals-of-operations-like-len-and
def __init__(self, parameter):
# custom = Custom (myParameter)
self.field = parameter
def __str__(self):
# print (custom)
return "parameter: "+ parameter
def __repr__(self):
# déconseillé. peut être utilisé à la place de str.
return "objet Custom (Reference). parameter = "+ self.parameter
def __len__(self):
# len (custom)
return parameter.__len__()
def __add__ (self, itemStr):
# newCustom = self + "grenouille"
# aussi sub -, mul *, pow **, abs abs(nb),
newCustom = Custom (self.parameter +" "+ itemStr)
return newCustom
def __radd__ (self, itemStr):
# newCustom = "grenouille" + self
# aussi rsub, rmul
newCustom = Custom (itemStr +" "+ self.parameter)
return newCustom
def __iadd__ (self, itemStr):
# custom += "grenouille"
# aussi isub
self.parameter +" "+ itemStr
return self
def __bool__(self):
# if custom: do code
if self.parameter: return True
else: return False
def __eq__(self, itemCustom):
# custom == itemCustom
# également ne !=, gt >, lt <, ge >=, le <=
if self.parameter == itemCustom.parameter: return True
else: return False
"""
def __or__ (self, item):
# également not
"""
def __setitem__ (self, pos, item):
# custom[2] = 'a'
if type (pos) == int:
if pos <0: pos += self.length
if pos < self.length: self.list[pos] = item
else: self.append (item)
elif type (pos) == slice:
posIndex = pos.indices (self.length)
rangeList = range (posIndex [0], posIndex [1], posIndex [2])
if type (item) in (tuple, list) and len (item) >= len (rangeList):
i=0
for l in rangeList:
self.list[l] = item[i]
i+=1
def __getitem__ (self, pos):
# print (custom[2])
if type (pos) == int:
while pos <0: pos += self.length
while pos >= self.length: pos -= self.length
return self.list [pos]
elif type (pos) == slice:
posIndex = pos.indices (self.length)
rangeList = self.range (posIndex [0], posIndex [1], posIndex [2])
newList =[]
for l in rangeList: newList.append (self.list[l])
return newList
else: return None