1 、range()函数
range():内置python函数
range(stop):0~stop-1
range(start,stop):start~stop-1
range(start,stop,step):start~stop-1 step:为步长
示例
>>> range(5)
# [0, 1, 2, 3, 4]
# >>> range(7)
# [0, 1, 2, 3, 4, 5, 6]
# >>> range(1,5)
# [1, 2, 3, 4]
# >>> range(2,5)
# [2, 3, 4]
# >>> range(10)
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# >>> range(11)
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> 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]
2、for循环语句
for 循环使用的语法:
for 变量 in range(10):
循环需要执行的代码
else:
全部循环结束后要执行的代码
应用练习
求1~100之和
求1~100的奇数之和
sum = 0
for i in range(1,101,2):
sum += 1
print(sum)
求1~100的偶数只和
sum = 0
for i in range(2,101,2):
sum +=i
print(sum)
用户输入一个数字,求该数的阶乘:3!=321
num = int(input('Num:'))
res = 1
for i in range(1,num+1):
res = res * i
print('%d的阶乘的结果为:%d' %(num,res))
用户登陆程序
1.输入用户名和密码
2.判断用户名和密码是否正确('name==root','passwd='westos')
3.为了防止暴力破解,登陆次数仅有三次,如果超过三次机会,报错
输入两个数值:
求两个数的最大公约数和最小公倍数.
最小公倍数=(num1*num2)/最大公约数
代码
num1 = int(input('Num1:'))
num2 = int(input('Num2:'))
min_mun = min(num1,num2)
for i in range(1,min_mun+1):
if num1 % i == 0 and num2 % i == 0:
gys = i
lcm = int((num1 * num2)/gys)
print('%s和%s的最大公约数是:%s' %(num1,num2,gys))
print('%s和%s的最小公倍数是:%s' %(num1,num2,lcm))
效果展示
[root@localhost mnt]# vim gongyue.py
[root@localhost mnt]# /usr/local/python3/bin/python3 gongyue.py
Num1:2
Num2:4
2和4的最大公约数是:2
2和4的最小公倍数是:4
3、break、continue、exit()
break:跳出整个循环,不会再循环后面的内容
continue:跳出本次循环,continue后面代码不会执行, 但是循环依然继续的.
exit():结束程序的运行
break 示例 :
or i in range(10):
if i == 5:
break
else :
print(i)
运行结果:
[kiosk@foundation47 python]$ python3 shiyan.py
0
1
2
3
4
continue 示例 :
for i in range(10):
if i == 5:
continue
else :
print(i)
运行结果:
[kiosk@foundation47 python]$ python3 shiyan.py
0
1
2
3
4
6
7
8
9
exit()示例:
4 命令行提示符的实现
import os 加载os模块
-
在Python中可以方便地使用os模块运行其他的脚本或者程序,这样就可以在脚本中直接使用其他脚本,或者程序提供的功能,而不必再次编写实现该功能的代码。
-
os模块中的system()函数可以方便地运行其他程序或者脚本
-
os.system(command) 使用格式
import os
for i in range(10000):
cmd = input('[westos@localhost python]$' )
if cmd :
if cmd == 'quit':
break
else:
os.system(cmd)
else:
continue
效果展示:
[kiosk@foundation47 python]$ python3 shiyan.py
[westos@localhost python]$
[westos@localhost python]$ls
0523 day 02 my day01 shiyan.py
[westos@localhost python]$quit
[kiosk@foundation47 python]$
5、while循环语句
1.格式
while 条件满足:
满足条件执行的语句
else:
不满足条件执行的语句
2、 while 死循环
只要满足 while的条件永远为真,就会进入无限循环
示例:
while 2>1:
print('@@@@')
3、while嵌套
\t :在控制台输出一个制表符,协助我们在输出文本的时候在垂直方向保持对齐
\n:在控制台输出一个换行符
\:转译 ,将特殊字符转译成普通字符
练习:99乘法表
第一种:
row = 1
while row <= 9:
col = 1
while col <=row:
print('%d * %d = %d\t' %(row,col,row*col),end='')
col += 1
print('')
row += 1
效果展示:
[kiosk@foundation47 day 02]$ python3 2.20.py
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
4*1=4 4*2=8 4*3=12 4*4=16
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
第二种 :
cro = 9
while cro > 0 :
col = cro
while col > 0 :
print('%d*%d=%d\t' %(cro,col,cro*col),end='')
col -=1
print('')
cro -=1
效果展示
[root@localhost mnt]# vim jisuan.py
[root@localhost mnt]# /usr/local/python3/bin/python3 jisuan.py
9*9=81 9*8=72 9*7=63 9*6=54 9*5=45 9*4=36 9*3=27 9*2=18 9*1=9
8*8=64 8*7=56 8*6=48 8*5=40 8*4=32 8*3=24 8*2=16 8*1=8
7*7=49 7*6=42 7*5=35 7*4=28 7*3=21 7*2=14 7*1=7
6*6=36 6*5=30 6*4=24 6*3=18 6*2=12 6*1=6
5*5=25 5*4=20 5*3=15 5*2=10 5*1=5
4*4=16 4*3=12 4*2=8 4*1=4
3*3=9 3*2=6 3*1=3
2*2=4 2*1=2
1*1=1
第三种 :
cro = 9
while cro > 0 :
col = 9
while col > 0 :
if col > cro :
print(' \t' ,end='')
else:
print('%d*%d=%d\t' %(cro,col,cro*col),end='')
col -=1
print('')
cro -=1
效果展示:
第四种 :
cro = 1
while cro <= 9 :
col = 9
while col > 0 :
if cro < col :
print(' \t' ,end='')
else:
print('%d*%d=%d\t' %(cro,col,cro*col),end='')
col -=1
print('')
cro +=
效果展示