身份运算符/流程控制if

身份运算符

in

  • 判断一个字符或者字符串是否存在于一个大字符串/元素中/key列表中

print("e" in "hello eogn")

True

print('z' in ['c','y','z'])

True

print('k1' in {'k1':666,'k2':01})
print(01 in {'k1':666,'k2':01})

True
False

  

not in

判断一个字符或者字符串是否存在于一个大字符串中/元素/key

 

print("e" not in "hello eogn")

False



print('z' not in ['c','y','z'])

False

print('k1' in {'k1':666,'k2':01})
print(01 in {'k1':666,'k2':01})

False
True

  

 

身份运算符  is / not is

  • is:引用的是同一个对象则返回 True,否则返回 False
  • not is:判断两个标识符是不是引用自不同对象,如果引用的不是同一个对象则返回结果 True,否则返回 False
x = 1
y = 1
print(x is y)

True

  

x = 1
y = 1
print(x is not y)

False

  

    if  的语法。

if的缩进

if条件:

     代码1

     if条件2:

  

if方法一:

    代码1

    代码2

    代码3

  

age = 60
is_beautiful = True
star = '水瓶座'

if age > 16 and age < 20 and is_beautiful and star == '水瓶座':
    print('我喜欢你,我们在一起吧!')

  

 

方法二:

if 条件:
     代码1
  else:
     代码1

  

age = 60
is_beautiful = True
star = '水瓶座'

if age > 16 and age < 20 and is_beautiful and star == '水瓶座':
    print('我喜欢你,我们在一起吧!')
else:
    print('阿姨好,我逗你玩儿呢,深藏功与名')
print('其他代码')

 

 

方法3:

  if 条件1:
    代码1
    代码2
     代码3
  elif 条件2:
    代码1
    代码2
     代码3
  elif 条件2:
    代码1
    代码2
    代码3
  else 条件3:
    代码1

score = input('请输入您的成绩:')
score = int(score)

if score >= 90:
    print('优秀啊,小伙子!')
elif score >= 80:
    print('还可以吧!')
elif score >= 70:
    print('要努力了呀,小伙子')
elif score >= 60:
    print('准备叫家长吧!')
else:
    print("滚啊!!!"

  

posted @ 2020-03-07 12:22  清轩挽长风  阅读(121)  评论(0编辑  收藏  举报