常量

常量即指不变的量.在python中没有一个专门 的语法代表常量,程序员约定俗成地用变量名全部被大写代表常量.

AGE_OF_OLDBOY = 56

基础运算符补充

1.算术运算

加减乘除+ - * /

% 取模(返回除法的余数) 如 20%10=0

** 幂

// 取整数(返回商的整数部分) 如 9//2=4

2.赋值运算

  • 增量赋值

      age=18
    age+=1 #age=age+1

    age=18
    age /=3   #age=age/3

    age**2    #age =age**2
  • 交叉赋值

      x=1
    y=2

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

    x,y=y,x
    print(x,y)
  • 链式赋值

      x=10
    y=x
    z=y
    x=y=z=10
  • 解压赋值

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

    #将数据从l中解压出来(打印出l中的数据)
    a=l[0]
    b=l[1]
    c=l[2]
    d=l[3]
    e=l[4] #这种方法繁琐

    a,b,c,d,e=l #更加方便

    l=[1,2,3,4,5]
    a,b,*_=l # *_代表了列表中a,b之外的其他元素
    a,*_,b=l
    *_,a,b=l

流程控制之if判断

语法一:

if 条件: 缩进代码块

  age_of_bk=30
print('star....')
inp_age=input('please input your age:')
inp_age=int(inp_age)    #int将inp_age由字符串类型转变成整型,便于进行比较
if inp_age==age_of_bk:
   print('猜对了')
print('end....')

语法二:if+else

if 条件: 缩进代码块

else : 缩进代码块

  age_of_girl=18
height=170
weight=90
is_pretty=True
if age_of_girl >=18 and age_of_girl < 22 and height >160 and is_pretty:
   print('表白')
else:
   print('阿姨好')

语法三:if+elif

if 条件1 缩进代码块elif 条件2 缩进代码块elif 条件3 缩进代码块else: 缩进代码块

  '''
如果:
      成绩>=90,那么:优秀

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

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

      其他情况:很差
'''

score=input('please input your score:')
score=int(score)
if score>=90:
   print('优秀')
elif score>=80:        #不用'and<90',因为if语句是一条成立不会运行下一条
   print('良好')
elif score>=70:
   print('普通')
else:
   print('很差')

语法四:if套if

if 条件1: if 条件2: 缩进代码块 缩进代码块

  age_of_girl=18
height=171
weight=99
is_pretty=True
success=True
if age_of_girl>=18 and age_of_girl<22 and height > 170 and is_pretty:
   if success:
       print('表白成功,在一起')
   else:
       print('不要爱情')
else:
   print('阿姨好')

流程控制之循环(while循环)

while循环:条件循环

如果条件为真,那么循环体则执行,执行完毕后再次循环,重新判断条件

如果条件为假,那么循环体不执行,循环终止

1.while 条件: 缩进代码块

  name_bk='kopa'
pwd_bk='123'
tag=True
while tag:
   name_inp=input('please input your name:')
   pwd_inp=input('please your password:')
   if name_inp==name_bk and pwd_inp==pwd_bk:
       print('login successful')
       tag=False
   else :
       print('your name or password erorr')

2.while+break: break代表结束本层循环

  name_bk='kopa'
pwd_bk='123'

while True:
   name_inp=input('please input your name:')
   pwd_inp=input('please your password:')
   if name_inp==name_bk and pwd_inp==pwd_bk:
       print('login successful')
       break
   else :
       print('your name or password erorr')

3.while+continue: continue代表结束本次循环,直接进去下一次

  count=1
while count<6:
   if count==3:
       count+=1
       continue
   print(count)
   count+=1 #如果print之后不加count+=1就会出现死循环
  #输错三次退出
name_bk='kopa'
pwd_bk='123'
count=0
while True:
   if count==3:
       print('输入次数过多')
       break
   inp_name=input('please input your name:')
   inp_pwd=input('please input your password:')
   if inp_name==name_bk and inp_pwd==pwd_bk:
       print('login success')
       break
   else:
       print('your name or pwd erorr')
       count+=1

4.while+else

  count=0
while True:
   if count == 10:
       break
   print(count)
   count+=1

else:
   print("else的子代块只有在while循环没有被break打断的情况下才会执行")
  count=0
while count <= 10:
   print(count)
   count+=1

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

总结

  name_of_bk='kopa'
pwd_of_bk='123'
tag=True
count=0

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
           elif cmd == '1':
               print('购物')
           elif cmd == '2':
               print('支付')
           elif cmd == '3':
               print('查看购物')
           else:
               print('请输入0,1,2,3')
   else:
       print('your name or password erorr')
       count+=1

最新文章

  1. SQLCMD备忘录:执行文件夹所有Sql文件
  2. QSS的应用
  3. 带转义符的json解释
  4. vs怎么创建MVC及理解其含义
  5. 如何让R代码按计划执行
  6. How to modify Code Comments[AX2012]
  7. Android 超仿Path时间轴和扇形菜单的效果
  8. UILabel字体加粗等属性和特效
  9. 那些年优秀的HTML5活动页面
  10. JavaScript学习笔记(三十八) 复制属性继承
  11. 原 iOS面试题收集
  12. what is yaml ?
  13. sql中奇怪的sum(1),sum(2),count(1),count(6),count(*):统计总数
  14. Djang之cookie和session
  15. Python Learning: 01
  16. [Android] Android 类似今日头条顶部的TabLayout 滑动标签栏 效果
  17. MySQL执行计划解析
  18. phpmyadmin无登录表单无法登陆
  19. JavaScript动画:offset和匀速动画详解(含轮播图的实现)
  20. linux学习笔记-12.输入输出重定向及管道

热门文章

  1. react-踩坑记录——swiper报错!
  2. 基于html2canvas实现网页保存为图片及图片清晰度优化
  3. mysql数据库可以远程连接或者说用IP地址可以访问
  4. 对mysql乐观锁、悲观锁、共享锁、排它锁、行锁、表锁概念的理解
  5. CXF2.7整合spring发布webservice
  6. win10家庭版多用户
  7. Springboot Session集群处理
  8. Spring Boot中的initializers的作用分析
  9. RESTful API 设计指南(转)
  10. thinkphp验证码不显示