day05课堂小结

基本运算符

算术运算符

加+、减-、乘*、除/、取整//、取余%、**幂

比较运算符

等于 ==、不等于 !=、不等于 <>、大于 >、小于 <、大于等于 >=、小于等于 <=

赋值运算符

c=a+b 将 a+b的运算结果赋值给 c,c += a 等效于 c=c+a

c* *=a等效于c=c**a

逻辑运算符

and、or、not

​ 与、或、非

身份运算符

  • is is是判断两个标识符是不是引用来自同一个对象,x is y,类似id(x) ==id(y)
  • is not 类似于 id(a) !=id(b)

is和==的区别: is用于判断两个变量引用对象是否为同一个(内存空间), ==用于判断引用变量的值是否相等。

位运算符

按位运算符是把数字当作二进制来进行计算的。

成员运算符

在序列、列表、元组里找值。

in/not in

流程控制之if判断

if

if 条件:
    代码1
    代码2
    代码3
    ...
# 注意缩进
cls = 'human'
gender = 'female'
age = 18

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

开始表白

end...

if...else

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

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

cls = 'human'
gender = 'female'
age = 38

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

阿姨好

if...elif...else

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

if...elif..else表示if条件1成立干什么,elif条件2成立什么干什么,elif条件3成立干什么,elif...否则干什么。

cls = 'human'
gender = 'female'
age = 28

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

考虑下

if的嵌套

# if的嵌套
cls = 'human'
gender = 'female'
age = 18
is_success = False

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

流程控制之while循环

语法

​ 循环就是一个重复的过程,计算机重复干一个活,while循环又称为条件循环。

while 条件
    code 1
    code 2
    code 3
    ...
   
while 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')

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

while + break

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

while True:
    print('1')
    print('2')
    break
    print('3')

1
2

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')
        break
     else:
        print('username or password error')
        
print('退出了while循环')

username:nick
password: 123
login successful
退出了while循环

while + continue

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

n = 1
while n < 4:
    print(n)
    n += 1

1
2
3

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

continue不能加在循环体的最后一步执行代码,因为代码加上去毫无意义,如下所示的continue所在的位置就是毫无意义的。ps:(注意是最后一步执行的代码,而不是最后一行)

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

    continue

while循环的嵌套

ATM密码输入成功还需要进行一系列的命令操作,比如取款,比如转账。并且在执行功能结束后会退出命令操作的功能,即在功能出执行输入q会退出输出功能的while循环并且退出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')

        while True:
            cmd = input('请输入你需要的命令:')
            if cmd == 'q':
                break
            print(f'{cmd} 功能执行')
        break
    else:
        print('username or password error')

print('退出了while循环')
# tag控制循环退出
tag = True
while tag:
    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')

        while tag:
            cmd = input('请输入你需要的命令:')
            if cmd == 'q':
                tag = False
            print(f'{cmd} 功能执行')
    else:
        print('username or password error')

print('退出了while循环')

while + else

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

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

猜你喜欢

转载自www.cnblogs.com/shin09/p/11508868.html