A set of reference materials to help you in those occassional bouts of forgetfulness.
Table of contents
In [ ]:
15 + 15
In [ ]:
# the pound sighn '#' are comments in Python
"string1" + " string2"
In [ ]:
# import a module or site-package using 'import' keyword.
import os
os.getcwd()
In [ ]:
#list contents of current dir
os.listdir(os.getcwd())
In [ ]:
# import sys module and find the OS of the current computer
import ...
In [ ]:
# use OS module to find the number of cores on the current computer
In [ ]:
# use datetime module to find the current date and time
Following are some resources to learn Python
In [1]:
l1 = list()
type(l1)
Out[1]:
In [2]:
l2 = []
len(l2)
Out[2]:
In [3]:
l3 = [1,2,3,4,5,6,7,8,9]
l3[:] #prints all
Out[3]:
In [4]:
l3[0]
Out[4]:
In [5]:
l3[:4] #prints first 4. the : is slicing operator
Out[5]:
In [6]:
l3[4:7] #upto 1 less than highest index
Out[6]:
In [7]:
a = len(l3)
l3[a-1] #negative index for traversing in opposite dir
Out[7]:
In [8]:
l3[-4:] #to pick the last 4 elements
Out[8]:
In [9]:
l3.reverse() #happens inplace
In [10]:
l3
Out[10]:
In [11]:
l3.append(10) #to add new values
l3
Out[11]:
In [12]:
a1 = ['a','b','c']
l3.append(a1)
In [13]:
l3[-1]
Out[13]:
In [14]:
a1 = ['a','b','c']
l3.extend(a1) #to splice two lists. need not be same data type
l3
Out[14]:
In [15]:
lol = [[1,2,3],[4,5,6]] #lol - list of lists
len(lol)
Out[15]:
In [16]:
lol[1].reverse()
lol[1]
Out[16]:
In [17]:
l3
Out[17]:
In [18]:
l3[-1] = 'solar fare' #modify the last element
l3
Out[18]:
In [19]:
#list.insert(index, object) to insert a new value
print(str(len(l3))) #before insertion
l3.insert(1,'two')
l3
Out[19]:
In [20]:
# l3.pop(index) remove item at index and give that item
l3.pop(-3) #remove 3rd item from last and give them
Out[20]:
In [21]:
l3
Out[21]:
In [22]:
# l3.clear() to empty a list
lol.clear()
lol
Out[22]:
In [1]:
l3 = [9, 8, 7, 6, 5, 4, 3, 2, 1, 10, ['a', 'b', 'c'], 'a', 'b', 'c', 10,10,10]
l3
Out[1]:
In [2]:
# l3.count(value) counts the number of occurrences of a value
l3.count(10)
Out[2]:
In [3]:
# l3.index(value, <start, <stop>>) returns the first occurrence of element
l3.index(10)
Out[3]:
Find all the indices of an element
In [5]:
# indices = [i for i, x in enumerate(my_list) if x == "whatever"]
#find all occurrence of 10
indices_of_10 = [i for i, x in enumerate(l3) if x == 10]
indices_of_10
Out[5]:
In [7]:
list(enumerate(l3))
Out[7]:
In [26]:
d1 = dict()
d2 = {}
len(d2)
Out[26]:
In [27]:
d3 = {'day':'Thursday',
'day_of_week':5,
'start_of_week':'Sunday',
'day_of_year':123,
'dod':{'month_of_year':'Feb',
'year':2017},
'list1':[8,7,66]}
len(d3)
Out[27]:
In [28]:
d3.keys()
Out[28]:
In [29]:
d3['start_of_week']
Out[29]:
In [30]:
type(d3['dod'])
Out[30]:
In [31]:
# now that dod is a dict, get its keys
d3['dod'].keys()
Out[31]:
In [32]:
d3['dod']['year']
Out[32]:
In [33]:
d3['day_of_year'] = -48
d3
Out[33]:
In [46]:
# insert new values just by adding kvp (key value pair)
d3['workout_of_the_week']='bungee jumpging'
d3
Out[46]:
In [47]:
d3['dayyy']
In [48]:
# safe way to get elements is to use get()
d3.get('day')
Out[48]:
In [49]:
d3.get('dayyy') #retuns None
In [50]:
# use items() to get a list of tuples of key value pairs
d3.items()
Out[50]:
In [51]:
# use values() to get only the values
d3.values()
Out[51]:
In [58]:
t1 = tuple()
t2 = ()
len(t1)
Out[58]:
In [59]:
type(t2)
Out[59]:
In [60]:
t3 = (3,4,5,'t','g','b')
t3[0]
Out[60]:
In [61]:
#use it just like a list
t3[-1]
Out[61]:
In [62]:
t3[0] = 'good evening'
In [63]:
s1 = set([1,1,1,2,2,2,4,4,4,4,4,4,4,5])
s1
Out[63]:
In [64]:
s2 = {1,2,2,2,2,3}
s2
Out[64]:
In [65]:
# works on dicts too
s3_repeat_values = set({'k1':'v1',
'k2':'v1',
'k3':'v2'})
s3_repeat_values
Out[65]:
In [66]:
type(s3_repeat_values)
Out[66]:
In [67]:
# repeating keys
s3_repeat_keys = set({'k1':'v1',
'k1':'v2'})
In [68]:
s3_repeat_keys
Out[68]:
Note. When you create a dict with duplicate keys, Python just keeps the last occurrence of the kvp. It thinks the kvp needs to be updated to the latest value
In [69]:
d80 = {'k1':'v1', 'k2':'v2', 'k1':'v45'} # k1 is repeated
d80
Out[69]:
In [70]:
list1 = [1,2,3,4,5,6,7]
for element in list1:
print(element)
In [71]:
for element in list1:
print(element, " squared ", element*element)
In [72]:
for count in range(11,20):
print(str(count))
In [73]:
list2_comp = [e*e for e in list1]
list2_comp
Out[73]:
In [74]:
list2_even = [e*e for e in list1 if e%2==0]
list2_even
Out[74]:
In [39]:
d3 = {'day':'Thursday',
'day_of_week':5,
'start_of_week':'Sunday',
'day_of_year':123,
'dod':{'month_of_year':'Feb',
'year':2017},
'list1':[8,7,66]}
In [43]:
# get those kvp whose value is a list
{k:v for k,v in d3.items() if type(v)==list}
Out[43]:
reverse keys and values? - works only when values are immutable types. hence filter them out
In [45]:
d4 = {k:v for k,v in d3.items() if type(v) not in [list, dict]}
d4
Out[45]:
In [47]:
# reverse keys and values
d4_reverse = {v:k for k,v in d4.items()}
d4_reverse
Out[47]:
In [ ]: