In [1]:
d = {'key1':'major', 'key2':123}
In [2]:
d['key1']
Out[2]:
In [6]:
d = {'k1':['major','key','bruhh']}
In [5]:
d['k1'][1]
Out[5]:
In [7]:
True
Out[7]:
In [8]:
False
Out[8]:
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]:
In [14]:
tup = (1,2,3) # this is a tuple. items are immutable.
In [19]:
tup[0] = 4
In [16]:
{1,2,3} #this is a set
Out[16]:
In [18]:
{1,2,3,3,2,1} # set items are unique
Out[18]:
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]:
In [29]:
s.add(5) # adding an existing value won't change the set
s
Out[29]:
Comparison operators
In [30]:
1 > 2
Out[30]:
In [32]:
1 < 2
Out[32]:
In [33]:
1 >= 2
Out[33]:
In [34]:
2 <= 2
Out[34]:
In [35]:
1 == 1
Out[35]:
In [36]:
1 == 2
Out[36]:
In [37]:
1 != 2
Out[37]:
In [38]:
'hello' != 'goodbye'
Out[38]:
Logic operators
In [44]:
(1 < 2) and (2 > 3) # one operator being false will make an entire statement false
Out[44]:
In [46]:
(1 < 2) or (2 > 3) or (1 == 1) # one operator being true will make an entire statement true
Out[46]:
In [48]:
True and True
Out[48]:
In [47]:
True and False
Out[47]:
In [49]:
True or False
Out[49]:
In [50]:
if (1<2):
print('yep')
In [51]:
if True:
x = 2 + 2
In [52]:
x
Out[52]:
In [57]:
if (1==2):
print('yep')
elif (2==2):
print('dang')
elif (3==3):
print('wooop')
else:
print('nah, son')
for
In [59]:
seq = [1,2,3,4,5]
In [64]:
for item in seq:
print('hello {}'.format(item))
In [1]:
i = 0
while (i < 5):
print('i is: {}'.format(i))
i += 1
In [70]:
for x in range(0,5): # range is a generator. lists are different
print(x)
In [72]:
list(range(10))
Out[72]:
Many lines:
In [73]:
x = [1,2,3,4]
In [75]:
out = []
for num in x:
out.append(num**2)
In [76]:
print(out)
One line!
In [79]:
out = [num**2 for num in x]
In [80]:
out
Out[80]:
In [83]:
def the_func(param):
print(param)
the_func('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
In [91]:
the_func('pop') # overrides default
In [90]:
the_func # returns function object
Out[90]:
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]:
In [104]:
def times2(num):
return num * 2
In [106]:
times2(3)
Out[106]:
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]:
In [123]:
def times2(num):return num * 2 # sike, it can actually be written as a one liner
In [115]:
times2(5)
Out[115]:
In [119]:
t = lambda var:var*2 # sike, it can actually be written as a shorter one liner
In [118]:
t(2)
Out[118]:
In [121]:
list(map(lambda var:var*2,seq)) # sike, it can actually all be combined tho
Out[121]:
In [125]:
list(filter(lambda var:var%2 == 0,seq)) # oh shiiiiiiiit
Out[125]:
Strings
In [126]:
s = 'Sup my name is Ian'
In [129]:
s.lower()
Out[129]:
In [130]:
s.upper()
Out[130]:
In [131]:
s.title()
Out[131]:
In [132]:
s.split()
Out[132]:
In [134]:
tweet = 'Go Sports! #Sprots'
In [135]:
tweet.split()
Out[135]:
Dicts
In [136]:
d = {'k1':1,'k2':2}
In [137]:
d
Out[137]:
In [138]:
d.keys()
Out[138]:
In [139]:
d.items()
Out[139]:
In [140]:
d.values()
Out[140]:
Lists
In [154]:
lst = [1,2,3]
In [142]:
lst.pop()
Out[142]:
In [143]:
lst
Out[143]:
In [146]:
lst = [1,2,3,4,5]
item = lst.pop()
In [147]:
item
Out[147]:
In [148]:
lst
Out[148]:
In [149]:
first = lst.pop(0)
In [150]:
lst
Out[150]:
In [151]:
first
Out[151]:
In [152]:
lst.append('new')
In [153]:
lst
Out[153]:
the 'in' operator
In [155]:
'x' in [1,2,3]
Out[155]:
In [157]:
'x' in ['x','y','z']
Out[157]:
In [158]:
x = [(1,2),(3,4),(5,6)]
In [161]:
x[0][1]
Out[161]:
In [166]:
for a,b in x:
print(a)
print(b)
In [ ]: