控制语句
评论(0) 浏览量(8574)

# -*- coding: cp936 -*-
x = 1
while x<=10:
    print("while---"+str(x))
    x += 1
words = ['this','is','an','ex','parrot']

for word in words:
    print(word)
for number in range(1,10):
    print(number)

d = {'x':1,'y':2,'z':3}
for key in d:
    print(str(key)+":"+str(d[key]))

str = "helloworld"
for i in range(len(str)):
    print(str[i])

str1 = ['x','y','z']
str2 = [1,2,3]
str3 = zip(str1,str2)
print(str3)
for s1,s2 in str3:
    print(s1,s2)
#range和xrange相同 但是如果zip等函数使用时,range会计算所有数据,而xrange只会计算所用到的数据
print(zip(range(5),xrange(10000000)))#这里取最短的,xrange不会计算后续值,而range会计算
#print(zip(range(5),range(10000000000)))#会报内溢出,因为range(10000000000)会全部计算

str1 = ['x','y','z']
str2 = [1,2,3]
str3 = zip(str1,str2)
print(str3)
for s1,s2 in str3:
    print(s1,s2)

for key in range(len(str3)):
    print(key,str3[key])#这里的key为索引

#sorted和reversed
print(sorted([4,3,2,1,234,2]))
print(list(reversed("Hello,World!")))

for n in range(1,10):
    if n==2:
        break
    print("break",n)

for n in range(1,10):
    if n==2:
        continue
    print("continue",n)

str = [x*x for x in range(10)]
print(str)

#ord返回accii
print(1+ord('a'))
       

运行结果:

while---1
while---2
while---3
while---4
while---5
while---6
while---7
while---8
while---9
while---10
this
is
an
ex
parrot
1
2
3
4
5
6
7
8
9
y:2
x:1
z:3
h
e
l
l
o
w
o
r
l
d
[('x', 1), ('y', 2), ('z', 3)]
('x', 1)
('y', 2)
('z', 3)
[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4)]
[('x', 1), ('y', 2), ('z', 3)]
('x', 1)
('y', 2)
('z', 3)
(0, ('x', 1))
(1, ('y', 2))
(2, ('z', 3))
[1, 2, 2, 3, 4, 234]
['!', 'd', 'l', 'r', 'o', 'W', ',', 'o', 'l', 'l', 'e', 'H']
('break', 1)
('continue', 1)
('continue', 3)
('continue', 4)
('continue', 5)
('continue', 6)
('continue', 7)
('continue', 8)
('continue', 9)
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
98

没有登录不能评论