Python 严格区分大小写,且print()要加括号
1. 数据类型以及变量:
- str:字符串
- int:整数
- float:浮点数
- none:空
- Boolean: True 和False
命名: 数字、字母、下划线(其余符号不能用)
2.运算符:
- + - * /(浮点除) % //(取整除)**(幂)
- += -= *= /= %=
- >= <= == != 判断符
- and or not 与或非逻辑符
- %d %s %r 常用格式化符
3.分支结构
if 判断,多重分支elif
if boolean:
pass
elif boolean:
pass
else:
pass
4.循环
for in循环
for i in object:
print(i)
不按下标循环, 如需下标
for i,value in enumerate(object):
print(i,value)
while循环
while True:
pass
小练习:请输入成绩:如果大于等于90,输出“优秀”,如果大于等于70,输出“良好”,
如果大于等于60,输出“及格”,否则输出“不及格”
grades = int(input("考了几分?"));
if grades>=90:
print("优秀");
elif grades >=70:
print("良好");
elif grades>= 60:
print("及格");
else:
print("不及格");
小练习2:用户输入月份,返回该月天数,如果是二月份,再次输入年份,判断天数
month = int(input("请输入月份?"))
n1 = [1,3,5,7,8,10,12]
def month2():
year = int(input("请输入年份?"))
if year%4 == 0 and year%100 != 0 or year%400 ==0:
print("29天")
else:
print("28天")
if month == 2:
month2()
elif month in n1:
print("31天")
else:
print("30天")