文章目录
前言
for(用法)
-
例:
for ITEM in range(10):
…1.求0~100加法和 sum = 0 for i in range(1,101,2): sum += i print(sum) 2.100以内偶数和 sum = 0 for i in range(2,101,2): sum +=i 3.100以内奇数和 for o in range(1,101,2): sum +=o print(sum) 4.对输入的数字进行阶乘 num = int(input('num:')) jiecheng = 1 for p in range(1,num+1): jiecheng *= p print(jiecheng)
for(练习)
for(练习1)
有1,2,3,4四个数字
求这四个数字能生成多少个互不相同且无重复数字的三位数(不能含有122,
133这种)
count = 0
for i in range(1,5):
for j in range(1,5):
for x in range(1,5):
if i != j and j != x and i != x:
print(i * 100 + j * 10 + x)
count += 1
print('The count is: %d' %count)
用户登陆程序需求:
1. 输入用户名和密码;
2. 判断用户名和密码是否正确? (name=‘root’, passwd=‘westos’)
3. 为了防止暴力破解, 登陆仅有三次机会, 如果超过三次机会,报错提示;
for i in range(2):
username = input('Please input your username:')
passwd = input('Please input your passwd:')
if username == 'westos' and passwd == 'root':
print('Welcome to linux8.0!!!')
break
print('Please check your username and passwd!')
print('%d chance left' %(2 - i))
username = input('Please input your username:')
passwd = input('Please input your passwd:')
for(练习2)
输入两个数值:
求两个数的最大公约数和最小公倍数.
最小公倍数=(num1*num2)/最大公约数
for(练习3)
模仿命令行
import os
for i in range(1000):
cmd = input('[root@python lujiaxi ~]#')
if cmd:
if cmd == 'exit':
print('logout')
break
else:
print('run %s' %cmd)
os.system(cmd)
else:
continue
while(用法)
-
例:
while 条件:
条件满足时,做的事情1
条件满足时,做的事情2
…counter = 1 while counter <= 5: print('hello python') counter = counter + 1 while 1: print('hello')
while(练习)
while(练习1)
计算:0~100之间所有数字的累积求和
python中的计数方法
常见的计数方法有两种,可以分为
自然计数法(从1开始) – 更符合人类的习惯
程序计数法(从0开始) – 几乎所有的程序语言都选择从0开始计数
因此,大家在编写程序时,应该尽量养成习惯:除非需求的特殊要求,否则>循环的计数从0开始
i = 0
result = 0
while i <= 100:
result += i
i += 1
print('The result is : %d' %result)
计算奇数和:
u = 0
result2 = 0
while u <= 100:
if u % 2 == 1:
result2 += u
u += 1
print('The result2 is : %d' %result2)
while(练习2)
*
**
***
****
*****
row = 1
while row <= 5:
col = 1
while col <= row:
print('*',end='')
col += 1
print('')
row +=1
while(练习3)
猜数字游戏
1. 系统随机生成一个1~100的数字;
2. 用户总共有5次猜数字的机会;
3. 如果用户猜测的数字大于系统给出的数字,打印“too big”;
4. 如果用户猜测的数字小于系统给出的数字,打印"too small";
5. 如果用户猜测的数字等于系统给出的数字,打印"恭喜",并且退出循环;
import random
sys = random.randint(1,100)
count = 1
while count <= 5:
user = int(input('Please guess the number:'))
count += 1
if user < sys:
print('too small')
elif user > sys:
print('too big')
# continue
else:
print('congraculations!')
break
print('only %d times left!' %(6-count))
break continue exit()
break:跳出整个循环,不会再循环后面的内容
continue:跳出本次循环,continue后面的代码不再执行,但是循环依然继续
exit():结束程序的运行
for i in range(10):
if i ==5:
break
print('hello python break')
print(i)
print('hello word break')
for i in range(10):
if i == 5:
continue
print('hello python break')
print(i)
print('hello word continue')
for i in range(10):
if i == 5:
exit()
print('hello python break')
print(i)
print('hello word exit()')
运行结果:
0
1
2
3
4
hello word break
0
1
2
3
4
6
7
8
9
hello word continue
0
1
2
3
4
其他
range
- 例:
range(stop): 0 ~ stop-1
range(start,stop): start ~ stop-1
range(start,stop,step): start ~ stop
ipython:
>>> range(5)
[0, 1, 2, 3, 4]
>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>
>>> range(1,10)
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range(1,11)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> range(1,11,2)
[1, 3, 5, 7, 9]
>>> range(2,11,2)
[2, 4, 6, 8, 10]
循环计算
在程序开发中,通常会遇到利用循环重复计算的需求(利用CPU的强大之处完成相应的复杂计算)
遇到这种情况:
1.在while上方定义一个变量,用于存放最终的计算结果
2.在循环体内部,每次循环都用最新的计算结果,更新之前定义的变量