2+4
10/9
10//9
2**8
20**500
complex(0,1)
2+3j
1j**2
a= 5
a
a
print(a)
2**8
a = 5
b = 17
a,b = b,a
a,b
3<4
3==5
3!=5
a is b
type(a)
if a>b:
print('Hey')
print('Done')
if b>a:
print('Hey')
else:
print('Wow!')
print('Done')
if b>a:
print('Hey')
elif 3==3:
print('Wow!')
print('Done')
for i in [1,3,5]:
print(i)
print('ha')
l = list(range(10))
l
l[0]
l[-1]
l[2:5]
l = 'hello, there'
l[2:5]
t = (5,50,500)
t
for item in t: print(item)
type(t)
l[0:9:2]
l[:9:2]
l[::2]
l[::-1]
'reverse'[::-1]
'e' in l
List comprehensions
[i**2 for i in range(11)]
[3*a for a in 'hello']
s = set([5,6,6,9,9,9,3])
s
5 in s
'hello'+', there!'
'hello'+str(3)
'a'<'b'
s = 'https://docs.google.com/spreadsheets/d/1h-iUlLKoC66xjKRUn66qpao8Sw_qj1ESqSj9JqJXZa0/edit#gid=18828465'
s.split('/')
s.replace('https','http')
s = s.replace('https','http')
s
d = {'banana':'long yellow fruit','zebra':'striped animal'}
d
d['banana']
d['bacon']='tasty but unhealthy meat'
d
d2 = {i:i**2 for i in range(10)}
d2[9]
type(d2)
len(d2)
len('hello')
[k for k in d2 if d2[k]==3]
[blah for blah in d2]
[blah for blah in d2.values()]
pwd
f = open('1st_debate_clinton.txt')
clinton = f.read()
len(clinton)
clinton[:20]
Goal: create a dictionary whose keys are the distinct words in the text, and the values are the counts.
clinton = clinton.lower()
clinton[:20]
clinton = clinton.replace('\n',' ')
punc = '.,;:!?()[]'
for p in punc:
clinton = clinton.replace(p,'')
clinton[:20]
d = {}
for word in clinton.split(' '):
if word in d:
d[word] += 1
else:
d[word] = 1 # create new entry
len(d)
# How to define a function
def mycomparison(x):
return x[1] # return the 2nd part (value)
mycomparison(('aaa',20))
#sorted(d.items(),key=mycomparison)[::-1]
def foo(f,x):
return f(x)
from math import sin
foo(sin,4)
def gah(x):
return x,x,x
gah(3)
from math import * # not recommended
import math
math.cos(5)
from numpy.linalg import norm as norm