第二次复习(11.29)

来源:互联网 发布:c语言char怎么输出 编辑:程序博客网 时间:2024/06/06 19:22

循环::

for循环是一个结构,导致程序要重复一定的次数。

list1=[1,2,3,4]for i in list1:    print i    
---在序列里,使用for循环遍历。


range() 可以快速的生成一个序列

range(i,j,步进值)

如果所创建的对象为整数,可以用range

列表重写:

print [i*2 for i in range(1,11)]print [i for i in range(1,11) if i%2==0]print [i**2 for i in range(1,11)]for i in [i**2 for i in range(1,11)]:    print i
for i in [i**2 for i in range(1,11) if i%2!=0]:
    print i

xrange(对象)//返回的是一个对象,对对象进行遍历。

a= xrange(10)for i in a:    print i
a= xrange(10)for i in a:    print ifor i in xrange(100):    print i


for 遍历访问字典:

dic1={'a':100,'b':100,'c':100,'d':100,'e':100}# dic1=dic.fromkeys('abcde',100)print dic1for k in dic1:    print k ,dic1[k]for k in dic1:    print "%s --->%s" % (k,dic1[k])for k in dic1:    print "%s --->%s" % (k,dic1[k]),print dic1.items()for i in dic1.items():    print ifor k,v in dic1.items():    print k,vfor k ,v, in dic1.iteritems():    print k,vfor i in dic1:    print i ,dic1[i]for i in dic1.items():    print ifor k,v in dic1.items():    print k,v    
{'a': 100, 'c': 100, 'b': 100, 'e': 100, 'd': 100}a 100c 100b 100e 100d 100a --->100c --->100b --->100e --->100d --->100a --->100 c --->100 b --->100 e --->100 d --->100 [('a', 100), ('c', 100), ('b', 100), ('e', 100), ('d', 100)]('a', 100)('c', 100)('b', 100)('e', 100)('d', 100)a 100c 100b 100e 100d 100a 100c 100b 100e 100d 100a 100c 100b 100e 100d 100('a', 100)('c', 100)('b', 100)('e', 100)('d', 100)a 100c 100b 100e 100d 100
九九乘法表

for i in xrange(1,10):    for j in xrange(1,i+1):        print "%s*%s=%s" % (j,i,j*i),    print
1*1=11*2=2 2*2=41*3=3 2*3=6 3*3=91*4=4 2*4=8 3*4=12 4*4=161*5=5 2*5=10 3*5=15 4*5=20 5*5=251*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=361*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=491*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=641*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81

for

else

for 循环如果正常结束,才会执行else 语句

break 

continue 

pass //占位

exit()//退出

import timefor i in xrange(10):    print i    time.sleep(1)else:    print 'main end'

0123456789main end

for i in xrange(10):    print i    # time.sleep(1)    if i == 5:        breakelse:    print 'main end'
012345
for i in xrange(10):    if i==3:        continue    elif i==5:        continue    elif i==6:        pass    elif i==7:        # sys.exit()        pass    print ielse:    print 'main end'print "hahaha"
012345
for i in xrange(10):    if i==3:        continue    elif i==5:        continue    elif i==6:        pass    elif i==7:        sys.exit()        # pass    print ielse:    print 'main end'print "hahaha"
01246
作业: 猜数字游戏

系统生成一个20以内的随机整数

玩家有6次机会进行猜猜看,每次猜测都有反馈(猜大了,猜小了,猜对了,结束)

六次中,猜对了,玩具赢

否则系统赢了。

随机模块:

random.randint(1,20)

Python 第三方软件:

pip list 查看

for 与while 相比

for 循环用在有次数的循环上

while 循环用在有条件的控制上。

while 

while 循环,知道表达式为假,才退出

while循环,表达式是一个逻辑表达式,必须返回一个true或false。

语法:

while expresson:

statement(s)

while是条件循环是如此,当条件变为假,循环结束。

n=0while True:    if n==10:        break;    print 'hello'    n+=1
hellohellohellohellohellohellohellohellohellohello
while True:    print 'hello'    input=raw_input("please input something,q for quit:")    if input=='q':        break
helloplease input something,q for quit:ahelloplease input something,q for quit:bhelloplease input something,q for quit:q
while input!='q':    print 'hello'    input=raw_input("please input something,q for quit:")abs()

helloplease input something,q for quit:ahelloplease input something,q for quit:ahelloplease input something,q for quit:ahelloplease input something,q for quit:q

x=''while x!='q':    print 'hello'    x=raw_input("please input something,q for quit:")    if x=='':        breakelse:    print 'hello world '
helloplease input something,q for quit:qhello world 
x=''while x!='q':    print 'hello'    x=raw_input("please input something,q for quit:")    if x=='':        break    if x=='quit':        continue    print 'continue'else:    print 'hello world '
helloplease input something,q for quit:acontinuehelloplease input something,q for quit:quithelloplease input something,q for quit:qcontinuehello world 




原创粉丝点击