Professional Documents
Culture Documents
Mementopython3-V1 2 2-English PDF
Mementopython3-V1 2 2-English PDF
len(c)→ items count Operations on containers frequently used in Generator of int sequences
min(c) max(c) sum(c) Note: For dictionaries and set, these for iterative loops default 0 not included
sorted(c) → sorted copy operations use keys. range([start,]stop [,step])
val in c → boolean, membersihp operator in (absence not in) range(5) 0 1 2 3 4
enumerate(c)→ iterator on (index,value)
Special for sequence containeurs (lists, tuples, strings) :
range(3,8) 3 4 5 6 7
reversed(c)→ reverse iterator c*5 → duplicate c+c2 → concatenate range(2,12,3) 2 5 8 11
c.index(val) → position c.count(val)→ events count range returns a « generator », converts it to list to see
the values, example:
☝ modify original list Operations on lists print(list(range(4)))
lst.append(item) add item at end
lst.extend(seq) add sequence of items at end
lst.insert(idx,val) insert item at index
function name (identifier) Function definition
lst.remove(val) remove first item with value named parameters
lst.pop(idx) remove item at index and return its value def fctname(p_x,p_y,p_z):
lst.sort() lst.reverse() sort / reverse list in place
"""documentation"""
Operations on dictionaries Operations on sets # statements block, res computation, etc.
d[key]=value d.clear() Operators: return res result value of the call.
| → union (vertical bar char) if no computed result to
d[key]→value del d[clé] & → intersection
☝ parameters and all of this bloc
only exist in the block and during return: return None
d.update(d2) update/add - ^ → difference/symetric diff
d.keys() associations the function call ("black box")
< <= > >= → inclusion relations
d.values() views on keys, values s.update(s2) Function call
d.items() associations s.add(key) s.remove(key) r = fctname(3,i+2,2*i)
d.pop(clé) s.discard(key) one argument per parameter
retrieve returned result (if necessary)
storing data on disk, and reading it back Files
f = open("fil.txt","w",encoding="utf8") Strings formating
formating directives values to format
file variable name of file opening mode encoding of "model {} {} {}".format(x,y,r) str
for operations on disk ◽ 'r' read chars for text "{selection:formating!conversion}"
(+path…) ◽ 'w' write files: ◽ Selection : "{:+2.3f}".format(45.7273)
◽ 'a' append… utf8 ascii 2 →'+45.727'
Examples