Please note, this is not meant to be a comprehensive overview of Python or programming in general, if you have no programming experience, you should probably take my other course: Complete Python Bootcamp instead.
This notebook is just a code reference for the videos, no written explanations here
This notebook will just go through the basic topics in order:
In [1]:
1 + 1
Out[1]:
In [2]:
1 * 3
Out[2]:
In [3]:
1 / 2
Out[3]:
In [4]:
2 ** 4
Out[4]:
In [5]:
4 % 2
Out[5]:
In [6]:
5 % 2
Out[6]:
In [7]:
(2 + 3) * (5 + 5)
Out[7]:
In [8]:
# Can not start with number or special characters
name_of_var = 2
In [9]:
x = 2
y = 3
In [10]:
z = x + y
In [11]:
z
Out[11]:
In [12]:
'single quotes'
Out[12]:
In [13]:
"double quotes"
Out[13]:
In [14]:
" wrap lot's of other quotes"
Out[14]:
In [15]:
x = 'hello'
In [16]:
x
Out[16]:
In [17]:
print(x)
In [18]:
num = 12
name = 'Sam'
In [19]:
print('My number is: {one}, and my name is: {two}'.format(one = num,
two = name))
In [20]:
print('My number is: {}, and my name is: {}'.format(num,
name))
In [21]:
[1, 2, 3]
Out[21]:
In [22]:
['hi', 1, [1, 2]]
Out[22]:
In [23]:
my_list = ['a', 'b', 'c']
In [24]:
my_list.append('d')
In [25]:
my_list
Out[25]:
In [26]:
my_list[0]
Out[26]:
In [27]:
my_list[1]
Out[27]:
In [28]:
my_list[1:]
Out[28]:
In [29]:
my_list[:1]
Out[29]:
In [30]:
my_list[0] = 'NEW'
In [31]:
my_list
Out[31]:
In [32]:
nest = [1, 2, 3, [4, 5, ['target']]]
In [33]:
nest[3]
Out[33]:
In [34]:
nest[3][2]
Out[34]:
In [35]:
nest[3][2][0]
Out[35]:
In [36]:
d = {'key1': 'item1',
'key2': 'item2'}
In [37]:
d
Out[37]:
In [38]:
d['key1']
Out[38]:
In [39]:
True
Out[39]:
In [40]:
False
Out[40]:
In [41]:
t = (1, 2, 3)
In [42]:
t[0]
Out[42]:
In [43]:
# t[0] = 'NEW'
# It would result in an error:'TypeError: 'tuple' object does not support item assignment'
In [44]:
{1, 2, 3}
Out[44]:
In [45]:
{1, 2, 3, 1, 2, 1, 2, 3, 3, 3, 3, 2, 2, 2, 1, 1, 2}
Out[45]:
In [46]:
1 > 2
Out[46]:
In [47]:
1 < 2
Out[47]:
In [48]:
1 >= 1
Out[48]:
In [49]:
1 <= 4
Out[49]:
In [50]:
1 == 1
Out[50]:
In [51]:
'hi' == 'bye'
Out[51]:
In [52]:
(1 > 2) and (2 < 3)
Out[52]:
In [53]:
(1 > 2) or (2 < 3)
Out[53]:
In [54]:
(1 == 2) or (2 == 3) or (4 == 4)
Out[54]:
In [55]:
if 1 < 2:
print('Yep!')
In [56]:
if 1 < 2:
print('yep!')
In [57]:
if 1 < 2:
print('first')
else:
print('last')
In [58]:
if 1 > 2:
print('first')
else:
print('last')
In [59]:
if 1 == 2:
print('first')
elif 3 == 3:
print('middle')
else:
print('Last')
In [60]:
seq = [1, 2, 3, 4, 5]
In [61]:
for item in seq:
print(item)
In [62]:
for item in seq:
print('Yep')
In [63]:
for jelly in seq:
print(jelly+jelly)
In [64]:
i = 1
while i < 5:
print('i is: {}'.format(i))
i = i+1
In [65]:
range(5)
Out[65]:
In [66]:
for i in range(5):
print(i)
In [67]:
list(range(5))
Out[67]:
In [68]:
x = [1, 2, 3, 4]
In [69]:
out = []
for item in x:
out.append(item ** 2)
print(out)
In [70]:
[item ** 2 for item in x]
Out[70]:
In [71]:
def my_func(param1 = 'default'):
"""
Docstring goes here.
"""
print(param1)
In [72]:
my_func
Out[72]:
In [73]:
my_func()
In [74]:
my_func('new param')
In [75]:
my_func(param1 = 'new param')
In [76]:
def square(x):
return x ** 2
In [77]:
out = square(2)
In [78]:
print(out)
In [79]:
def times2(var):
return var*2
In [80]:
times2(2)
Out[80]:
In [81]:
lambda var: var * 2
Out[81]:
In [82]:
seq = [1, 2, 3, 4, 5]
In [83]:
map(times2,seq)
Out[83]:
In [84]:
list(map(times2,seq))
Out[84]:
In [85]:
list(map(lambda var: var * 2, seq))
Out[85]:
In [86]:
filter(lambda item: item % 2 == 0, seq)
Out[86]:
In [87]:
list(filter(lambda item: item % 2 == 0, seq))
Out[87]:
In [88]:
st = 'hello my name is Sam'
In [89]:
st.lower()
Out[89]:
In [90]:
st.upper()
Out[90]:
In [91]:
st.split()
Out[91]:
In [92]:
tweet = 'Go Sports! #Sports'
In [93]:
tweet.split('#')
Out[93]:
In [94]:
tweet.split('#')[1]
Out[94]:
In [95]:
d
Out[95]:
In [96]:
d.keys()
Out[96]:
In [97]:
d.items()
Out[97]:
In [98]:
lst = [1,2,3]
In [99]:
lst.pop()
Out[99]:
The pop() method takes a single argument (index) and removes the element present at that index from the list.
In [100]:
lst
Out[100]:
In [101]:
lst2 = [1, 2, 3, 4]
lst2.pop(0)
Out[101]:
In [102]:
lst2
Out[102]:
In [103]:
'x' in [1, 2, 3]
Out[103]:
In [104]:
'x' in ['x', 'y', 'z']
Out[104]: