Python Crash Course

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:

  • Data types
    • Numbers
    • Strings
    • Printing
    • Lists
    • Dictionaries
    • Booleans
    • Tuples
    • Sets
  • Comparison Operators
  • if,elif, else Statements
  • for Loops
  • while Loops
  • range()
  • list comprehension
  • functions
  • lambda expressions
  • map and filter
  • methods

Data types

Numbers


In [1]:
1 + 1


Out[1]:
2

In [2]:
1 * 3


Out[2]:
3

In [3]:
1 / 2


Out[3]:
0.5

In [4]:
2 ** 4


Out[4]:
16

In [5]:
4 % 2


Out[5]:
0

In [6]:
5 % 2


Out[6]:
1

In [7]:
(2 + 3) * (5 + 5)


Out[7]:
50

Variable Assignment


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]:
5

Strings


In [12]:
'single quotes'


Out[12]:
'single quotes'

In [13]:
"double quotes"


Out[13]:
'double quotes'

In [14]:
" wrap lot's of other quotes"


Out[14]:
" wrap lot's of other quotes"

Printing


In [15]:
x = 'hello'

In [16]:
x


Out[16]:
'hello'

In [17]:
print(x)


hello

In [18]:
num = 12
name = 'Sam'

In [19]:
print('My number is: {one}, and my name is: {two}'.format(one = num,
                                                          two = name))


My number is: 12, and my name is: Sam

In [20]:
print('My number is: {}, and my name is: {}'.format(num,
                                                    name))


My number is: 12, and my name is: Sam

Lists


In [21]:
[1, 2, 3]


Out[21]:
[1, 2, 3]

In [22]:
['hi', 1, [1, 2]]


Out[22]:
['hi', 1, [1, 2]]

In [23]:
my_list = ['a', 'b', 'c']

In [24]:
my_list.append('d')

In [25]:
my_list


Out[25]:
['a', 'b', 'c', 'd']

In [26]:
my_list[0]


Out[26]:
'a'

In [27]:
my_list[1]


Out[27]:
'b'

In [28]:
my_list[1:]


Out[28]:
['b', 'c', 'd']

In [29]:
my_list[:1]


Out[29]:
['a']

In [30]:
my_list[0] = 'NEW'

In [31]:
my_list


Out[31]:
['NEW', 'b', 'c', 'd']

In [32]:
nest = [1, 2, 3, [4, 5, ['target']]]

In [33]:
nest[3]


Out[33]:
[4, 5, ['target']]

In [34]:
nest[3][2]


Out[34]:
['target']

In [35]:
nest[3][2][0]


Out[35]:
'target'

Dictionaries


In [36]:
d = {'key1': 'item1',
     'key2': 'item2'}

In [37]:
d


Out[37]:
{'key1': 'item1', 'key2': 'item2'}

In [38]:
d['key1']


Out[38]:
'item1'

Booleans


In [39]:
True


Out[39]:
True

In [40]:
False


Out[40]:
False

Tuples


In [41]:
t = (1, 2, 3)

In [42]:
t[0]


Out[42]:
1

In [43]:
# t[0] = 'NEW'
# It would result in an error:'TypeError: 'tuple' object does not support item assignment'

Sets


In [44]:
{1, 2, 3}


Out[44]:
{1, 2, 3}

In [45]:
{1, 2, 3, 1, 2, 1, 2, 3, 3, 3, 3, 2, 2, 2, 1, 1, 2}


Out[45]:
{1, 2, 3}

Comparison Operators


In [46]:
1 > 2


Out[46]:
False

In [47]:
1 < 2


Out[47]:
True

In [48]:
1 >= 1


Out[48]:
True

In [49]:
1 <= 4


Out[49]:
True

In [50]:
1 == 1


Out[50]:
True

In [51]:
'hi' == 'bye'


Out[51]:
False

Logic Operators


In [52]:
(1 > 2) and (2 < 3)


Out[52]:
False

In [53]:
(1 > 2) or (2 < 3)


Out[53]:
True

In [54]:
(1 == 2) or (2 == 3) or (4 == 4)


Out[54]:
True

if,elif, else Statements


