• day 2


    1. 格式化输出

    name = input('请输入姓名:')
    age = int(input('请输入年龄:'))   #定义字符串为整数型用int,定义全部交互的数据为整数型
    job = input('请输入工作:')
    hobbie =input('请输入爱好:')
    
    msg = ('''------------ info of %s  -----------   #后面括号第一个name
    Name  : %s      # 第二个name
    Age   : %d      # 代表age
    job   : %s      # 代表job
    Hobbie: %s      # 代表hobbie
    ------------- end -----------------''') % (name, name, age, job, hobbie)  #  %把上面的
    占位符与括号内的变量一一对应起来 print(msg)

     以上   %是占位符      s是字符串

    %s  代表字符串占位符   %d代表数字占位符  

    注:如果占位符使用 %d 时,此时变量age 必须输入数字,否则报错

    请输入年龄:wdwa
    请输入工作:wda
    请输入爱好:wda
    Traceback (most recent call last):
      File "D:/pycharmprojects/day 2/day 2  练习.py", line 6, in <module>
        msg = ('''------------ info of %s  -----------   #后面括号第一个name
    TypeError: %d format: a number is required, not str
    

     解决方法如上,将变量 age的交互输入定义为整数型  :age = int(input('请输入年龄:'))

    如果要在输出结果中打印出%这个特殊字符则用如下操作

    progress = int(input("请输入进度 :"))
    msg = "学习进度: %d  %% " % (progress)
    print(msg)
    

     %%   前一个为转译,后面则是正常的%

    2. while循环

    while  条件:

      循环体

    #如果条件为真,则循环执行

    #如果条件为假,则循环不执行

    while True:
        print('1')
        print('2')
    

       1.终止循环的第一个方法:改变条件

    count = 1
    while count < 101:
        print(count)
        count +=1
    

         上列为输出1-100所有数字,上列  count<101  即为while条件,count从1开始一直自加到  一百,当自加到101时,由于不满足条件,则跳出循环

    count = 1
    sum = 0
    while count < 101:
        sum += count   #sum = sum + count
        count +=1
    print(sum)
    

         1-100自加,

        思路:你要想从1+2+3......+100一直加到100,那么最起码你得有一个自变量,比如      count,这个count每次循环都会自加1,除了这个之外,你还有有一个变量,让这个变量    一直去加这个count,这个基础变量还不能影响最终的结果,所以这个基础变量初始值为    0,按照这个思路写一下。

      2.终止循环第二种方式:break

        

    while  True:
        print('1')
        print('2')
        break
        print('3')
    print('4')
      
    D:工作用软件Pythonpython.exe "D:/pycharmprojects/day 2/day 2  练习.py"
    1
    2
    4
    
    Process finished with exit code 0
    

         上列说明,在循环中只要遇到break, 马上退出循环

      3.终止循环的第三种方式:continue

    while  True:
        print('1')
        print('2')
        continue
        print('3')
    print('4')
    

         上述代码会持续输出1212121212........,continue 意为终止本次循环继续下次循环          while下的语句为一次循环, 遇见continue即往上折返。不会执行3  4不属于white循环语    句。

    count = 1
    while count < 10:
        count +=1
        if count == 7:
            continue
        print(count)
    

         输出1234568910,跳过连续数字中间某一个值,当count值为7的时候,终止本次循       环,7不被打印,进入下次循环,打印8.进入下个循环.....

    count = 0
    while count <= 100:
        count += 1
        if  1 <= count <= 5:
            print(count)
            continue
        if  95 <= count <= 100:
            print(count)
    

         输出1-5   95-100   利用continue 跳出一次循环,进行下次循环

    while  else

    count = 0
    while count < 4:
        count +=1
        if count ==3:break
        print(count)
    else: print('跳出')
    print('...........................................')
    count = 0
    while count < 4:
        count +=1
        if count ==3:pass   #pass  跳过
        print(count)
    else: print('跳出')
    
    1
    2
    ....................
    1
    2
    3
    4
    跳出
    

       遇到break的时候跳出循环,while没有正常完成,不执行else 

      pass是跳过,循环正常完成,执行else

    3. 基本运算符

      算数运算:**  幂次方, // 返回商的整除部分,% 取余

      比较运算:!=  不等于, == 赋值

      逻辑运算:

        1. 运算优先级:() > not > and > or     同一优先级从左往右

     print(3>4 or 4<3 and 1==1)#F
     print(1 < 2 and 3 < 4 or 1>2 )#T
     print(2 > 1 and 3 < 4 or 4 > 5 and 2 < 1)#T
     print(1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8)#F
     print(1 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6)#F
     print(not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6)#F
    

         ps   int----->bool  非零  True  零 False

    #PS   int ----> bool  非零转换成bool  True   0 为False
    print(bool(2))
    print(bool(0))
    #bool ---->int
    print(int(True))   #1
    print(int(False))  #0
    

        2. x or y , x为真,值就是x,x为假,值是y;

                      x and y, x为真,值是y,x为假,值是x。

    #  x or y  x True  则返回x
    print(1 or 2)#1
    print(3 or 2)#3
    print(0 or 2)#2
    print(0 or 20)#20
    #  x and y  x True  则返回y
    print(1 and 2)#2
    print(0 and 2)#0
    

     python运算符优先级:

    4. 初始编码

      ASCII  :1个字节   八个bit   2**8=128  最开始只有7位,为了后期扩展增加了一位。0

      utf-8 : 3个字节  24个bit   2**24   中文9万多    够用了

    8bit = 1byte
    1024byte = 1KB
    1024KB = 1MB
    1024MB = 1GB
    1024GB = 1TB
    1024TB = 1PB
    1024TB = 1EB
    1024EB = 1ZB
    1024ZB = 1YB
    1024YB = 1NB
    1024NB = 1DB
    常⽤到TB就够了 
    
  • 相关阅读:
    JMeter 压测基础(四)——Java工程测试
    Docker 实战(二)——centos7镜像安装nginx,将安装nginx的centos容器生成新的镜像,并导出
    JMeter压测基础(三)——Mysql数据库
    Jmeter压测基础(二)——Badboy功能、Jmeter参数化、检查点、集合点、动态关联、图形监控
    API 自动化框架
    Python Flask Restful
    【19】Grafana添加Zabbix为数据源
    【18】使用公共邮箱发送邮件
    xls格式转化为txt格式
    【17】自动发现磁盘脚本
  • 原文地址:https://www.cnblogs.com/zp751060301/p/12611116.html
Copyright © 2020-2023  润新知