python学习day5 常量 运算符补充 流程控制基础

1。常量

  值不会改变的量

  python中没有特别的语法定义常量,一般约定用全大写字母命名常量,比如圆周率pi

 2.预算符补充

  2.1算数运算符

  print(10/3)除法运算

  print(10//3)整除运算

  print(10**2)平方运算

  2.2赋值运算

  age=18

  age+=1

  age/=3

  age **= 2

  2.3交叉赋值

  x=10

  y=20

  x,y=y,x(底层实现z=y,y=x,x=z)

  2.4链式赋值

  x=10

  y=10

  z=10

  x=y=z=10

  print(id(x))

  print(id(y))

  print(id(z))

  2.5解压赋值

  l=[1,2,3,4,5]

  a,b,c,d,e=l

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

  另外_下划线代表的变量表示不会用到

  所以解压赋值也可以

  a,*_=l(取出第一个值)

  a,*_,b=l(取出首个元素和最后一个元素)

  *_,a,b=l(取出最后两个元素)

3.流程控制之if判断语句

 3.1只有一个条件

  if 条件:

    代码1

    代码2

    代码3

    。。。

3.2除了一个条件成立之外,其他情况运行else内代码

  if 条件1:

    代码1

    代码2

    代码3

    。。。

  else:

    代码1

    代码2

3.3有多个条件时,从上至下,第一个条件不成立时候去判断下一个条件

  if 条件1:

    代码1

    代码2

  elif 条件2:

    代码1

    代码2

  elif 条件3:

    代码1

    代码2

  else:

    代码1

    代码2

# score=input('your score>>: ')
# score=int(score)
# if score >=90:
# print('优秀')
# elif score >=80:
# print('良好')
# elif score >=70:
# print('普通')
# else:
# print('很差')

3.4 if嵌套

  if 条件1:

    if 条件2:

      代码1

      代码2

    else

      代码1

      代码2

  else 

    代码1

    代码2

age=18

sex_='male'

height=170

weight=60

is_sucess=True

if age >=18 and age <= 25 and sex == 'female' and height>=170 and weight<=60:

  print('表白‘)

  if  is_sucess:

    print('在一起')

  else:

    print('逗你玩的‘)

else:

  print('阿姨好')

4.流程控制之while循环

  4.1用条件判断控制while循环的语句是否运行

while 条件:

    代码1

    代码2

 count=1

 sum=0

 while count<=10:

  sum += count

  count+=1

  4.2用break结束本层循环

  while True:

    代码1

    代码2

    break

count=1 

sum=0

while True:

  sum+=count

  count+=1

  if count == 10:

    break

break:结束本层循环

continue:结束本次循环,开始下一次

4.3while continue

count=1

while count<=5:
if count ==3:
count+=1
continue
else:
print(count)
count+=1

4.4while else(只有while循环没有被break打断时才会运行else内的代码块)

count=1

while count<=5:

  if count==3:

    break

  print(count)

  count+=1

else:

  print('else子代码块只有在while循环没被break打断时运行‘)

原文地址:https://www.cnblogs.com/shanau2/p/9989524.html