常量

常量即指不变的量.在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

基础运算符补充,流程控制之if判断/while循环的更多相关文章

  1. [基本运算符、流程控制之if判断、与用户交互、深浅拷贝]

    [基本运算符.流程控制之if判断.与用户交互] 基本运算符 1.算数运算符 python支持的算术运算符与数学上计算的符号使用是一致的 salary = 3.3 res = salary * 12 p ...

  2. 格式化输出的三种方式,运算符及流程控制之if判断

    ''' 格式化输出的三种方式,运算符及流程控制之if判断 ''' # 格式化输出的三种方式 # 一.占位符 程序中经常会有这样场景:要求用户输入信息,然后打印成固定的格式 比如要求用户输入用户名和年龄 ...

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

    1.常量 值不会改变的量 python中没有特别的语法定义常量,一般约定用全大写字母命名常量,比如圆周率pi 2.预算符补充 2.1算数运算符 print(10/3)除法运算 print(10//3) ...

  4. Linux基础-shell脚本知识整理和脚本编写----------变量、运算符、流程控制、函数、计划任务(发送邮件)

    I:知识整理:变量.运算符.流程控制.函数.计划任务 变量 系统变量:set:显示所有变量                env:环境变量 常用系统变量: path pwd lang home his ...

  5. python基础02—运算符与流程控制

    运算符与流程控制 运算符 赋值运算 用'='表示,'='的左边只能是变量 算术运算 +.-.*:加.减.乘 /:除法运算,运算结果为浮点数 //:除法运算,运算结果为整数(商) %:求余 **:求幂 ...

  6. 【python基础】第06回 运算符和流程控制 1

    本章内容概要 1.运算符 2.流程控制 本章内容详解 1.运算符 什么是运算符? 运算符用于执行程序代码运算,会针对一个以上操作数项目来进行运算.例如:2+3,其操作数是2和3,而运算符则是" ...

  7. DAY 04运算符与流程控制

    输入输出补充: python2与python3的输入输出不同 python2中有两种用户 输入方式,一种是raw_input,和input raw_input与python3的input是相同的 而p ...

  8. Unit04: JavaScript 概述 、 JavaScript 基础语法 、 流程控制

    Unit04: JavaScript 概述 . JavaScript 基础语法 . 流程控制 my.js function f3() { alert("唐胜伟"); } demo1 ...

  9. Java 中的运算符和流程控制

    Java 中的运算符和流程控制 + 面试题 算术运算符 Java 中的算术运算符,包括以下几种: **算术运算符** **名称** **举例** + 加法 1+2=3 - 减法 2-1=1 \* 乘法 ...

随机推荐

  1. 将List的元素通过中文字符串排序

    类customer public class Customer { public String name; public int age; Customer(String name, int age) ...

  2. map中的count方法

    map.count(Key)返回值为1或者0,1返回存在,0返回不存在,返回的是布尔类型的值,因为在map类型中所有的数据的Key值都是不同的,所以被count的数要么存在1次,要么不存在

  3. RabbitMQ简单应用の简单队列

    (1)首先创建一个maven项目: pom.xml,重点是配置RabbitMQ <dependencies> <dependency> <groupId>junit ...

  4. Netty源码学习笔记

    1.ByteBuf

  5. 【转】Linux下查看系统配置

    [转]Linux下查看系统配置 CPU 1. lscpu:显示cpu架构信息 [xxx@localhost ~]$ lscpu Architecture: x86_64 CPU op-mode(s): ...

  6. javascript高级程序语言学习笔记

    1.加法操作符(+)的用法 第一种情况,如果两个操作符都是数值,执行常规的加法计算. 第二种情况,如果两个操作数都是字符串,则将第二个操作数与第一个操作数拼接起来. 第三种情况,只有一个操作数是字符串 ...

  7. 第三章 Models详解

    摘自:http://www.cnblogs.com/xdotnet/archive/2012/03/07/aspnet_mvc40_validate.html Model的概念 万丈高楼平地起,先理解 ...

  8. hibernate框架学习之数据抓取(加载)策略helloworld

    package cn.itcast.h3.query.hql; import java.util.List; import org.hibernate.Query; import org.hibern ...

  9. CF449C:Jzzhu and Apples

    题意简述 给出正整数n,你要把1-n之间的正整数分成尽可能多组,使得每一组两个数的最大公约数大于1;输出能分成最多组的个数,并按任意顺序输出每组的两个数. 很妙的一道题. 首先我们考虑去处理每个质数的 ...

  10. win10:在关闭防火墙下如何屏蔽特定端口

    如果win10没有组策略,请参考:https://www.cnblogs.com/huiy/p/9291392.html 在"开始"菜单选择"运行",输入&qu ...