【Python】for遍历

  • for可以遍历list,tule,dict

代码练习

st = input('Please input')
if eval(st)==True:
    print('true')
else:
    print('false')

for i in range(5):
    print('hello word!')

n = int(input('input print number:'))
for a in range(n):
    print('hello word!')
    
a = int(input('首项为:'))
n = int(input('项数为:'))
d = int(input('公差为:'))
s = 0
for i in range(n):
    s = s + a
    a = a + d
    print(i,a,s)
    
m = tuple(range(10)) 
for i in m:
    print(i)

m = dict(a=1,b=2,c=3,d=4,e=5,f=6)
print(m)

k = m.keys()
for i in k:
    print(i)


m = [1,2,3,4,5,'a','b','c',[1,2,4]]
for i in m:
    print(i)

s = 'sajkkghgklg'
for i in s:
    print(i)

for i in list(range(10)):
    print(i)

for i in range(10):
    print(i)

m = dict(a=1,b=2,c=3,gg=5,ss=6)
k = m.keys()
v = m.values()
for i in k:
    print(i)
for i in v:
    print(i)
for (k,v) in m.items():
    print('{}-{}'.format(k,v))

k = ['a','b','c']        
v = [1,2,3]
m = []
for i in range(len(k)):
    m.append([k[i],v[i]])
m2 = dict(m)
print(m2)

n = int(input('阶乘运算n='))
s = 1
for i in range(n):
    s=s*(i+1)
print(s)

你可能感兴趣的:(【Python】for遍历)