#条件判断
#布尔变量条件判断方法
not True # False
not False #True
True and False #False
True or False #True
True == False #False
True != False #True
True > False #True
True >= False #True
True < False #False
True <= False #False
True is False #False
True is True #True
False < True <=True #True
False < True < True #False
#数字条件判断
0 and 2 # 0
0 or -2 # -2
not 2  #False
not 0  #True
not -1 #False
-2 < 3 < 2 #False
0 == False #True
1 == True #true
2 == True #False
0 is False #False
1 is True #False

#python小坑之逻辑判断
# == != > >= < <=  计算的时候是用数值进行计算,但是输出结果是一个布尔值
# not 计算用布尔,结果输出布尔
# is 不计算,只用来判断
# 链式判断大小关系,结果与数学上保持一致
# and从左向右找零或者False,找到立即返回,没找到则返回最后一个
# or 从左往右找非零或者True,找到立即返回,没找到返回最后一个

# and
1 and 2 and 3 # 3
1 and True and 3 # 3
1 and 2 and True # True
1 and 0 and 3 # 0
1 and False and 3 # False
0 and False and 3 # False
False and 0 and 3 # False

# or
3 or 2 or 1 # 3
0 or 2 or 1 # 2
False or 0 or 1 #1
0 or False or -3#-3
0 or False or False #False
0 or False or 0 # 0

# if 语句
age = input('please enter your age:')
age = int(age)
if age >= 18:
     print('your age is:',age)
     print('adult')
elif age >= 0:
     print('your age is:',age)
     print('teenager')
else:
     print('input error')

# 循环语句
# for
xList = ['a','b','c','d']
s = ''
for x in xList:
     s += x
     print(x)
     print(s)

a = list(range(5))
b = list(range(1,5))
c = list(range(1,5,2))
print(a)
print(b)
print(c)

L = list(range(101))
print(len(L))
print(L)
s = 0;
for i in L:
     s += i
print(s)

# while 循环 (只要条件满足则持续操作)
n = 1
s = 0;
while n<101:
     s += n
     n += 1


print(s)

a = 1
b = 0;
while True:
     b += a
     a += 1
     if a > 100:
         break
print(b)

c = 0
d = 0
while True:
     c += 1
     if c%2 == 1:
         continue
     if c>101:
         break
     d += c
print(d)

#内置函数
abs(-2)
min([1,2,3])
max(['s','f','c'])
int(True)
float(-2)

#自定义函数
def my_fun1(a,b):
     if a > b:
         return a
     else:
         return b

#函数可以返回多个值
def my_func2(a,b):
     if a > b:
         return a,b
     else:
         return b,a
    
#函数的默认参数
def my_funMax(a,b=0):
     if a > b:
         return a
     else:
         return b  
    
if __name__ == '__main__':
#    assert my_fun1(1,2)==2
#    assert my_fun1(4,3)==4
#    assert my_func3(4,3)==(4,3)
#    assert my_func3(3,4)==(4,3)
     assert my_funMax(3,4)==4
     assert my_funMax(3)==3

    print('ok')

内容来源于网络如有侵权请私信删除
你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!