if判断语句判断语句 for循环循环 while循环循环
判断语句判断语句
if语句语句
if语句语法结构
if语句示例解析
条件表达式
1.标准if条件语句的语法
if expression:
if suite
else:
else_ suite
如果表达式的值非0或者为布尔值True,则代码组if_ suite被执行;否则就去执行else_ suite
代码组是一个python术语 ,它由一条或多条语组成,表示一个子代码块
2.if语句示例解析
只要表达式数字为非零值即为True
>>> if 10:
print('Yes')
Yes
空字符串、空列表、空元组,空字典的值均为False
>>> if "":
print('Yes')
else:
print('No')
No
3.条件表达式
Python在很长的一-段时间里没有条件表达式(C ? X:Y) ,或称三元运算符,因为范罗萨姆一-直拒绝加入这样的功能
从Python 2.5集成的语法确定为: XifC else Y
>>> x,y = 3,4
>>> smaller = xifx >> print smaller
3
# 练习
default_username="root"
default_password="123456"
username = input("localhost login: ")
password = input("password: ")
# if判断语句
if default_username == username:
if default_password == password:
print("welcome to", username)
else:
print("password error !")
else:
print("username error")
三元表达式三元表达式
if 条件 {
真-->执行
} else {
假-->执行
}
条件?真-->执行:假-->执行
真-->执行 if 条件 else 假-->执行
# 练习
x = input("id: ")
if int(x) > 10:
print('大')
else:
print('小')
# 练习
x = input("id: ")
print('大' if int(x) > 10 else '小')
# 练习
x = input("id: ")
aa = "yes" if int(x) > 50 else "on"
print(aa)
扩展扩展if语句语句
扩展if语句结构
扩展if语句示例解析
评论0
最新资源