文章目录
1.使用if判断闰年
闰年要满足以下几个要求:
- 能被4整除且不能被100整除
- 或者可以被400整除
代码如下:
# _*_coding:utf-8_*_
# @Time :9/5/20 9:00 PM
# @Author :Song
# @Filename :判断闰年.py
# @Software :PyCharm
year = int(input("Year:"))
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print("%d是闰年" % (year))
else:
print("%d不是闰年" % (year))
执行结果:
- 输入闰年:
/usr/local/python3/bin/python3.6 /home/kiosk/PycharmProjects/01/2020.9.5/判断闰年.py
Year:2000
2000是闰年
Process finished with exit code 0
- 输入平年:
/usr/local/python3/bin/python3.6 /home/kiosk/PycharmProjects/01/2020.9.5/判断闰年.py
Year:2001
2001不是闰年
Process finished with exit code 0
2.输入年月判断这个月有多少天
需要满足:
- 判断闰年平年决定2月几天
- 判断大月小月判断30天或31天
代码如下:
year = int(input("Year:"))
month = int(input("Month:"))
if month == 1 or month == 3 or month == 5 or month == 7 or month == 8 or month == 10 or month == 12:
print("%d年%d月有31天" % (year, month))
elif month == 2:
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print("%d年%d月有29天" % (year, month))
else:
print("%d年%d月有28天" % (year, month))
else:
print("%d年%d月有30天" % (year, month))
测试如下:
- 平年小月
Year:2001
Month:6
2001年6月有30天
Process finished with exit code 0
- 闰年二月:
Year:2020
Month:2
2020年2月有29天
Process finished with exit code 0
3.for循环
和for循环配合使用range( )函数使用规则如下:
[kiosk@foundation5 ~]$ python
Python 2.7.5 (default, Aug 2 2016, 04:20:16)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> range(9) # 9个数依次从0到9
[0, 1, 2, 3, 4, 5, 6, 7, 8]
>>> range(1,9) # 从1到9,共8个数
[1, 2, 3, 4, 5, 6, 7, 8]
>>> range(0,10,2) # 从0-9,共10个数中的偶数
[0, 2, 4, 6, 8]
>>> range(1,10,2) # 从1-9,共9个数中的奇数
[1, 3, 5, 7, 9]
1.1 求和1+……+100
使用range( )这个python中的内置函数:
sum = 0
for i in range(1, 101):
sum = sum + i
# sum += i 这种方式更好
print("从1+……+100的和为%d" % (sum))
执行结果:
从1+……+100的和为5050
Process finished with exit code 0
1.2 输入一个数字求他的阶乘
和加法一样,只不过这个是使用的乘法:
# _*_coding:utf-8_*_
# @Time :9/5/20 9:33 PM
# @Author :Song
# @Filename :阶乘.py
# @Software :PyCharm
num = int(input("num:"))
sum = 1
for i in range(1, num + 1):
sum *= i
print("%d的阶乘为%d" % (num, sum))
测试结果:
num:4
4的阶乘为24
Process finished with exit code 0
1.3 登录系统的入口雏形
要求:
- 只能输入3次密码
- 三次出错,等待100秒再次输入
代码如下:
for i in range(0, 3):
name = input("用户名:")
passwd = input("密码:")
if name == "root" and passwd == "python":
print("登录成功")
else:
print("登录失败")
print("你还有%d次机会" % (2 - i))
测试结果:
用户名:root
密码:song
登录失败
你还有2次机会
用户名:root
密码:centos
登录失败
你还有1次机会
用户名:root
密码:python
登录成功
Process finished with exit code 0
4. 理解break、continue、exit
1)break:跳出整个循环
- 代码如下:
for i in range(10):
if i == 5 :
break
print(i)
print("看我!")
- 测试:
0
1
2
3
4
看我!
Process finished with exit code 00
1
2
3
4
看我!
Process finished with exit code 0
2)continue跳出本次循环:
- 代码如下
for i in range(10):
if i == 5 :
continue
print(i)
print("看我!")
- 测试结果:跳过了i=5的内一次
0
1
2
3
4
6
7
8
9
看我!
Process finished with exit code 0
3)exit:退出程序
- 代码如下:
for i in range(10):
if i == 5 :
exit()
print(i)
print("看我!")
- 测试结果:i=5时退出程序,所以不回输出循环外的“看我”
0
1
2
3
4
Process finished with exit code 0
5.实现命令行提示符
解析shell命令,可以使用第三方模块import os
- 代码如下:
# _*_coding:utf-8_*_
# @Time :9/5/20 10:00 PM
# @Author :Song
# @Filename :命令行提示符.py
# @Software :PyCharm
for i in range(1000):
cmd = input("[root@server1 ~]# ")
if cmd:
if cmd == "exit" :
print("logout")
break
else:
print("run %s" %( cmd))
os.system(cmd)
else:
continue
- 测试结果:
[root@server1 ~]#
[root@server1 ~]# ls
run ls
判断闰年.py
命令行提示符.py
求和.py
测试.py
登录入口.py
阶乘.py
[root@server1 ~]# exit
logout
Process finished with exit code 0