f = lambda x: 2*x + 5 print f(3) print f(5) f = lambda x : x%2 and "Odd" or "Even" print "5 is",f(5) print "10 is",f(10)
Output:
11 15 5 is Odd 10 is Even
f = lambda x: 2*x + 5 print f(3) print f(5) f = lambda x : x%2 and "Odd" or "Even" print "5 is",f(5) print "10 is",f(10)
11 15 5 is Odd 10 is Even
import shutil
import os
# copy(& rename) file from src to dst.
src = os.getcwd()+"\\"+"a.txt"
dst = os.getcwd()+"\\"+"data\\new_a.txt"
shutil.copyfile(src, dst)
# copy file from src to dst directory.
src = os.getcwd()+"\\"+"a.txt"
dst = os.getcwd()+"\\data2"
shutil.copy(src, dst)
'''
Recursively copy an entire directory tree rooted at src.
dst file must not exists
'''
src = os.getcwd()+"\\" + "data"
dst = os.getcwd()+"\\" + "data3"
shutil.copytree(src, dst)
# Recursively move a file or directory to another location.
src = os.getcwd()+"\\" + "data3"
dst = os.getcwd()+"\\" + "data4"
shutil.move(src, dst)
# delete entire directory
dest_file = os.getcwd()+"\\data3"
shutil.rmtree(dst)
s1 = "life is very easy with python" s2 = "python" print s1 print s1.title() print s2 print s2.title()
life is very easy with python Life Is Very Easy With Python python Python
import itertools
A = [ 1, 2, 3]
B = [4]
C = [5, 6]
print "product example:"
s=[ A, B, C ]
print list(itertools.product(*s))
print "permutations example:"
i = 1
for n in itertools.permutations(A,3):
print i,":",n
i += 1
print "combinations example:"
i = 1
for n in itertools.combinations("ABCD",3):
print i,"[:]",n
i += 1
print "combinations_with_replacement example:"
print list( itertools.combinations_with_replacement("ABD",3))
product example:
[(1, 4, 5), (1, 4, 6), (2, 4, 5), (2, 4, 6), (3, 4, 5), (3, 4, 6)]
permutations example:
1 : (1, 2, 3)
2 : (1, 3, 2)
3 : (2, 1, 3)
4 : (2, 3, 1)
5 : (3, 1, 2)
6 : (3, 2, 1)
combinations example:
1 [:] ('A', 'B', 'C')
2 [:] ('A', 'B', 'D')
3 [:] ('A', 'C', 'D')
4 [:] ('B', 'C', 'D')
combinations_with_replacement example:
[('A', 'A', 'A'), ('A', 'A', 'B'), ('A', 'A', 'D'), ('A', 'B', 'B'), ('A', 'B', 'D'), ('A', 'D', 'D'), ('B', 'B', 'B'), ('B', 'B', 'D'), ('B', 'D', 'D'), ('D', 'D', 'D')]
import itertools
def add(*args):
s = 0;
for num in args:
s += num
return s
print "imap example:"
# will calculate 5+2+10, 2+3+10
for n in itertools.imap(add, (5,2,3), (2, 3, 3),(10, 10)):
print n
print "chain example"
for ch in itertools.chain('ABC','DEF'):
print ch
print "ifilter example "
A = [1,2,4,5,7,8]
for n in itertools.ifilter(lambda x:x%2 ,A):
print n
print "ifilterfalse example:"
for n in itertools.ifilterfalse(lambda x:x%2 ,A):
print n
print "compress example:"
for ch in itertools.compress('abcdef',[0,1,0,1,1,1]):
print ch
imap example:
17
15
chain example
A
B
C
D
E
F
ifilter example
1
5
7
ifilterfalse example:
2
4
8
compress example:
b
d
e
f
izip example:
('a', 'X')
('b', 'Y')
izip_longest example:
('a', 'X')
('b', 'Y')
('c', None)
('d', None)