On Github parthrbhatt / python-talk
$ pip install virtualenv
$ cd my_project_folder $ virtualenv venv $ source venv/bin/activate
import sys def fib(n): """Fibonacci series""" a, b = 0, 1 sys.stdout.write('%s ' %a) while b < n: a, b = b, a + b sys.stdout.write('%s ' %a) if '__main__' == __name__: n = raw_input('Enter a number: ') print 'Fibonacci series' fib(int(n))
from fib import fib if '__main__' == __name__: fib(10)
a, b = b, a
a = 'This is a string.' b = "This is also a string." c = "do or don't" d = 'She said "I know python"' e = "This string spans\ across multiple lines" f = """This string spans across multiple lines""" print '='*80 print a[3], a[-3] print len(a)
import sys from os.path import join
if '__main__' == __name__: fib(10)
Structure Python's module namespace.
from sound.formats import waveread, wavewrite import sound.effects.echo from sound.formats import * from sound.effects.echo import echofilter
Create small anonymous functions.
add = lambda a, b: a+b add(10, 20)
Syntactically restricted to a single expression.
filter(function, sequence)
filter(lambda x: not x%2, [1,2,3,4])
map(function, sequence)
def sq(a): return (a*a) lst = [1,2,3,4] map(sq, lst)
Or, of course:
map(lambda x: x*x, [1,2,3,4])
Returns a single value constructed by calling the (binary) function on the first two items of the sequence, then on the result and the next item, and so on
reduce(function, sequence)
Eg:
def add(a, b): return (a+b) lst = [1,2,3,4] reduce(add, lst)
Of course, the above is same as:
reduce(lambda x,y: x+y, [1,2,3,4])
Thank You !