赞
踩
- #语法
- while expression:
- suite_to_repeat
- #代码
- >>> sum=0
- >>> j=1
- >>> while(j<10):
- sum+=j
- j+=1
- >>> sum
- 45
- >>> j
- 10
- #语法
- for iter_var in iterable_object:
- suit_to_repeat
- >>> for i in range(3,11,2):
- print(i,end='')
-
-
- 3579
- >>> s='pyrhon'
- >>> for c in s:#遍历序列迭代
- print(c,end=' ')
-
-
- p y r h o n
- >>> for i in range(len(s)):#索引每一个元素迭代
- print(s[i],end=' ')
-
-
- p y r h o n
- >>> x=iter(range(3,11,2))
- >>> next(x)
- 3
- >>> next(x)
- 5
- >>> next(x)
- 7
- >>> next(x)
- 9
- >>> next(x)
- Traceback (most recent call last):
- File "<pyshell#28>", line 1, in <module>
- next(x)
- StopIteration
- #判断是否为可迭代对象
- >>> isinstance(range(10),Iterator)
- False
- >>> isinstance(iter (range(10)),Iterator)
- True
- #猜五次
- from random import randint
- x=randint(0,300)
- for count in range(5):
- digit=int(input('one:'))
- if digit==x:
- print('bingo')
- else:
- if digit>x:
- print ('too large')
- else:
- print ('too small')
-
- x=eval(input('1='))
- y=eval(input('2='))
- if x<y:
- x,y=y,x
- while x%y!=0:
- r=x%y
- x=y
- y=r
- print('result=',y)
- i,j,k=0,0,0
- count=0
- for i in range(21):
- for j in range(51):
- k=100-5*i-2*j
- if k>=0:
- count+=1
- print(count)
- #判断是素数
- from math import sqrt
- j=2
- while j<=100:
- i=2
- k=sqrt(j)
- while i<=k:
- if j%i==0:break
- i=i+1
- if i>k:
- print(j,end=' ')
- j+=1
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。