day 05小结

1.基本运算符

# + - * / ** #返回一个值

1.1 比较运算符

# = < <= == != #返回一个布尔值

1.2 赋值运算符

# =
x = 10

1.3逻辑运算符(把多个条件同时相加)

name = 'Bob'
height = 188
weight = 111
# and 左右两个条件都为True,则为True,否则为False
# print(name == 'Bob' and height == 188) #True
# print(name == 'BBO' and height == 188) #Flase

# or 左右两个条件只要有一个满足则为True,否则为False
# print(name == 'bob' and height == 233) #True
# print(naem == 'Bbo' and height == 232) #Flase

# not 否,如果条件为True,则为False,如果条件为False,则为True
# print(not name == 'Bob') # Flase

1.4身份运算符

每个变量值都有内存地址(身份)

y = 257
y = x 
z = 257
print(id(x)== (id(y)))
print(x is y) # is比较的是内存地址
print(x is not y) # is not判断是否不等于
print(not x is y)
print(id(x) == id(z))
print(x is z)

1.5位运算符

# 十进制 0,1,2,3,4,5,6,7,8,9 逢十进一
# 9892 == 2*10**0 + 9*10**1 + 8*10**2 + 9*10**3
print(2*10**0 + 9*10**1 + 8*10**2 + 9*10**3)
         
# 二进制 0,1 逢二进一
# 01000011 == 1*2**0 + 1*2**1 + 0 + 0 + 0 + 0 + 1*2**6 + 0
print(1*2**0 + 1*2**1 + 0 + 0 + 0 + 0 + 1*2**6 + 0)

1.6成员运算符

判断元素是否在容器内(字符串)

class_student_lt = ['s1','s2','s3']
print('s1' in class_student_lt) # True
print('s1' not in class_student_lt) # False
print('s4' in class_studetn_lt) # False

s = 'Bob'
print('n' in 'Bob')

1.7Python运算符优先级

# 《+ - * / : 先算* / 再算 + - ,就叫做优先级》
# 需要优先,就加括号,括号优先级最高(经验)

2.流程控制之if判断

流程判断 --> 控制 变量的变化的一个方向

2.1 if

if 条件:
    代码1
    代码2
    代码3
    ···
# 代码块(同一缩进级别的代码,例如代码1、代码2和代码3是相同缩进的代码,这三个代码组合在一起就是一个代码块,相同的代码块会自上而下运行)

cls = 'human'
grander = 'female'
age = 18

if cls == 'human' and gender == 'female' and age > 16 and age <22:
    print('开始表白')
print('end...')

2.2 if...else

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

if...else表示if成立代码会干什么,else不成立会干什么

cls = 'human'
gander = 'female'
age = 38 

if cls == 'human' and gender == 'female' and age >16 and age <22:
    print('开始表白')
else:
    print('阿姨好')

2.3 if...elif...else

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

2.2 if的嵌套

cls = 'human'
gender = 'female'
age = 18
is_success = False

if cls == 'human' and gender == 'female' and age > 16 and age < 22:
    print('开始表白')
    if is_success:
        print('那我们一起走吧...')
    else:
        print('我逗你玩呢')
else:
    print('阿姨好')

3.while循环

1.语法

循环就是一个重复的过程,人类需要重复干一个活,机器也需要重复干一个活。这个时候就需要while循环,while又称为条件循环

while 条件:
    code 1
    code 2
    code 3
    ...

whlie True:
    print('*1'*100)
    print('*2'*100)
#实现ATM的密码输入的功能
while True:
    user_db = 'nick'
    pwd_db = '123'
    
    inp_user = input('username:')
    inp_pwd = input('password:')
    if inp_user == user_db and pwd_db == inp_pwd
        print('login successful')
    else:
        print('username or password error')

上述代码虽然实现了功能,但是用户名密码输入对了,他也会继续输入

3.2 while + break

break的意思是终止当前层的循环,执行其他代码

# while + break
count = 0
while 1:
    if cunt = 100
     break #break终止循环
    count += 1
    print(count)

3.3 while + continue

continue的意思是终止本次循环,直接进入下一次循环

n = 1
while n<10
    if n == 8
        # n += 1  # 如果注释这一行就会进入死循环
        continue
     print(n)
n +=1

continue不能加在循环体的最后一行执行代码,因为代码加上去毫无意义,如下图所示

ps: 注意是最后一步执行的代码,而不是最后一行

while True:
    if 条件1:
        code1
        code2 
        code3
        
    else:
        code1
        code2 
        code3
    continue

3.4 while 的循环的嵌套

ATM密码输入成功还需要进行一系列的命令操作,比如取款,比如转帐。并且在执行功能结束后

会退出命令操作的功能,即在功能出执行输出q会退出输出功能的while循环并且退出ATM程序

# 退出内层循环的while循环嵌套
while True:
    user_db = 'Bob'
    pwd_db = '000'
    
    inp_user = input('username:')
    inp_pwd = input('password:')
    
    if inp_user == user_db and pwd_db == inp_pwd:
        print('login successful')
        
        while True:
            cmd = input('请输入你需要的命令:')
            if cmd == 'q':
                break
            print(f'{cmd}功能执行')
     else:
        print('username or password error')
        
 print('退出了while循环')

退出双层循环的while循环嵌套你需要在每个循环中输入一个break

3.5 tag控制循环退出

tag = True
while tag:
    user_ db = 'bob'
    pwd_db = '000'
    
    inp_user = input('username:')
    inp_pwd = input('password:')
    
    if inp_user == user_db and pwd_db == inp_pwd
        print('login successful')
        
        while tag:
            cmd = input('请输入你要的指令')
            if cmd == 'q'
                tag = False
            print(f'{cmd}执行功能')
    else:
        print('username or password error')
print('推出了while循环')

3.6 while + else

while + else : else 会在while没有被break时才会执行else中的代码

# while+else
n = 1
while n < 3
    print(n)
    n += 1
else:
    print('else会在while没有被break时才会执行else中的代码')

猜你喜欢

转载自www.cnblogs.com/LZF-190903/p/11508466.html
今日推荐