

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Python is an interpreted, object-oriented, high-level programming language with dynamic semantics developed by Guido van Rossum. It was originally released in 1991. Designed to be easy as well as fun, the name "Python" is a nod to the British comedy group Monty Python.
Typology: Cheat Sheet
1 / 2
This page cannot be seen from the preview
Don't miss anything!


Ordered sequence of values indexed by integer numbers. Tuples are
immutable
Synatx:
len(myListOrTuple)
Syntax: Lists: myList = []
It is an unordered set of key value pairs
Syntax : myDict = {}
It is a way of organizing data that contains the items stored
and their relationship to each other
The areas in which Data Structures are applied:
Tuples: myTuple = ()
list/tuple: Syntax: "x" in
myListOrTuple
Syntax:
myListOrTuple.index("x") -
ValueError exception
list/tuple:
Syntax:
myListOrTuple.count("x")
Syntax: Lists: del
myList[x] Tuples: tuples
are immutable!
Lists: myList1 + myList
Tuples: myTuple1 +
myTuple
Concatenating a List and a
Tuple will produce a
TypeError exception
list/tuple Syntax: Lists:
myList.insert(x,
Syntax : myDict["k"] = value
Syntax : myDict["k"] = newValue
Syntax : myDict["k"] -- If the key is
not present, a KeyError is raised
Syntax : "k" in myDict
Syntax : myDict.keys()
System
Package
Data structures can be
used in the following
areas:
Array of structure)
model: Graph
model: Trees
Syntax: Lists: myList[x] =
"x“
Tuples: tuples are
immutable!
list/tuple:
Syntax: Lists: del myList[x]
Tuples: tuples are
"value")
Tuples: tuples are
immutable!
Syntax: Lists:
myList.append("x") Tuples:
tuples are immutable!
Syntax: List to Tuple:
tuple(myList) Tuple to List:
list(myTuple)
Syntax : len(myDict)
Syntax : del myDict["k"]
Syntax : myDict.clear()
Data Structures
Primitive Data
Structures:
Non- Primitive Data Structures:
†
- Uniform hashing assumption
whole numbers from negative infinity to infinity. Eg: 4, 5, -1 etc
In python it
be of the same data type.
Syntax of writing an array in
python: import array as arr
a = arr.array("I",[3,6,9])
It is an unordered collection with no duplicate elements. It supports mathematical
operations like union, intersection, difference and symmetric difference.
Primitive
Non -
Primitive
can be created by using a pair of single or double quotes for
the sequence.
Eg: x = 'Cake’
y = '’Cookie’’
type(a)
heterogeneous items. It is described using the square brackets []
and hold elements separated by comma
Eg: x = [] # Empty list
set:
Syntax : mySet =
set()
Syntax :
Method 1 :
mySet1.union(mySet2)
Method 2 : mySet1 | mySet
Integer Float String (^) Boolean
Certain operations can be performed on
a string:
type(x)
o The list can be classified into linear and non-linear
data structures
Syntax : mySet =
set(element1, element2...)
sets
Array List
Tuple
Dictiona
ry
Set File
o We can use * to repeat the
string for a specific number of
times. Eg: x*
o String can be sliced, that is to
select parts of the string.
Eg: Coke
z1 = x[2:]
print(
z1) # Slicing
z2 = y[0] + y[1]
print(z2) Output: ke
Co o To capitalize the strings
Eg: str.capitalize('cookie')
o To retrieve the length of the strings
Eg:
str1 = "Cake 4 U" str2 = "404" len(str1)
o To replace parts of a string with another string
Algorithm Best case
Average
case
Worst case Remarks
Selecti
on
sort
½ n
2 ½ n
2 ½ n
2
n exchanges,
quadratic is the best case
Insertion
sort
n ¼ n
2 ½ n
2
Used for small or partial-
sorted arrays
Bubb
le
sort
n ½ n
2 ½ n
2
Rarely useful,
Insertion sort can be
used instead
Shell sort n log 3 n unknown (^) c n 3/
Tight code,
Sub quadratic
Merge
sort
½ n lg n n lg n n lg n
n log n guarantee;
stable
Quick sort n lg n 2 n ln n ½ n
2
n log n probabilistic
guarantee;
fastest in practice
Heap sort n
† 2 n lg n 2 n lg n
n log n guarantee;
in place
Worst Case Average Case
Data Structure Search Insert Delete Search Insert Delete
Sequential
search
n n n n n n
Binary search log n n n log n n n
Binary search
tree
n n n log n log n sqrt(n)
Red-black BST log n log n log n log n log n log n
Hash table n n n 1
† 1
† 1
†
o Eg: str1.replace('
U', str2)
o Linear data
structures contain
Stacks and
queues
o Non-linear data
structures
contains Graphs
and Trees
objects that can be inserted
or removed according to
LIFO(Last In First Out)
concept. pop() method is
used during disposal in
Python
Eg: stack.pop() #
Bottom -> 1 -> 2 -> 3
-> 4 -> 5 (Top)
s
t
ack.pop() # Bottom -> 1 -> 2 -> 3 -
4 (Top) print(stack)
according to
FIFO(First In First Out) concept.
called nodes, and a finite set of ordered pair (u,v) called edges. It can
be classified as direction and weight
node has at most two children
moderate insertion/ deletion
Syntax : mySet.add("x")
Syntax :
Method 1 :
mySet.remove("x") -- If "x"
is not present, raises a
KeyErorr
Method 2 :
mySet.discard("x") --
Removes the element, if
present
set
Syntax : mySet.clear()
Syntax : "x" in mySet
Syntax :
Method 1 :
mySet1.intersect(mySet2)
Method 2 : mySet1 &
mySet
Syntax :
Method 1 :
mySet1.difference(mySet2)
Method 2 : mySet1 -
mySet
Syntax :
Method 1 :
mySet1.symmetric_differen
ce(m ySet2)
Linear Non - Linear
Stacks Queues^ Graphs Trees
TRUE or FALSE
either MIN or Max
to find them is
hashing
Syntax :
len(mySet)
Method 2 : mySet1 ^
mySet
FURTHERMORE:
Data Structures Certification Training Course