自学Python之路html
if语句是用来进行判断的,其使用格式以下:
python
if 要判断的条件: 条件成立时,要作的事情
price = input("请问这苹果多钱一斤:") price_num = int(price) if price_num> 5: print('纳尼,竟然', price) print('简直太贵了!') print("货比三家,再转转。")
单分支判断语句 if 条件: 内容1 内容2 else: 内容3 内容4
多分支判断语句 if 条件: 内容1 内容2 elif 条件 内容3 elif 条件 内容4 else: 内容5
python中的比较运算符以下表: bash
运算符 | 描述 | 示例 |
---|---|---|
== | 检查两个操做数的值是否相等,若是是则条件变为真。 | 如a=3,b=3则(a == b) 为 true. |
!= | 检查两个操做数的值是否相等,若是值不相等,则条件变为真。 | 如a=1,b=3则(a != b) 为 true. |
<> | 检查两个操做数的值是否相等,若是值不相等,则条件变为真。 | 如a=1,b=3则(a <> b) 为 true。这个相似于 != 运算符 |
> | 检查左操做数的值是否大于右操做数的值,若是是,则条件成立。 | 如a=7,b=3则(a > b) 为 true. |
< | 检查左操做数的值是否小于右操做数的值,若是是,则条件成立。 | 如a=7,b=3则(a < b) 为 false. |
>= | 检查左操做数的值是否大于或等于右操做数的值,若是是,则条件成立。 | 如a=3,b=3则(a >= b) 为 true. |
<= | 检查左操做数的值是否小于或等于右操做数的值,若是是,则条件成立。 | 如a=3,b=3则(a <= b) 为 true. |
python中的逻辑运算符以下表: spa
运算符 | 逻辑表达式 | 描述 | 实例 |
---|---|---|---|
and | x and y | 布尔"与" - 若是 x 为 False,x and y 返回 False,不然它返回 y 的计算值。 | (a and b) 返回 20。 |
or | x or y | 布尔"或" - 若是 x 是 True,它返回 True,不然它返回 y 的计算值。 | (a or b) 返回 10。 |
not | not x | 布尔"非" - 若是 x 为 True,返回 False 。若是 x 为 False,它返回 True。 | not(a and b) 返回 False |
python中的赋值运算符以下表: 3d
运算符 | 描述 | 实例 |
---|---|---|
= | 简单的赋值运算符 | c = a + b 将 a + b 的运算结果赋值为 c |
+= | 加法赋值运算符 | c += a 等效于 c = c + a |
-= | 减法赋值运算符 | c -= a 等效于 c = c - a |
*= | 乘法赋值运算符 | c *= a 等效于 c = c * a |
/= | 除法赋值运算符 | c /= a 等效于 c = c / a |
%= | 取模赋值运算符 | c %= a 等效于 c = c % a |
**= | 幂赋值运算符 | c **= a 等效于 c = c ** a |
//= | 取整除赋值运算符 | c //= a 等效于 c = c // a |
举例1:code
price = input("请问这苹果多钱一斤:") price_num = int(price) if price_num> 5: print("纳尼,竟然", price) print("简直太贵了!") print("货比三家,再转转。") else: print(price,"还算地道") print("给我来二斤")
举例2:htm
score = input("请输入您的分数:") score_num = int(score) if score_num>=90 and score_num<=100: print ("本次考试,等级为A") elif score_num>=80 and score_num<90: print ("本次考试,等级为B") elif score_num>=70 and score_num<80: print ("本次考试,等级为C") elif score_num>=60 and score_num<70: print ("本次考试,等级为D") elif score_num>=0 and score_num<60: print ("本次考试,等级为E")
举例3:blog
name = input("please input username:") pwd = input("please input password:") if name == "carlos" and pwd == "123": # 当username为carlos,且password为123时才能输出yes print("yes") elif name == "amy" or pwd == "456": # 当username为amy,或password为456时输出yes print("yes") else: print("no")
举例4:get
color = input("你白么:") money = input("请输入你的财产:") beautiful = input("你美么:") if color == "白" and money >= "100000" and beautiful == "美": print("哇...白富美") else: print("滚蛋")
举例5:input
a = input("请输入一个整数:") a_num=int(a) if a_num>0 and a_num<=50: print("a在0到50之间") a = input("请输入一个整数:") if not(a_num<0 and a_num>=50): print("a在0到50之间")
if 条件1: 知足条件1 作的事情1 知足条件1 作的事情2 ...(省略)... if 条件2: 知足条件2 作的事情1 知足条件2 作的事情2 ...(省略)...
chePiao = 1 # 用1表明有车票,0表明没有车票 daoLenght = 9 # 刀子的长度,单位为cm if chePiao == 1: print ("有车票,能够进站") if daoLenght < 10: print ("经过安检") print ("终于能够见到Ta了,美滋滋") else: print("没有经过安检") print("刀子的长度超过规定,等待警察处理..") else: print ("没有车票,不能进站") print ("亲爱的,那就下次见了,一票难求啊~~)