python 03

常量:

Python中常以大写的变量名来标注常量。

算数运算符补充:

10/3(除法)

10//3(除法取整数部分)

10**2(次方)

 赋值运算:

增量赋值

age+=1(age=age+1)

age/=3(age=age/3)

age**=2(age=age**2)

交叉赋值:

x=10
y=20
x,y=y,x
#temp=x
#x=y
#y=temp
print(x,y)

链式赋值

x=y=z=10

解压赋值

l=[1.2,2,3,4.4,5]

a,b,c,d,e=l
print(a,b,c,d,e)
a,b,*_=l
print(a,b)

if判断

语法一:

if 条件:

    代码1

    代码2

    代码3

    ...

age_of_bk=30
print('start.....')
inp_age=input('>>>:')
inp_age=int(inp_age)
if inp_age==age_of_bk
    print('猜对了')
print('猜对了')

print('end')

语法二:

if 条件:

    代码1

    代码2

    代码3

    ....

else

    

   代码1

    代码2

    代码3

    ....

age=38
gender='male'
is_beautiful=True

if age>=18 and age <=25 and gender='male' and is_beautiful
    print('开始表白')
else:
    print('阿姨好')

语法三

if 条件1:

    代码1

    代码2

    代码3

...

elif 条件2:

    代码1

    代码2

    代码3

    ....

elif 条件3:

    代码1

    代码2

    代码3

    ....

else:

    代码1

    代码2

    代码3

    ....

score=input('your score>>:')
score=int(score)
if score>=90:
    print('优秀')
elif score>=80:
    print('良好')
elif score>=70print('普通')
else:
    print('很差')
语法4:
if 条件1:
if 条件2:
代码1
代码2
代码3
...
代码2
代码3
age=18
gender='female'
is_beautiful=True
is_successful=True

if age >= 18 and age <= 25 and gender == 'female' and is_beautiful:
    print('开始表白。。。。')
    if is_successful:
        print('在一起')
    else:
        print('我逗你玩呢。。。')
else:
    print('阿姨好')

while循环:条件循环

while 条件:

    代码1

    代码2

    代码3

    ...

 name_of_bk='egon'
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('username or password error')

     print('other code...')
while+break: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......')
while + continue: continue代表结束本次循环,直接进入下一次
count=1
while count < 6:
    if count == 3:
        count+=1
        continue
    print(count)
    count+=1
while + else
count=0
while count <= 10:
    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次

 for循环

l=['a','b','c']
print(len(l))
i=0
while i<len(l):
    print(l[i])
    i+=1 #while循环 

l=['a','b','c']  
for item in l:
    print(item) #for循环

dic={'x':111,'y':222,'z':333}
for k in dic:#k='x'
    print(k,dic[k])
while循环 VS for循环
1.
while循环:称之为条件循环,循环的次数取决于条件何时为False
for循环:称之为迭代器循环,循环的次数取决于数据的包含的元素的个数
2.
for循环专门用来取值,在循环取值方面比while循环要强大,以后但凡
遇到循环取值的场景,就应该用for循环

# for+break
# names=['egon','kevin','alex','hulaoshi']
# for name in names:
#     if name == 'alex':break
#     print(name)



# for+continue
# names=['egon','kevin','alex','hulaoshi']
# for name in names:
#     if name == 'alex':continue
#     print(name)

# for+else
# names=['egon','kevin','alex','hulaoshi']
# for name in names:
#     # if name == 'alex':break
#     print(name)
# else:
#     print('=====>')

# for循环嵌套
# for i in range(3): #i=2
#     for j in range(2): #j=1
#         print(i,j) #2,1








原文地址:https://www.cnblogs.com/BestSkye/p/9989575.html