赞
踩
语法:变量 = 结果1 if 条件 else 结果2
作用:根据条件(True/False) 来决定返回结果1还是结果2。
1.作用:
用来创建一个生成一系列整数的可迭代对象(也叫整数序列生成器)。
2. 语法:
range(开始点,结束点,间隔)
3. 说明:
函数返回的可迭代对象可以用for取出其中的元素
返回的数字不包含结束点
开始点默认为0
间隔默认值为1
# 在控制台中分别录入4个数字,打印其中最大的数字 # 将第一个数字记在心里,然后与第二个比较 # 如果第二个大于心中的,则心中记录第二个 # 然后与第三个比较.... number01 = float(input("请输入第1个数字:")) number02 = float(input("请输入第2个数字:")) number03 = float(input("请输入第3个数字:")) number04 = float(input("请输入第4个数字:")) # 假设第一个是最大值 max_value = number01 # 以此与后面进行比较 if max_value < number02: # 发现更大的,则替换假设的。 max_value = number02 if max_value < number03: max_value = number03 if max_value < number04: max_value = number04 print(max_value)
运行结果如下所示:
方法一:(其他程序语言只能用这种方法)
score = int(input("请输入成绩:"))
if score >= 90 and score <= 100:
print("优秀")
elif score >= 80 and score < 90:
print("良好")
elif score >= 60 and score < 80:
print("及格")
elif score >= 0 and score < 60:
print("不及格")
else:
print("输入有误")
运行结果如下所示:
方法二(python语言特有的方法)
score = int(input("请输入成绩:"))
if 90 <= score <= 100:
print("优秀")
elif 80 <= score < 90:
print("良好")
elif 60 <= score < 80:
print("及格")
elif 0 <= score < 60:
print("不及格")
else:
print("输入有误")
运行结果如下所示:
方法三:if语句的简便用法
score = int(input("请输入成绩:"))
if score > 100 or score < 0:
print("输入有误")
elif 90 <= score:
print("优秀")
elif 80 <= score:
print("良好")
elif 60 <= score:
print("及格")
else:
print("不及格")
运行结果如下所示:
# 在控制台中获取一个月份,打印天数,或者提示"输入有误".
# 1 3 5 7 8 10 12 --> 31天
# 4 6 9 11 --> 30天
# 2 --> 28天
month = int(input("请输入月份:"))
if month < 1 or month > 12:
print("输入有误")
elif month == 2:
print("28天")
elif month == 4 or month == 6 or month == 9 or month == 11:
print("30天")
else:
print("31天")
运行结果如下所示:
# 在控制台中获取一个整数,如果是偶数为变量state赋值"偶数",否则赋值"奇数"
number = int(input("请输入整数:"))
# if number % 2 == 1:
# state = "奇数"
# else:
# state = "偶数"
# if number % 2:
# state = "奇数"
# else:
# state = "偶数"
state = "奇数" if number % 2 else "偶数"
print(state)
运行结果如下所示:
# 在控制台中录入一个年份,如果是闰年,给变量day赋值29,否则赋值28. year = int(input("请输入年份:")) # result = year % 4 == 0 and year % 100 != 0 or year % 400 == 0 # if result: # day = 29 # else: # day = 28 # if year % 4 == 0 and year % 100 != 0 or year % 400 == 0: # day = 29 # else: # day = 28 # 代码可读性 很差 # if not year % 4 and year % 100 or not year % 400: # if year % 4 == 0 and year % 100 != 0 or year % 400 == 0: # day = 29 # else: # day = 28 day = 29 if year % 4 == 0 and year % 100 != 0 or year % 400 == 0 else 28 print(day)
运行结果如下所示:
# 使下列代码循环执行,按e键退出。
while True:
season = input("请输入季度:")
if season == "春":
print("1月2月3月")
elif season == "夏":
print("4月5月6月")
elif season == "秋":
print("7月8月9月")
elif season == "冬":
print("10月11月12月")
if input("输入e键退出:") == "e":
break
运行结果如下所示:
# 1:在控制台中输出0 1 2 3 4 5 # 2:在控制台中输出2 3 4 5 6 7 # 3:在控制台中输出0 2 4 6 # count = 0 # while count < 6: # print(count) # count += 1 # count = 2 # while count < 8: # print(count) # count += 1 count = 0 while count <= 6: print(count) count += 2
运行结果如下所示:
""" 真值表达式 if 条件表达式: 语句 本质就是使用bool函数操作数据 """ # 1. 真值表达式 if "a": # if bool("a"): print("真值") str_input = input("请输入:") if str_input: print("输入的字符串不是空的") # 2. 条件表达式:有选择性的为变量进行赋值 # sex = None # if input("请输入性别:") == "男": # sex = 1 # else: # sex = 0 # print(sex) sex = 1 if input("请输入性别:") == "男" else 0 print(sex)
运行结果如下所示:
"""
循环语句
while 条件表达式:
循环体
"""
# 死循环:循环条件永远是满足的。
while True:
usd = int(input("请输入美元:"))
print(usd * 6.9)
if input("输入q键退出:"):#只要输入数据,if条件表达式就为真
break # 退出循环体
运行结果如下所示:
"""
while 计数
"""
# 需求:执行三次
count = 0
while count < 3: # 0 1 2
count += 1
usd = int(input("请输入美元:"))
print(usd * 6.9)
运行结果如下所示:
# 在控制台中获取月份,显示季度,或者提示月份错误.
month = int(input("请输入月份:"))
if month < 1 or month > 12:
print("月份错误")
elif month >= 10:
print("冬天")
elif month >= 7:
print("秋天")
elif month >= 4:
print("夏天")
else:
print("春天")
运行结果如下所示:
"""在控制台中获取年龄, 如果小于0岁,打印输入有误 如果一个人的年龄小于2岁,就打印一条消息,指出他是婴儿。 如果一个人的年龄为2(含)~13岁,就打印一条消息,指出他是儿童。 如果一个人的年龄为13(含)~20岁,就打印一条消息,指出他是青少年。 如果一个人的年龄为20(含)~65岁,就打印一条消息,指出他是成年人。 如果一个人的年龄超过65(含)岁~150岁,就打印一条消息,指出他是老年人。 150岁以上,打印"那不可能" """ age = int(input("请输入年龄:")) if age < 0: print("输入有误") elif age < 2: print("婴儿") elif age < 13: print("儿童") elif age < 20: print("青少年") elif age < 65: print("成年人") elif age < 150: print("老年人") else: print("那是不可能的")
运行结果如下所示:
""" 根据身高体重,参照BMI,返回身体状况. BMI:用体重千克数除以身高米数的平方得出的数字 中国参考标准 体重过低BMI<18.5 正常范围18.5≤BMI<24 超重24≤BMI<28 I度肥胖28≤BMI<30 II度肥胖30≤BMI<40 Ⅲ度肥胖BMI≥40.0 """ height = float(input("请输入身高:")) weight = float(input("请输入体重:")) BMI = weight / height ** 2 if BMI < 18.5: print("体重过低") elif BMI < 24: print("正常范围") elif BMI < 28: print("超重") elif BMI < 30: print("I度肥胖") elif BMI < 40: print("II度肥胖") else: print("Ⅲ度肥胖")
运行结果如下所示:
""" 猜数字2.0: 最多猜3次,如果猜对提示"猜对了,总共猜了?次",如果超过次数,提示"游戏结束". """ import random random_number = random.randint(1, 100) print(random_number) count = 0 while count < 3: # 三次以内 count += 1 input_number = int(input("请输入数字:")) if input_number > random_number: print("大了") elif input_number < random_number: print("小了") else: print("猜对了,总共猜了" + str(count) + "次") break# 退出循环体,不会执行else语句。 else:# while的条件不满足 # 三次以外 print("失败")
运行结果如下所示:
""" for: 适合执行预定次数。 while:适合根据条件循环执行。 """ # for 变量 in 可迭代对象: # 循环体 str01 = "我叫苏大强!" # item 存储的是字符串中每个字符的地址 for item in str01: print(item) print(id(item)) # 整数生成器: range(开始值,结束值,间隔) # for + range : 更善于执行预定次数。 for item in range(5):#01234 print(item) # 需求:折纸10次 thickness = 0.0001 for item in range(10): thickness*=2 print(thickness)
运行结果如下所示:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。