Python编程学习【2】 1. 比较运算符 2. bool运算符 3. 位移运算 4. 运算符优先级 5. 对象的bool值 6. 选择结构 if

  1. 比较运算符
  2. bool运算符
  3. 位移运算
  4. 运算符优先级
  5. 对象的bool值
  6. 选择结构 if
#study 2022 9/29
#*********************比较运算符 > <  >=   <=   ==   !=
#运算符结果为bool值
a=10
b=10
print(a==b)       #ture
print(a is b)    #ture     a和b的id是相同的

list1=[11,22,33,44]
list2=[11,22,33,44]
print(list1==list2)   #value    -->ture
print((list1 is list2))   #id  -->false
print(id(list1))
print(id(list2))
print(a is not b)   #false  a的id与b是不相等的
print(list1 is not list2)  # ture

#************************bool运算符
#and    or    not   与或非
#in      not in
s='hellowworld'
print('w'in s)   # 看 字符'w'是否在字符串s中,在返回ture # 否者返回false
print('k'in s)   #false

print('******************************位运算符****************')
#按位与 &    按位或|     左移位 <<   右移位>>
print(4&8)    #按位与,同为1时结果为1
print(4|8)    #按位或,全为0时为0
print(4<<2)   #左移动两位 ,移动一位相当于乘以2
print(4>>2)   #右移两位   ,移动一位相当于除以2

print('****************************************运算符优先级**********')
#先算术(** )( * / // % )( + -)
#后位运算 (<<  >>) (& )  (|)
#比较运算 (< > >=   <=  ==   !=)
#bool运算  and  or
#等于  =


#任何简单或复杂的算法都可以由 顺序结构  选择结构与 循环结构来组成


#************************************对象的布尔值
#python一切皆对象,所有对象都有一个bool值,
#一些空的东西布尔值为 false
print(bool(False))
print(bool(0))
print(bool(0.0))
print(bool(None))
print(bool(''))
print(bool(""))
print(bool([]))     #空列表
print(bool(list()))  #空列表
print(bool(()))      #空元组
print(bool(tuple()))   #空元组
print(bool({}))           #空字典
print(bool(dict()))         #空字典
print(bool(set()))             #空集合
#除以上的为false  其他对象的布尔值都为false


print("**********************************选择结构")
#     if   条件表达式1   :
#           条件执行体1
#     elif   条件表达式2:
#           条件执行体2
#     else:
#           条件执行体3
my=input('are you happy today')
if my=='yes' :
    print('me too')
else:
    print('be happy bro')

score=int(input('input your score'))
if 90<=score<=100 :                     # if score>=90 and score<=100
    print('A')
elif 80<=score<90:
    print('B')


print('******************************嵌套分支结构')
# if 条件表达式1 :
#        if 内层条件表达式:
#              内层条件执行体1
#        else ;
#              内层条件执行体2
# else
#     条件执行体


answer=input('do you like lol?,input no or yes')
legent=input(' your favorite legent?')
if answer=='yes' :
    if answer=='yasuo':
        print("you like  wind bro")
    else :
        print('yasou is the happiest legent,you can try')
else :
    print('axiba')


#快速分支结构
num1=int(input('please input the first number'))
num2=int(input('please input the second number'))
print(str(num1)+'大于等于'+str(num2) if num1>=num1 else str(num1)+'小于'+str(num2) )

print('***********************************pass的使用')
#pass 会占用一个语句位置,需要写语句但没有想清楚怎么写,用pass占一个位置
answer=input('pass')
if answer=='pass':
    pass
else:
    pass

print('***********************************对象直接作为条件判断')
age=int(input('input your age'))
if age  :                                 #年龄直接作为条件判断,根据对象的bool值,输入不为bool为false的值时都判断为true
    print(age)
else:
    print('age is:',age)

你可能感兴趣的:(Python,python,学习,开发语言)