In [1]:
2+4
Out[1]:
6
In [2]:
10/9
Out[2]:
1.1111111111111112
In [3]:
10//9
Out[3]:
1
In [4]:
2**8
Out[4]:
256
In [6]:
20**500
Out[6]:
327339060789614187001318969682759915221664204604306478948329136809613379640467455488327009232590415715088668412756007100921725654588539305332852758937600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
In [7]:
complex(0,1)
Out[7]:
1j
In [8]:
2+3j
Out[8]:
(2+3j)
In [9]:
1j**2
Out[9]:
(-1+0j)
In [10]:
a= 5
a
Out[10]:
5
In [11]:
a
Out[11]:
5
In [12]:
print(a)
5
In [13]:
2**8
Out[13]:
256
In [14]:
a = 5
b = 17
In [15]:
a,b = b,a
In [16]:
a,b
Out[16]:
(17, 5)
In [17]:
3<4
Out[17]:
True
In [18]:
3==5
Out[18]:
False
In [19]:
3!=5
Out[19]:
True
In [20]:
a is b
Out[20]:
False
In [21]:
type(a)
Out[21]:
int

Header

Subheader

Hello. $$\int_0^\infty e^{-x} dx$$

In [23]:
if a>b:
    print('Hey')
print('Done')
Hey
Done
In [26]:
if b>a:
    print('Hey')
else:
    print('Wow!')
print('Done')
Wow!
Done
In [27]:
if b>a:
    print('Hey')
elif 3==3:
    print('Wow!')
print('Done')
Wow!
Done
In [29]:
for i in [1,3,5]:
    print(i)
    print('ha')
1
ha
3
ha
5
ha
In [34]:
l = list(range(10))
l
Out[34]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [35]:
l[0]
Out[35]:
0
In [36]:
l[-1]
Out[36]:
9
In [37]:
l[2:5]
Out[37]:
[2, 3, 4]
In [38]:
l = 'hello, there'
In [39]:
l[2:5]
Out[39]:
'llo'
In [40]:
t = (5,50,500)
t
Out[40]:
(5, 50, 500)
In [41]:
for item in t: print(item)
5
50
500
In [42]:
type(t)
Out[42]:
tuple
In [43]:
l[0:9:2]
Out[43]:
'hlo h'
In [44]:
l[:9:2]
Out[44]:
'hlo h'
In [45]:
l[::2]
Out[45]:
'hlo hr'
In [46]:
l[::-1]
Out[46]:
'ereht ,olleh'
In [47]:
'reverse'[::-1]
Out[47]:
'esrever'
In [48]:
'e' in l
Out[48]:
True

List comprehensions

In [50]:
[i**2 for i in range(11)]
Out[50]:
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
In [51]:
[3*a for a in 'hello']
Out[51]:
['hhh', 'eee', 'lll', 'lll', 'ooo']
In [52]:
s = set([5,6,6,9,9,9,3])
s
Out[52]:
{3, 5, 6, 9}
In [53]:
5 in s
Out[53]:
True
In [54]:
'hello'+', there!'
Out[54]:
'hello, there!'
In [55]:
'hello'+str(3)
Out[55]:
'hello3'
In [56]:
'a'<'b'
Out[56]:
True
In [57]:
s = 'https://docs.google.com/spreadsheets/d/1h-iUlLKoC66xjKRUn66qpao8Sw_qj1ESqSj9JqJXZa0/edit#gid=18828465'
In [58]:
s.split('/')
Out[58]:
['https:',
 '',
 'docs.google.com',
 'spreadsheets',
 'd',
 '1h-iUlLKoC66xjKRUn66qpao8Sw_qj1ESqSj9JqJXZa0',
 'edit#gid=18828465']
In [59]:
s.replace('https','http')
Out[59]:
'http://docs.google.com/spreadsheets/d/1h-iUlLKoC66xjKRUn66qpao8Sw_qj1ESqSj9JqJXZa0/edit#gid=18828465'
In [60]:
s = s.replace('https','http')
In [61]:
s
Out[61]:
'http://docs.google.com/spreadsheets/d/1h-iUlLKoC66xjKRUn66qpao8Sw_qj1ESqSj9JqJXZa0/edit#gid=18828465'
In [62]:
d = {'banana':'long yellow fruit','zebra':'striped animal'}
d
Out[62]:
{'banana': 'long yellow fruit', 'zebra': 'striped animal'}
In [63]:
d['banana']
Out[63]:
'long yellow fruit'
In [64]:
d['bacon']='tasty but unhealthy meat'
d
Out[64]:
{'bacon': 'tasty but unhealthy meat',
 'banana': 'long yellow fruit',
 'zebra': 'striped animal'}
In [68]:
d2 = {i:i**2 for i in range(10)}
In [69]:
d2[9]
Out[69]:
81
In [70]:
type(d2)
Out[70]:
dict
In [71]:
len(d2)
Out[71]:
10
In [72]:
len('hello')
Out[72]:
5
In [74]:
[k for k in d2 if d2[k]==3]
Out[74]:
[]
In [75]:
[blah for blah in d2]
Out[75]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [76]:
[blah for blah in d2.values()]
Out[76]:
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
In [77]:
pwd
Out[77]:
'/home/tara'
In [80]:
f = open('1st_debate_clinton.txt')
clinton = f.read()
In [81]:
len(clinton)
Out[81]:
35035
In [82]:
clinton[:20]
Out[82]:
'How are you, Donald?'

Goal: create a dictionary whose keys are the distinct words in the text, and the values are the counts.

In [83]:
clinton = clinton.lower()
clinton[:20]
Out[83]:
'how are you, donald?'
In [84]:
clinton = clinton.replace('\n',' ')
In [87]:
punc = '.,;:!?()[]'
for p in punc:
    clinton = clinton.replace(p,'')
In [88]:
clinton[:20]
Out[88]:
'how are you donald a'
In [89]:
d = {}
for word in clinton.split(' '):
    if word in d:
        d[word] += 1
    else:
        d[word] = 1   # create new entry
In [90]:
len(d)
Out[90]:
1392
In [95]:
# How to define a function
def mycomparison(x):
    return x[1]  # return the 2nd part (value)
mycomparison(('aaa',20))
Out[95]:
20
In [98]:
#sorted(d.items(),key=mycomparison)[::-1]
In [99]:
def foo(f,x):
    return f(x)
In [100]:
from math import sin
In [101]:
foo(sin,4)
Out[101]:
-0.7568024953079282
In [103]:
def gah(x):
    return x,x,x
In [104]:
gah(3)
Out[104]:
(3, 3, 3)
In [ ]:
from math import *  # not recommended
In [105]:
import math
In [106]:
math.cos(5)
Out[106]:
0.28366218546322625
In [108]:
from numpy.linalg import norm as norm