Kernel: Python 3
Datastructures
mutable (elements can change over time)
immutable (elements cannot change)
Category:
FIFO (First in First out)
LIFO (Last in First out)
Type:
list is mutable
tuple is immutable
dictionary is mutable
set is mutable
In [1]:
Out[1]:
['__add__',
'__class__',
'__contains__',
'__delattr__',
'__delitem__',
'__dir__',
'__doc__',
'__eq__',
'__format__',
'__ge__',
'__getattribute__',
'__getitem__',
'__gt__',
'__hash__',
'__iadd__',
'__imul__',
'__init__',
'__init_subclass__',
'__iter__',
'__le__',
'__len__',
'__lt__',
'__mul__',
'__ne__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__reversed__',
'__rmul__',
'__setattr__',
'__setitem__',
'__sizeof__',
'__str__',
'__subclasshook__',
'append',
'clear',
'copy',
'count',
'extend',
'index',
'insert',
'pop',
'remove',
'reverse',
'sort']
Appending
In [8]:
Out[8]:
[2, 3, 4, 5, 6, 6]
Tuples
In [7]:
Out[7]:
(1, 2, 3, 4, 5)
In [9]:
Out[9]:
0
In [10]:
Out[10]:
2
In [12]:
Out[12]:
{'a': 1, 'b': 2, 'c': 3}
In [13]:
Out[13]:
2
In [14]:
Out[14]:
dict_values([1, 2, 3])
In [15]:
Out[15]:
dict_keys(['a', 'b', 'c'])
For Loop
In [16]:
Out[16]:
a 1
b 2
c 3
In [18]:
Out[18]:
{'a': [1, 2, 3], 'b': [4, 5, 6], 'c': [7, 8, 9]}
In [19]:
Out[19]:
[{'a': 1}, {'b': 2}, {'c': 3}]
In [20]:
Out[20]:
{1, 2, 3, 4, 5}
In [21]:
Out[21]:
{1, 2, 3, 4, 5, 6}
In [26]:
Out[26]:
(5, {6})
String functions
-upper()
-lower()
-capitalize()
-split()
-join()
-len()
Character functions
-chr()
-ord()
Upper case conversion
In [35]:
Out[35]:
'PCEP EXAM IS COOL TO HAVE'
In [29]:
Out[29]:
'pcEp exAm is Cool tO haVe'
Lower case conversion
In [30]:
Out[30]:
'pcep exam is cool to have'
Capitalize first letter conversion
In [31]:
Out[31]:
'Pcep exam is cool to have'
Splitting with a 'character/symbol'
In [36]:
Out[36]:
['pcEp', 'exAm', 'is', 'Cool', 'tO', 'haVe']
In [37]:
Out[37]:
['', 'cE', ' exAm is Cool tO haVe']
In [40]:
In [41]:
Out[41]:
'pcEp exAm is Cool tO haVe-This is another string'
In [50]:
Out[50]:
25
In [51]:
In [52]:
Out[52]:
97
In [53]:
Out[53]:
99
In [54]:
Out[54]:
'p'
In [55]:
Out[55]:
't'
Function and Generators
Function -> serious memory impact
Generators -> they minimize memory impact
Results:
return -> give back the processes results
yield -> give back one value at a time
Par and arg:
arg -> position and order matters
par -> like keyword arguments
In [62]:
Out[62]:
[0, 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]
In [63]:
Out[63]:
<generator object generate at 0x7ff97003fa50>
In [64]:
Out[64]:
None
In [66]:
Out[66]:
This is whatever
True
In [67]:
Out[67]:
6
In [69]:
In [70]:
In [71]:
Out[71]:
(1,)
{'c': 11}
In [76]:
In [77]:
Out[77]:
1
2
In [78]:
Out[78]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [79]:
Out[79]:
11
Fibonacci Series
In [80]:
Out[80]:
55
Exceptions
In [81]:
Out[81]:
An exception has occurred, use %tb to see the full traceback.
SystemExit
/Users/smhuda/opt/anaconda3/lib/python3.8/site-packages/IPython/core/interactiveshell.py:3445: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.
warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)
In [82]:
Out[82]:
This was a value error
In [83]:
Out[83]:
This never ecxecutes