Show Menu
Cheatography

datastructure Cheat Sheet (DRAFT) by

000000000000000000000000000000

This is a draft cheat sheet. It is a work in progress and is not finished yet.

tuple

A tuple is a collection which is ordered and unchangeable

tuple = ("apple", "banana", "cherry", "orange", "kiwi", "melon", "mango")
print(thistuple)
print(thistuple[1])             =>"banana"               
print(thistuple[-1])           =>"mango"
print(thistuple[2:5])         =>"cherry ... kiwi"   
print(thistuple[-4:-1]) =>('orange', 'kiwi', 'melon')  
---------------------convert-------------------
x = ("apple", "banana", "cherry")
y = list(x)
y[1] = "kiwi"
x = tuple(y)
---------------------loop------------------
thistuple = ("apple", "banana", "cherry")
for x in thistuple:
  print(x)
---------------------test------------------
thistuple = ("apple", "banana", "cherry")
if "apple" in thistuple:
  print("Yes, 'apple' is in the fruits tuple")
----------------------len----------------------
thistuple = ("apple", "banana", "cherry")
print(len(thistuple))
----------------------del---------------------
thistuple = ("apple", "banana", "cherry")
del thistuple
----------------------jointuples--------------
tuple1 = ("a", "b" , "c")
tuple2 = (1, 2, 3)
tuple3 = tuple1 + tuple2
-----------------------------------------------------
thistuple = (1, 3, 7, 8, 7, 5, 4, 6, 8, 5)
x = thistuple.count(5) =>nb of repetiton of 5
x = thistuple.index(8)=>3 is the first location of 8
 

sets

A set is a collection which is unordered and unindexed
thisset = {"apple", "banana", "cherry"}
print(thisset)
---------------------------------------------
for x in thisset:
  print(x)
-------------------check---------------------------
print("banana" in thisset)=>true or false
---------------------add-------------------------
thisset.add("orange")
thisset.update(["orange", "mango", "grapes"])=>add more than one
-----------------len------------------------
print(len(thisset))
--------------remove----------------------
thisset.remove("banana")
or
thisset.discard("banana")
---------------pop-------------------------
x = thisset.pop()=>remove the last elet
---------------clear-----------------------
thisset.clear()=> empty the set
----------------del-------------------------
del thisset
---------------joinset---------------------
set1 = {"a", "b" , "c"}
set2 = {1, 2, 3}
set3 = set1.union(set2)
-------------update----------------------
set1 = {"a", "b" , "c"}
set2 = {1, 2, 3}
set1.update(set2)=>add set2 to set1