Notes from udemy course
- JupyterIntro
- Python 2 and 3 difference
- git Intro: Learn git online
- Strings: indexing starts at [0].
- Then [-1] is end.
- [:] is all.
- [:3] is first three.
- [3:] leaves first 3.
- [::1] is increments of all.
- [::-1] is everything reversed.
- s[:3:-1] does something interesting.
- 'original string'.format('appended string')
- python 3 print() function and not print statement
- no item/char assignment in string. s[0] = 'x' will not work.
- '+' concatenates. '*' repeats.
- s.append(), s.split(','), ...
print 'bla bla %s %1.3f' %('Hi',3.1456)
print 'bla bla {x} here {x} there {y}'.format(x='inserted text', y = 3.14)
- List can hold multiple datatypes. ['Hi', 1, 3.14]
- List of lists is possible.
- l[0] is first element.
- l[:3] all unto 3rd. (first 3. i.e., 0,1,2)
- l = [1,2,[1,3,4]] is valid. l[0][1] returns 3.
- list comprehensions: [row[0] for row in matrix].
dict = {'KeyN':'ValueN', ... }
dict['KeyN']
returns 'ValueN',- dictionary supports flexible data types. Lists, etc.
- nested dictionaries possible. dict['K1']['NestedKM'][2] works
dict.keys()
returns list of keys, not in order.dict.values()
returns list of values.dict.items()
returns tuples of ('key', values)
- Tuples are data-type flexible but immutable.
- t =(1,2,3), t[0] = 4 will throw an error!
- very few methods. t.count(), t.index()
- f = open(fileNameString) returns a file object
- f.read() reads the file and changes reading cursor to eof
- f.seek(0) sends cursor back to zero
- f.readlines() returns list of strings
%%writefile new.text
to write in file- reading lines
for lines in open('new.text'):
print lines
- will print lines
- Alternatively, for words in open('new.text'): will also print lines
- won't read entire file in memory
- f.readlines() will store entire file string in memory. avoid.
- Sets are like dictionaries with just keys.
- x = set() creates a set
- x.add(2) adds 2 to set
- set doesn't allow repetitions.
set(xlist)
will return non repeating elements of the list- a = True creates a boolean
- 1 > 2 is a boolean
- b = None is a placeholder
- == equal to
- != not equal
- <> not equal
- < left lesser than right
- > left greater than right
- <= lesser than of equal to
- >= greater than or equal to
1 < 2 < 3
is same as1 < 2 and 2 < 3
1 < 3 > 2
is same as1 < 3 and 3 > 2
- and
- or
- python gets rid of () and {} using : and indentation. more readable.
- no semi colons like in C++
- Syntax:
if case 1:
perform action 1
elif case 2:
perform action 1
else:
perform action 3
for element in list_l:
print element
# prints elements in the list
for element in string_l:
print element
prints letters in the string- tuples unpacking
- l = [(2,4),(6,8),(10,12)]
for tup in l:
print tup
prints the tuplesfor (t1,t2) in l:
print t1
prints first element in the tuplefor item in dictionary_d:
print item
prints keys in the dictionary- in python 2,
for k,v in d.iteritems():
- iteritems() creates a generator (comes later)
- in python 3,
for k,v in d.items():
while test:
code statement
else:
final code statement
break
, breaks out of closest enclosing loopcontinue
, continues to the top of closest enclosing looppass
, does nothing at all
range(stop)
gives list of integersrange(start, stop[, step])
- generator does not store every instance in memory
- in python 2, range() returns a list. list saved in memory.
- in python 2, xrange() returns a generator. not saved in memory.
- in python 3, range() returns a generator. not saves in memory.
- creates list. flatten outs a for loop within a list
[ element_function_of_item for item in list_l ]
lst = [x for x in 'word']
creates ['w', 'o', 'r', 'd']lst = [x**2 for x in range(0,11)]
- conditions work
[ element_function_of_item for item in list_l if condition_on_item]
- nested lists work
[ x**2 for x in [x**2 for x in xrange(1,11) ]]
- generator comprehension and dictionary comprehension
- methods are functions built into objects
object.method(arg1,arg2,...)
form- use tab to show available methods
- use shift+tab to show details of a method
- help(l.count) to show details of a method
- methods as having an argument 'self' referring to the object itself.
- 'block of reusable statements'
lambda num: num**2
returns a function- could be saved in a function variable name
square = lambda num: num**2
and callsquare(2)
- works with multiple arguments,
adder = lambda x,y : x+y
adder(2,3)
- more notes
- works well with map, reduce, and filter
- map(function, sequence_iterable)
- map(lambda_expression, sequence_iterable)
- LEGB
- Local
- Enclosing function
- Global
- Built-in
globals()
andlocals()
will return respective variables- functions are objects and hence could be assigned variable names
- Object
- Class
- Attribute
- Method
- class object attribute
- Inheritance
- base class and derived class
- Also check
__call__
and__init__
are different so1, so2- _ and __ method notation
try:
You do your operations here...
...
except ExceptionI:
If there is ExceptionI, then execute this block.
except ExceptionII:
If there is ExceptionII, then execute this block.
...
else:
If there is no exception then execute this block.
finally:
clode block will run irrespective of exceptions
- Packagaes are set of modules in a folder with one extra file
- Package folder has
__init__.py
which could be empty - print
dir(module)
for available function help(module.function)
for documentation__all__ = ["func1", "func2"]
in__init__.py
to import specific functions
- Python 3 use iterator for map, reduce and filter
- Python 2 use lists for map, reduce and filter
- map, reduce, and filter
- map(function, sequence_iterable)
- map(lambda_expression, sequence_iterable)
- Also check Series.map()
- map()
map(func, iterable)
- applies function to all elements in iterable
- reduce()
map(func, iterable)
- applies function recursively
- filter()
filter(condition, list)
return list that satisfies condition - zip()
zip(listA,listB)
return list of tuples (listAele, listBele)- smallest list decides length
- enumerate()
enumerate(iterable)
returns tuple (eleNum, iterVal) - complex()
complex(a,b)
orcomplex('a+bj')
returns complex number - any() -
any(list)
to find if any satisfies condition. returns True or False- I thought any(condition, list) to find indices that satisfies condition
- use list comprehension
filter()
does this
- all()
all(list)
to find if all satisfy condition. returns True or False
- scope
globals()
andlocal()
- functions are objects
hello()
function could be assigned to a variablegreet = hello
greet
is a seperate object.hello
could be deleted- functions within function possible
()
will execute the function- this
@new_decorator
def func_needs_decorator():
print "This function is in need of a Decorator"
- is short form for
def new_decorator(func):
def wrap_func():
func()
return wrap_func
- and
def func_needs_decorator():
print "This function is in need of a Decorator"
- and
func_needs_decorator = new_decorator(func_needs_decorator)
- together
- very useful in web frameworks like Flask
- HW
yeild
instead ofreturn
- This is the holy grail!
- proceeds next on demand/request
- doesn't load all the items in the memory
- suppose
g
is a generator next(g)
will yeild the next element- stackoverflow generator
- stackoverflow itertor
from collections import Counter
counter
- dict subclass that helps count hash-able objects
- common patters
sum(c.values()) # total of all counts
c.clear() # reset all counts
list(c) # list unique elements
set(c) # convert to a set
dict(c) # convert to a regular dictionary
c.items() # convert to a list of (elem, cnt) pairs
Counter(dict(list_of_pairs)) # convert from a list of (elem, cnt) pairs
c.most_common()[:-n-1:-1] # n least common elements
c += Counter() # remove zero and negative counts
defaultdict
- never raises key error
- can be initialized
d = defaultdict(lambda: 0)
OrderedDcit
- remembers the order in which elements are added
namedtuple
- hard to remember tuple indices
- namedtuple allows numerical as well as named indices
Dog = namedtuple('Dog','age breed name')
sam = Dog(age=2,breed='Lab',name='Sammy')
sam[0]
orsam.age
works
- collections docs
import pdb
pdb.settrace()
sets a breakpoint- press q to quit the debugger
- pdb docs
timeit.timeit('string operation', n=executionNo)
prints time for execution%timeit <operation>
builtin magic for notebooksmap
seems faster compared to list comprehension
- text matching patterns
re.search(pattern,string)
returns a match object- the match object has lots of methods
- re docs
re.split(splitterm, phrase)
returns a split list without the splittermre.findall(pattern,string)
returns list of pattern in string- Repitition
- Character sets
- Exclusion
- Escape Codes
- Implements an in-memory file like object
f = StringIO.StringIO(string)
creates a file object from stringf.read()
,f.write()
,f.seek()
will workcStringIO
is faster
- hex(), bin(), pow(), abs(), round(num, decimals)
- Changing case: s.capitalize(), s.uper(), s.lower()
- Counting and Finding: s.count(), s.find()
- Formatting: s.expandtabs(), s.center()
- Checks: s.isalnum(), s.alpha(), s.islower(), s.isspace(), s.istitle(), s.isupper(), s.endswith()
- RegEx:
s.split()
splits at every occurance - RegEx:
s.partition()
splits at first occurance
- no duplicate elements
- s.add(), s.clear(), s.copy()
- s.difference(), s1.difference_update(s2)
- s.intersection(), s.intersection_update()
- s1.isdisjoint(s2)
- s.issubset(), s.isupperset()
- s1.symmetric_difference(s2)
- s.union()
s1.update(s2)
updates with union
- Dictionary comprehension works
- d.iteritems() return both keys and values
- d.itervalues() returns values
- d.iterkeys() returns keys
- d.viewvalues(), d.viewkeys()
- l.append(), l.count(), l.extend(), l.index()
- l.pop(), l.remove(), l.reverse(), l.sort()