C:/Users/Rafe/Appdata/Local/Programs/Python/Python35-32/Scripts Object and Data Structures Basics
C:/Users/Rafe/Appdata/Local/Programs/Python/Python35-32/Scripts Object and Data Structures Basics
dictionaries
my_dict[ 'k1' ]
my_dict['k2'] += 11
d['k1']['nest']
d.keys()
d.values()
d.items()
tuples
t= (1,2,3)
t.length()
t= ('ocdc', 2)
t[0] t[-1]
t.count(‘one’)
files
Pwd gives loction of current working directory
f-open(‘test.txt’)
f.read()
%%writefile new.txt
First line
Seocnd line
print (line/words)
x=set()
x.add(2)
a=True
b=’anything’
Comparison Operators
Python Statements
For Loops
print (t1)
for item in d:
print (item)
print(k)
print (v)
While Loops
x=0
while x<10 :
x +=1
if x==3:
break
else:
print (‘continuing’)
continue
range
range(10)
start=0
end=11
range(start, end, 3)
print (num)
list(x)
List Comprehensions
l.append(letter)
print(l)
Functions
def: built-in keyword. tells you are about to create a function //check python built-in functions
def is_prime(num):
“”” // docStirng
input: a number
“””
if um%2==0
break
else:
print (‘prime’)
Lambda Expression
// create anonymous functions. body similar to what we would put in def body’s return statement.
x=50
def func():
global x
print (‘global x is ’, x)
x=2
func()
//use locals() and globals() function to check current local and global variables
self.breed = breed
self.fur= fur
sam.breed
sam.species
Methods
class Circle(object)
pi=3.14
self.radius= radius
def area(self):
return(self.radius**2)*Circle.pi
self.radius= newradius
def getradius(self):
return self.radius
Inheritance
class Animal(object):
def __init__(self):
print (‘animal created’)
def whoAmI(self):
print (‘Animal’)
def eat(self):
print (‘Eating’)
Class Dog(Animal):
def __init__(self):
Animal.__init__(self)
def whoAmI(self):
print( ‘Dog’)
def bark(self):
d= Dog()
d.eat()
d.bark()
Special Methods
f=open(‘test987’,’r’)
f.write(‘hfiu hjsdk’)
except:
else:
print (‘successs’)
def askint():
try:
except:
finally:
print (val)
def askinteger():
while True:
try:
except:
continue
else:
print (‘correct’)
break
finally:
print val
math.sqrt(4)
sqrt(4)
Built-in Functions
map()
def fahrenheit(T):
return(9.0/5)*T+32
map(fahrenheit, temp)
reduce()
lst= [1,3,45,878,45,65]
def max_find(a,b):
if a>b:
return a
else:
return b
max_find(12,100)
reduce(max_find, lst)
filter()
if num%2==0
return True
else:
return False
ls= [1,45,76,2,4,5]
filter(evencheck, ls)
zip()
a= [1,6,4,85,3,9,0]
b= [2,54,6,9,1,54,23]
zip(a,b)
print(max(pair))
zip(d1, d2.itervalues())
def switch(d1, d2)
dout= {}
dout[d1key]: d2val
return dout
switch(d1,d2)
enumerate()
if i>2:
break;
else:
print (item)
complex(2,8)
Python Decrorators
s= 'akjhk skjnc kjv'
def fu():
print(locals())
print(globals())
print(globals()['s'])
def fu(name= 'arfe'):
greet = fu
fu()
greet()
del fu
greet()
def hello():
def welcome():
welcome()
hello()
if name= 'arfe':
return hello
else:
return welcome
x= fu()
print(x())
#function as argument
def nd(func):
def wf():
func()
return wf
def fnd():
fnd= nd(fnd)
fnd()
@nd
def fnd():
fnd()
python Generators
def gencubes(n):
for num in range(n):
yield num**3
for x in gencubes(10):
print(x)
def gencubes(n):
out=[]
out.append(num**3)
return out
for x in gencubes(10):
print(x)
s= 'hello'
for let in s:
print(let)
next(s)
s= 'hello'
s_iter = iter(s)
next(s_iter)
next(s_iter)
next(s_iter)
#counter
s='he he ho ka jndj'
lst= [1,1,2,1,1,3,2,4,5,5,3,2,32,4]
Counter(lst)
print(Counter(s))
words=s.split()
print(Counter(words))
c=Counter(words)
print(c.most_common(2))
#defaultdict