4.10

Dictionaries


In [1]:
d = {'key1':'major', 'key2':123}

In [2]:
d['key1']


Out[2]:
'major'

In [6]:
d = {'k1':['major','key','bruhh']}

In [5]:
d['k1'][1]


Out[5]:
'key'

Booleans


In [7]:
True


Out[7]:
True

In [8]:
False


Out[8]:
False

Tuples and sets


In [10]:
my_list = [1,2,3] # this is a list. items are not unique and they can be reassigned.

In [12]:
my_list[1] # grabbing an item using indexing


Out[12]:
2

In [14]:
tup = (1,2,3) # this is a tuple. items are immutable.

In [19]:
tup[0] = 4


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-19-945dcde76e56> in <module>()
----> 1 tup[0] = 4

TypeError: 'tuple' object does not support item assignment

In [16]:
{1,2,3} #this is a set


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

In [18]:
{1,2,3,3,2,1} # set items are unique


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

In [28]:
s = set([1,2,1,2,1,2,2,2,2,2,2]) # converting a list to a set gets rid of duplicates
s.add(5)
s


Out[28]:
{1, 2, 5}

In [29]:
s.add(5) # adding an existing value won't change the set
s


Out[29]:
{1, 2, 5}

Operators

Comparison operators


In [30]:
1 > 2


Out[30]:
False

In [32]:
1 < 2


Out[32]:
True

In [33]:
1 >= 2


Out[33]:
False

In [34]:
2 <= 2


Out[34]:
True

In [35]:
1 == 1


Out[35]:
True

In [36]:
1 == 2


Out[36]:
False

In [37]:
1 != 2


Out[37]:
True

In [38]:
'hello' != 'goodbye'


Out[38]:
True

Logic operators


In [44]:
(1 < 2) and (2 > 3) # one operator being false will make an entire statement false


Out[44]:
False

In [46]:
(1 < 2) or (2 > 3) or (1 == 1) # one operator being true will make an entire statement true


Out[46]:
True

In [48]:
True and True


Out[48]:
True

In [47]:
True and False


Out[47]:
False

In [49]:
True or False


Out[49]:
True

Conditional statements


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


yep

In [51]:
if True:
    x = 2 + 2

In [52]:
x


Out[52]:
4

In [57]:
if (1==2):
    print('yep')
elif (2==2):
    print('dang')
elif (3==3):
    print('wooop')
else:
    print('nah, son')


dang

4.11

Loops

for


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

In [64]:
for item in seq:
    print('hello {}'.format(item))


hello 1
hello 2
hello 3
hello 4
hello 5

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


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

In [70]:
for x in range(0,5): # range is a generator. lists are different
    print(x)


0
1
2
3
4

In [72]:
list(range(10))


Out[72]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

List comprehension

Many lines:


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

In [75]:
out = []

for num in x:
    out.append(num**2)

In [76]:
print(out)


[1, 4, 9, 16]

One line!


In [79]:
out = [num**2 for num in x]

In [80]:
out


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

Functions


In [83]:
def the_func(param):
    print(param)
the_func('if I give you the func, you gon take it?')


if I give you the func, you gon take it?

In [87]:
def the_func(name='kendrick'): # setting a default
    print('hello world, {} here'.format(name)) # contents  of function

In [88]:
the_func() # runs the function and lets input default to something


hello world, kendrick here

In [91]:
the_func('pop') # overrides default


hello world, pop here

In [90]:
the_func # returns function object


Out[90]:
<function __main__.the_func>

In [102]:
def square(inp):
    """
    This is a docstring.
    Docstrings can be multiple lines, and can be called in jupyter with Shift-Tab.
    This function squares the input.
    """
    return inp**2

In [95]:
output = square(2)

In [97]:
output


Out[97]:
4

4.12

Standard functions


In [104]:
def times2(num):
    return num * 2

In [106]:
times2(3)


Out[106]:
6

In [108]:
# map()

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

In [113]:
list(map(times2,seq)) # << This is where lambda comes in. No need to define an entire function.


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

Lambda functions


In [123]:
def times2(num):return num * 2 # sike, it can actually be written as a one liner

In [115]:
times2(5)


Out[115]:
10

In [119]:
t = lambda var:var*2 # sike, it can actually be written as a shorter one liner

In [118]:
t(2)


Out[118]:
4

In [121]:
list(map(lambda var:var*2,seq)) # sike, it can actually all be combined tho


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

Filter functions


In [125]:
list(filter(lambda var:var%2 == 0,seq)) # oh shiiiiiiiit


Out[125]:
[2, 4]

Methods

Strings


In [126]:
s = 'Sup my name is Ian'

In [129]:
s.lower()


Out[129]:
'sup my name is ian'

In [130]:
s.upper()


Out[130]:
'SUP MY NAME IS IAN'

In [131]:
s.title()


Out[131]:
'Sup My Name Is Ian'

In [132]:
s.split()


Out[132]:
['Sup', 'my', 'name', 'is', 'Ian']

In [134]:
tweet = 'Go Sports! #Sprots'

In [135]:
tweet.split()


Out[135]:
['Go', 'Sports!', '#Sprots']

Dicts


In [136]:
d = {'k1':1,'k2':2}

In [137]:
d


Out[137]:
{'k1': 1, 'k2': 2}

In [138]:
d.keys()


Out[138]:
['k2', 'k1']

In [139]:
d.items()


Out[139]:
[('k2', 2), ('k1', 1)]

In [140]:
d.values()


Out[140]:
[2, 1]

Lists


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

In [142]:
lst.pop()


Out[142]:
3

In [143]:
lst


Out[143]:
[1, 2]

In [146]:
lst = [1,2,3,4,5]
item = lst.pop()

In [147]:
item


Out[147]:
5

In [148]:
lst


Out[148]:
[1, 2, 3, 4]

In [149]:
first = lst.pop(0)

In [150]:
lst


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

In [151]:
first


Out[151]:
1

In [152]:
lst.append('new')

In [153]:
lst


Out[153]:
[2, 3, 4, 'new']

the 'in' operator


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


Out[155]:
False

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


Out[157]:
True

Tuple unpacking


In [158]:
x = [(1,2),(3,4),(5,6)]

In [161]:
x[0][1]


Out[161]:
2

In [166]:
for a,b in x:
    print(a)
    print(b)


1
2
3
4
5
6

In [ ]: