import os
# input words
name=input("input your name:")
print("your name is:"+name)
#dic
grades={'bob':60,'lucy':78,'jack':86}
print(grades)
#set
teachers={'Twang','Tli','Txu'}
print(teachers)
#slice operation (like java substring but it is powerfull because it can use almost anywhere)
classmates=['bob','nice','lucy','tom','jack']
# [n:m] n to m-1
print(classmates[2:3])
parents=('mother','father')
print(parents)
print(parents[0:1])
print("---------------------------")
#Iterator
for Agrade in grades:
print(Agrade)
for Aname in grades.values():
print(Aname)
for name,Agrade in grades.items():
print(name,Agrade)
for Aset in teachers:
print(Aset)
for i,Aset in enumerate(teachers):
print(i,Aset)
print("---------------------------")
#range function
for i in range(12):
print(i)
for i in range(4,6):
print(i)
print("---------------------------")
pailie=[m + n for m in 'ABC' for n in 'XYZ']
print(pailie)
temp=[d for d in os.listdir('.')]
print(temp)
#[for in ]生成的是 list, (for in)是生成器,保存的是算法
gen=(x*x for x in range(5))
for temp in gen:
print(temp)
#非伯纳切数列
def fib(max):
n, a, b = 0, 0, 1
while n < max:
yield b
a, b = b, a + b
n = n + 1
return 'done'
for temp in fib(6):
print(temp)
# yanghui
def yanghuiTriangles(max):
b=[1,]
n=0
while n<max:
yield b
b=[1]+[b[i]+b[i-1] for i in range(len(b)) if i>=1]+[1]
n=n+1
return 'done'
for yh in yanghuiTriangles(5):
print(yh)
# function program ,f is a function
def add(x,y,f):
return f(x)+f(y)
print(add(-4,3,abs))
# f=x^2
def power2(x):
return x*x
r=map(power2,range(11))
print(list(r))