If语句判断真假与运算符

if判断语句

语句结构

  if    条件:
    if语句块
  else:  
    else语句

运行原理

if语句的运行原理:根据条件由机器解析成True或者False来决定下一步要执行的语句。(楼主文化水平不高,只能说一些大白话,莫怪!)

条件真假判断

任何非零和非空对象都为真 解释为True
数字0、空对象和特殊对象None均为假 解释为False
比较和相等测试会应用到数据结构中,返回值为True或False

#第一种:
if 4 > 5 :
	print('我请你喝酒')
print('喝什么酒')

#第二种:
if 4 > 5:
	print('我请你喝酒')
else:
	print('喝什么酒')

elif嵌套语句

score = int(input("输入分数:"))

if score > 100:
    print("我擦,最高分才100...")
elif score >= 90:
    print("A")
elif score >= 60:
    print("C")
elif score >= 80:
    print("B")
elif score >= 40:
    print("D")
else:
    print("太笨了...E")

else语句

name = input('请输入名字:')
age = input('请输入年龄:')

if name == '小二':
	if age == '18':
		print(666)
	else:
		print(333)
else:
	print('错了....')

运算符介绍

1.算术运算符 + - * / (取商) %(取余数) **
2.赋值运算符
    num=100
    num = num + 90
3.成员关系运算符 in not in
4.比较运算符 > < >= < = == != <>
5.逻辑运算符 and or

运算符优先级顺序

优先级由上至下递增
Lambda #运算优先级最低
逻辑运算符: or
逻辑运算符: and
逻辑运算符:not
成员测试: in, not in
同一性测试: is, is not
比较: <,<=,>,>=,!=,==
按位或:
按位异或: ^
按位与: &
移位: << ,>>
加法与减法: + ,-
乘法、除法与取余: *, / ,%
正负号: +x,-x

逻辑运算符—and or

其一, 在不加括号时候, and优先级大于or
其二, x or y 的值只可能是x或y. x为真就是x, x为假就是y
其三, x and y 的值只可能是x或y. x为真就是y, x为假就是x

优先级

and的优先级大于or有括号的运算最优先

a = 50
if (a< 100 and a > 10 or (a >20 and a<100):
    print "a is true"
else:
    print 'a is false'

if语句结合逻辑运算练习

def func(x):
    if type(x) is str and x:  #参数是字符串
        for i in x:
            if i == ' ':
                return True
    elif x and type(x) is list or type(x) is tuple: #参数是列表或者元组
        for i in x:
            if not i:
                return True
    elif not x:
        return True
print(func([]))

学习链接

https://www.cnblogs.com/fighter007/p/8267615.html
http://www.runoob.com/python/python-operators.html#ysf4

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值