In [55]:
if 1 < 2:
    print('Yep!')


Yep!

In [56]:
if 1 < 2:
    print('yep!')


yep!

In [57]:
if 1 < 2:
    print('first')
else:
    print('last')


first

In [58]:
if 1 > 2:
    print('first')
else:
    print('last')


last

In [59]:
if 1 == 2:
    print('first')
elif 3 == 3:
    print('middle')
else:
    print('Last')


middle

for Loops


In [60]:
seq = [1, 2, 3, 4, 5]

In [61]:
for item in seq:
    print(item)


1
2
3
4
5

In [62]:
for item in seq:
    print('Yep')


Yep
Yep
Yep
Yep
Yep

In [63]:
for jelly in seq:
    print(jelly+jelly)


2
4
6
8
10

while Loops


In [64]:
i = 1
while i < 5:
    print('i is: {}'.format(i))
    i = i+1


i is: 1
i is: 2
i is: 3
i is: 4

range()


In [65]:
range(5)


Out[65]:
range(0, 5)

In [66]:
for i in range(5):
    print(i)


0
1
2
3
4

In [67]:
list(range(5))


Out[67]:
[0, 1, 2, 3, 4]

list comprehension


In [68]:
x = [1, 2, 3, 4]

In [69]:
out = []
for item in x:
    out.append(item ** 2)
print(out)


[1, 4, 9, 16]

In [70]:
[item ** 2 for item in x]


Out[70]:
[1, 4, 9, 16]

functions


In [71]:
def my_func(param1 = 'default'):
    """
    Docstring goes here.
    """
    print(param1)

In [72]:
my_func


Out[72]:
<function __main__.my_func>

In [73]:
my_func()


default

In [74]:
my_func('new param')


new param

In [75]:
my_func(param1 = 'new param')


new param

In [76]:
def square(x):
    return x ** 2

In [77]:
out = square(2)

In [78]:
print(out)


4

lambda expressions


In [79]:
def times2(var):
    return var*2

In [80]:
times2(2)


Out[80]:
4

In [81]:
lambda var: var * 2


Out[81]:
<function __main__.<lambda>>

map and filter


In [82]:
seq = [1, 2, 3, 4, 5]

In [83]:
map(times2,seq)


Out[83]:
<map at 0x7f3d420df668>

In [84]:
list(map(times2,seq))


Out[84]:
[2, 4, 6, 8, 10]

In [85]:
list(map(lambda var: var * 2, seq))


Out[85]:
[2, 4, 6, 8, 10]

In [86]:
filter(lambda item: item % 2 == 0, seq)


Out[86]:
<filter at 0x7f3d420ec9e8>

In [87]:
list(filter(lambda item: item % 2 == 0, seq))


Out[87]:
[2, 4]

methods


In [88]:
st = 'hello my name is Sam'

In [89]:
st.lower()


Out[89]:
'hello my name is sam'

In [90]:
st.upper()


Out[90]:
'HELLO MY NAME IS SAM'

In [91]:
st.split()


Out[91]:
['hello', 'my', 'name', 'is', 'Sam']

In [92]:
tweet = 'Go Sports! #Sports'

In [93]:
tweet.split('#')


Out[93]:
['Go Sports! ', 'Sports']

In [94]:
tweet.split('#')[1]


Out[94]:
'Sports'

In [95]:
d


Out[95]:
{'key1': 'item1', 'key2': 'item2'}

In [96]:
d.keys()


Out[96]:
dict_keys(['key2', 'key1'])

In [97]:
d.items()


Out[97]:
dict_items([('key2', 'item2'), ('key1', 'item1')])

In [98]:
lst = [1,2,3]

In [99]:
lst.pop()


Out[99]:
3

The pop() method takes a single argument (index) and removes the element present at that index from the list.


In [100]:
lst


Out[100]:
[1, 2]

In [101]:
lst2 = [1, 2, 3, 4]
lst2.pop(0)


Out[101]:
1

In [102]:
lst2


Out[102]:
[2, 3, 4]

In [103]:
'x' in [1, 2, 3]


Out[103]:
False

In [104]:
'x' in ['x', 'y', 'z']


Out[104]:
True

Great Job!