python基础3

一、常量

常量指固定不变的量,python一般用纯大写字母定义常量。

二、基本运算符补充

1、算术运算

print(10/3print(10//3print(10*3)


2、赋值运算符

增量赋值:
age=18
age+=1  #age=age+1
print(age)

age=18
age/=3  #age=age/3
print(age)

age**=2  #age=age**2
2、交叉赋值
x=10
y=10

z=x
x=y
y=z
x,y=y,x
print(x,y)

3、链式赋值
x=10
y=x
z=y
x=y=z=10

print(id(x))
print(id(y))
print(id(z))

4、解压赋值
l=[1.2,2.2,3.3,4.4,5.5]
a=l[0]
b=l[1]
c=l[2]
d=l[3]
e=l[4]

a,b,c,d,e=l
a,b,c,d,e,f=l
a,b,c,d=l

print(a,b,c,d,e)

l=[1.2,2.2,3.3,4.4,5.5]
a,b,*_=l
print(a,b)
*_,a,b=l
print(a,b)

三、流程控制之if判断
语法1:
if 条件:
    代码1
    代码2
    代码3
 age_of_bk=30
print('start.....')

inp_age=input('>>>: ') #inp_age='18'
inp_age=int(inp_age)
if inp_age == age_of_bk:
    print('猜对了')

print('end.....')
语法2:
if 条件:
    代码1
    代码2
    代码3
    ...
else:
    代码1
    代码2
    代码3

age=38
gender='male'
is_beautiful=True
if age>=18 and age<=25 and gender=='feamale' and is_beautiful:
    print('开始表白')
else:
    print('阿姨好')
语法3:
if 条件1:
    代码1
    代码2
    代码3
    ...
elif 条件2:
    代码1
    代码2
    代码3
    ...
elif 条件3:
    代码1
    代码2
    代码3
    ...
else:
    代码1
    代码2
    代码3
    ...
如果:
        成绩>=90,那么:优秀

       如果成绩>=80且<90,那么:良好

       如果成绩>=70且<80,那么:普通

       其他情况:很差
score=input('your score>>:')
score=int(score)
if score>=90:
    print('优秀’)
 elif:score>=80:
    print('良好’)
elif:score>=70:
    print:('普通’)
else:
    print:('很差')
if 条件1:
    代码1
    代码2
    代码3
if 条件2:
    代码1
    代码2
    代码3
    语法4:
if 条件1:
    if 条件2:
        代码1
        代码2
        代码3
        ...
    代码2
    代码3
age=18
gander='female'
is_beautiful=True
is_successful=True
if age>=18 and age<=25 and gander=='female'and is_beautiful==True:
    print('开始表白')
    if is_successful:
        print('在一起')
    else:
        print('我逗你玩呢。。。')
else:
    print('阿姨好')
四、流程控制之循环
1、while条件循环
I基本语法
while条件
    代码1
    代码2
    代码3
示范:
name_of_bk='enge'
pwd_of_bk='123'
tag=True
while tag:
    inp_name=input('your name:')
    inp_pwd=input('your password:')
    if inp_name==name_of_bk and inp_pwd==pwd_of_bk:
        print('login successful')
        tag=False
    else:
        print('usernanme or password error')
    print('other code....')
II、while+brerk:break代表结束本层循环
示范
while True:
    print(1)
    break
    print(2)
    print(3)
    name_of_bk='egon'
pwd_of_bk='123'

while True:
    inp_name=input('your name>>: ')
    inp_pwd=input('your password>>: ')
    if inp_name == name_of_bk and inp_pwd == pwd_of_bk:
        print('login successful')
        break
    else:
        print('username or password error')

    print('other code......')
III、while+cuntinue:continue等于跳过本次循环

count=1
while count<6:
    if count==3:
        count+= 1
        continue
    print(count)
    count+=1
输错三次退出
name_of_bk='egon'
pwd_of_bk='123'

count=0
while True:
    if count == 3:
        print('输错的次数过多。。。')
        break
    inp_name=input('your name>>: ')
    inp_pwd=input('your password>>: ')
    if inp_name == name_of_bk and inp_pwd == pwd_of_bk:
        print('login successful')
        break
    else:
        print('username or password error')
        count+=1 #count=3 输错3次
IV:while + else
count=0
while True:
    if count == 10:
        break
    print(count)
    count+=1

else:
    print("else的子代块只有在while循环没有被break打断的情况下才会执行")
    name_of_bk='egon'
pwd_of_bk='123'

count=0
tag=True
while tag:
    if count == 3:
        print('输错的次数过多。。。')
        break
    inp_name=input('your name>>: ')
    inp_pwd=input('your password>>: ')
    if inp_name == name_of_bk and inp_pwd == pwd_of_bk:
        print('login successful')
        while tag:
            print("""
            0 退出
            1 购物
            2 支付
            3 查看购物
            """)
            cmd=input('>>>: ')
            if cmd == '0':
                tag=False
                continue
            if cmd == '1':
                print('购物。。。。。。。')
            elif cmd == '2':
                print('支付。。。。。')
            elif cmd == '3':
                print('查看购物车')
            else:
                print('输入错误的指令')
    else:
        print('username or password error')
        count+=1 #count=3 输错3次

原文地址:https://www.cnblogs.com/liushen1995/p/9989558.html