# python-for循环

### for循环：

sum  = 0
for(i=1;i<=100;i++)
sum += 1;

for 循环使用的语法
for 变量 in range(10):    #变量从0循环到9
循环需要执行的代码

range的用法：
>>> range(5)
[0, 1, 2, 3, 4]
>>> range(10)
[0, 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(0,11,2)
[0, 2, 4, 6, 8, 10]
>>>

range(stop): 0~stop-1
range(start,stop): start~stop-1
range(start,stop,step): start~stop step(步长)

>>> for i in range(3):
...     print(i)
...
0
1
2

求1～100之间的所有偶数和
[kiosk@foundation15 day02]$/usr/local/python3.6/bin/python3 求1～100之间的所有偶数和.py 2550 [kiosk@foundation15 day02]$ cat 求1～100之间的所有偶数和.py
"""
# _*_coding:utf-8_*_
Name:求1～100之间的所有偶数和.py
Date:1/17/19
Author:westos-liming
Connect:liming.163.com
Desc:
"""
sum = 0
for i in range(0,101,2):
sum += i
求阶乘：
[kiosk@foundation15 day02]$/usr/local/python3.6/bin/python3 阶乘.py please input a num:0 1 [kiosk@foundation15 day02]$ /usr/local/python3.6/bin/python3 阶乘.py
1
[kiosk@foundation15 day02]$/usr/local/python3.6/bin/python3 阶乘.py please input a num:4 24 [kiosk@foundation15 day02]$ cat 阶乘.py
"""
# _*_coding:utf-8_*_
Name:阶乘.py
Date:1/17/19
Author:westos-liming
Connect:liming.163.com
Desc:
"""
num = int(input("please input a num:"))
if num == 0:
JC = 1
else:
JC =1
for i in range(1,num+1):
JC *= i
print(JC)


break和continue的用法：
break是跳出循环
continue是结束本次循环，进入下一次循环

for循环练习-1：

[kiosk@foundation15 day02]$/usr/local/python3.6/bin/python3 四个数字生成不同的 三位数.py 24 [kiosk@foundation15 day02]$ cat 四个数字生成不同的三位数.py
"""
# _*_coding:utf-8_*_
Name:四个数字生成不同的三位数.py
Date:1/17/19
Author:westos-liming
Connect:liming.163.com
Desc:
"""
count = 0
for i in range(1, 5):
for j in range(1, 5):
if j == i:
continue
else:
for k in range(1, 5):
if k == i or k == j:
continue
else:
count += 1
print(count)

"""
for循环练习-2：

1.输入用户名和密码；
2.判断用户名和密码是否正确（name='root',passwd='westos'）
3.登录仅有三次机会，超过3次会报错
"""
[kiosk@foundation15 day02]$/usr/local/python3.6/bin/python3 用户登录.py 请输入用户名：sadsa 请输入密码：sadsa 登录失败，您还有2次机会 请输入用户名：root 请输入密码：sad 登录失败，您还有1次机会 请输入用户名：root 请输入密码：westos 登录成功 [kiosk@foundation15 day02]$ cat 用户登录.py
"""
# _*_coding:utf-8_*_
Name:用户登录.py
Date:1/17/19
Author:westos-liming
Connect:liming.163.com
Desc:
"""
Count=3
User='root'
Passwd='westos'
for i in range(3):
Count -= 1
InUser=input("请输入用户名：")
InPasswd=input("请输入密码：")
if InUser == User and InPasswd == Passwd:
print("登录成功")
else:
print("登录失败，您还有%d次机会" %Count)
用for循环实现命令行提示符：
[kiosk@foundation15 day02]$/usr/local/python3.6/bin/python3 实现命令行提示符.py [kiosk@test ~]$ ls
run ls

[kiosk@test ~]$touch file run touch file [kiosk@test ~]$ ls
run ls
file			     实现命令行提示符.py	  用户登录.py

[kiosk@test ~]$rm -rf file run rm -rf file [kiosk@test ~]$ ls
run ls

[kiosk@test ~]$exit [kiosk@foundation15 day02]$ cat 实现命令行提示符.py
"""
# _*_coding:utf-8_*_
Name:实现命令行提示符.py
Date:1/17/19
Author:westos-liming
Connect:liming.163.com
Desc:
"""
import os
for i in range(1000):
cmd = input('[kiosk@test ~]\$ ')
if cmd:
if cmd == 'exit':
break
else:
print('run %s' %(cmd))
os.system(cmd)
else:
continue

求两个数的最大公约数和最小公倍数
"""
# _*_ coding:utf-8 _*_
Name:06_最大公约数和最小公倍数.py
Date:19-1-13
Author:westos-wsp
Connect:wsp439@sina.com
Desc:
"""
"""

求两个数的最大公约数和最小公倍数
最小公倍数=(num1 * num2) / 最大公约数
"""

#1.输入两个数字
num1 = int(input('Num1:'))
num2 = int(input('Num2:'))

#2.找出两个数中的最小值
min_num = min(num1,num2)

#3.最大公约数的范围在1～min_num之间
for i in range(1,min_num + 1):
if num1 % i == 0 and num2 % i == 0:
gys = i

#4.最小公倍数
gbs = int((num1 * num2) / gys)

print('%s和%s的最大公约数为:%s' %(num1,num2,gys))
print('%s和%s的最小公倍数为:%s' %(num1,num2,gbs))