#!/usr/bin/python
#coding=utf-8
#
#
S = 'abcdefghijk' #a b c d e f g h i j k
for i in range(0, len(S), 2):#0 1 2 3 4 5 6 7 8 9 10 11
print S[i]
print len(S)
#
S = 'abcdefghijk'
for (index, char) in enumerate(S):
print index
print char
#
ta = [1, 2, 3]
tb = [9, 8, 7]
tc = ['a', 'b', 'c']
for (a, b, c) in zip(ta, tb, tc):
print(a, b, c)
#
ta = [1,2,3]
tb = [9,8,7]
# cluster
zipped = zip(ta,tb)
print (zipped)
# decompose
na, nb = zip(*zipped)
print(na, nb)
#
f = open('test.txt')
f.next()
f.next()
f.next()
f.next()
f.next()
f.next()
f.next()
for line in open('test.txt'):
print line
#
def gen():
a = 100
yield a
a = a*8
yield a
yield 1000
for i in gen():
print i
def gen():
for i in range(4):
yield i
G = (x for x in range(4))
#
L = []
for x in range(10):
L.append(x**2)
print L
L = [x**2 for x in range(10)]
print L
#
#lambda
func = lambda x,y: x + y
print func(3, 4)
def func(x, y):
return x + y
print func(3, 4)
#
def test(f, a, b):
print 'test'
print f(a, b)
test(func, 3, 5)
def U(a, b, c):
print 'U'
print a(b, c)
U(func, 5, 6)
#map() ,map()
re = map((lambda x:x+3),[1, 2, 3, 5, 6])
print re
re = map((lambda x,y: x+y),[1,2,3],[6,7,9])
print re
#filter() , True,
def func(a):
if a > 100:
return True
else:
return False
print filter(func, [10, 56, 101, 500])
#reduce() ,reduce
print reduce((lambda x, y: x + y), [1, 2, 3, 4,9])
#
re = iter(range(5))
try:
for i in range(100):
print re.next()
except StopIteration:
print 'here is end ',i
print 'HaHaHaHa'
'''
re = iter(range(5))
for i in range(100):
print re.next()
print 'HaHaHaHa'
try:
print(a*2)
except TypeError:
print("TypeError")
except:
print("Not Type Error & Error noted")
def test_func():
try:
m = 1/0
except NameError:
print("Catch NameError in the sub-function")
try:
test_func()
except ZeroDivisionError:
print("Catch error in the main program")
print 'Lalala'
raise StopIteration('this is error')
print 'Hahaha'
'''
#
a = 3
a = 'au'
print a
a = 5
b = a
a = a + 2
print a
print b
L1 = [1, 2, 3]
L2 = L1
L1 = 1
print L1, L2
L1 =[1, 2, 3]
L2 =L1
L1[0] = 10
print L2
#
def f(x):
x = 100
print x
a = 1
f(a)
print a
def f(x):
x[2] = 100
print x
a =[1, 2, 3]
f(a)
print a
xl =[1, 3, 5]
yl =[9, 12, 13]
L = [x**2 for (x,y) in zip(xl, yl) if y > 10]
print L
# , 、 、list、tuple、
a = 2
b = ' '
c = [1, 2, 3]
d = (1, 2, 3)
e = {'tom': 11, 'sam': 57, 'lily': 100}
def num(x): #
x = 100
print x
num(a)
print a
def str(x): #
x = 'ade'
print x
str(b)
print b
def list(x): #list
x[0] = 100
print x
list(c)
print c
def tuple(x): #tuple
x[0] = 100
print x
tuple(d)
raise StopIteration()
print d
def dic(x): #
x['tom'] = 100
print x
dic(e)
print e