Show Menu
Cheatography

Python_Chap_13 Cheat Sheet by

container : lists, strings, dictionaries, sets, tuplets

Container

A container is a data structure that can store and organize multiple values or objects.
The common types of container : lists, strings, dictio­naries, sets, tuplets
propriétés :
Capacité à supporter le test d'appa­rte­nance, for example, 'to' in 'toto' return True
Capacité à supporter la fonction len() renvoyant la longueur du container.
ordonn­é/o­rdered; indexa­ble­/su­bsc­rip­table; itérab­le/­ite­rable
immuable, identifant id(), hachable (hasable, hashib­ility) hash()

Tuplet

iterable, ordered, indexi­­able, similar to lists but immutable.
Avoid containing mutable variables, such as list, dictionary
(1,2,3)
tuplet()

# create a new tuplet and add element (different id)
set1 = (1, 2, 3)
set1 = set1 + (2,)

# operators
>>> (1, 2) + (3, 4)
(1, 2, 3, 4)
>>> (1, 2) * 4
(1, 2, 1, 2, 1, 2, 1, 2)

# iteration
dic1.i­tems() return a list contains tuplets, each tuplet contains key/value pair
similar to enumer­ate()
>>> for bidule in enumer­ate­([75, -75, 0]):
... print(­bidule, type(b­idule))
...
(0, 75) <class 'tuple­'>
(1, -75) <class 'tuple­'>
(2, 0) <class 'tuple­'>
 

Dictionary

dic = {key1: value1, key2: value2, ...}

# iterable by key 
dic.it­ems() & dic.keys() & dic.va­lues()

# ordered by key or value 
sorted­(dic)  # by key
sorted­(dic, reverse=True)
sorted­(dic, key=di­c.get) # by value
min(dic, key=di­c.get) & max(dic, key=di­c.get)
# return the key with maximun or minimun value  

# get value 
dic[key] & dic.ge­t(key) 
# if the key exist, dic[key] return error 

# modify value / add new key-value pair / remove key  
dic[ke­y] = ­value 
del dic[key] 
dict.p­op(key) 
dic.updata({key: value})

#duplicate a dictionary (same to lists)
dic2 = dic1.copy() 

# transform list of list to dictionary 
dic1 = dir([['a', 1], ['b', 2]])

# list of dictionary & iteration
>>> animaux = [ani1, ani2]
>>> animaux
[{'n': 'g', 'p': 1, 't': 5}, {'n': 's', 'p': 7, 't': 1}]
>>> for ani in animaux:
...     print(ani["n"])
...
girafe
singe
>>>len(animaux)
2 # length of dictionaries in a list
les objets utilisés comme clé doivent être hachables
Si un des sous-é­léments a plus de 2 éléments (ou moins), Python renvoie une erreur
 

Set

iterable, mutable, unordered, indexable, a list without duplicated element
{1,2,3}

# transforme a list to a set
set()

# add the new element at the end
set.add()

# remove element
set.di­scard()
set.re­­move()
# remove() raises an exception if the element is not present, but discard() does not

# add multiple elements to a set
set.up­­data()
# This method takes any iterable object as an argument, such as another set, a list, a tuple, or a dictio­nary. For example, set s = {1, 2, 3}, add {4, 5} and [6, 7] to it by calling s.upda­te({4, 5}, [6, 7]). This will result in s = {1, 2, 3, 4, 5, 6, 7}

# operations of sets
set(list1) & set(list2) # elements
set(list1) | set(list2) # union
set(list1) - set(list2) # difference

Range

range(­­start, stop, step)
step could be negative, for example range(5, 1, -1)
similar to lists, but immuta­ble­/ha­shable

transform range to list
list(r­ange())
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Studying Cheat Sheet
          Python_Chap_4 Cheat Sheet

          More Cheat Sheets by Theo666

          Python_Chap_2 Cheat Sheet
          Python_Chap_4 Cheat Sheet
          Chap_3 Cheat Sheet