Sorting a list of tuples by multiple conditions:
sorted_by_length = sorted(list_,
key=lambda x: (x, len(x), float(x)))
To get the lower bound integer in python:
write a python file with filename.py extension and add definitions and statements in them.
‘import filename’ in another file.
access definitions inside that file using filename.nameofdef
concatenating to a tuple: tup=tup+(a[i],)
list1= – ordered sequence.
Can’t mutate a string. assignment error.
Aliasing problem in python:
if you have an object say a list and you attach it to the name list1
if you say list2=list1
list2=list1, then list2 points to same object as that of list one.
If you add an element to list2, then it will be reflected in list1 as well which you might not have intended.
So how to take a copy of list1 without having any impact on list1 if I do changes to the copy?
sort the original list-> list1.sort()
take a sorted list of original list into another object
http://stackoverflow.com/q/2739552/5819704 – [[1,2]]*3, all three lists inside outer list refer to same object. This doesn’t sync with your intuition at the beginning.
tuple and string doesn’t support item assignment i.e. s=5, only list supports.
Functions as objects:
sending functions as arguments to other functions.
Higher order programming: Map
Dictionaries: A list of key, value pairs
‘Vivek’ in my_dict -> True
Memorization: Storing the already computed values in dictionary and looking up for them in case we need them again. This will save from computing something that is already computed.
- Unit testing-
- validate each piece of the program.
- testing each function separately.
- Regression testing-
- After each fix, you need to retest to confirm already tested modules or not broken by the latest fix.
- Integration testing-
- Does overall program work?
- Black box testing-
- without looking at the code.
- done by other than the implementer to avoid some implementer biases.
- Glass box testing:
- Path complete.
- Design of test cases based on code.
- Defensive programming:
- specifications for functions.
- Modularize programs.
- check conditions on inputs/outputs.
- Eliminate the source of bugs:
assert to check whether the assumptions are met or not.
classes and objects:
Data attributes associated with the class, but not with instance or objects.
Generator: yield 1 in method. runs upto the yield and stops. again call the funct.__next__() runs upto next yield and stops.
Lisp and python programming languages allow you to implement reflection.
import pylab as plt
plt.figure(‘figurename’) – to plot in new figure
plt.plot(list of x values, list of y values,’b-‘,label=’linear’)
Finds coefficients of a polynomial of degree n, that provides a best least squares fit for the observed data. It returns n+1 values.
Anonymous function – lambda:
f1=lambda x,y: x+y
Credits: Jake Vanderplas
An abstract class in python can’t be instantiated.