• while 格式化输出 运算符 字符编码


    流程控制之while循环

    条件循环:while,语法如下

    while 条件:    
        # 循环体
        # 如果条件为真,那么循环体则执行,执行完毕后再次循环,重新判断条件。。。
        # 如果条件为假,那么循环体不执行,循环终止
    
    #打印0-10
    count=0
    while count <= 10:
        print('loop',count)
        count+=1
    
    #打印0-10之间的偶数
    count=0
    while count <= 10:
        if count%2 == 0:
            print('loop',count)
        count+=1
    
    #打印0-10之间的奇数
    count=0
    while count <= 10:
        if count%2 == 1:
            print('loop',count)
        count+=1
    

    死循环

    import time
    num=0
    while True:
        print('count',num)
        time.sleep(1)
        num+=1   
    

    循环嵌套与tag

      tag=True 
    
      while tag:
    
        ......
    
        while tag:
    
          ........
    
          while tag:
    
            tag=False
    

    break与continue

    #break用于退出本层循环
    while True:
        print "123"
        break
        print "456"
    
    #continue用于退出本次循环,继续下一次循环
    while True:
        print "123"
        continue
        print "456"
    

    5 while+else

    #与其它语言else 一般只与if 搭配不同,在Python 中还有个while ...else 语句,while 后面的else 作用是指,当while 循环正常执行完,中间没有被break 中止的话,就会执行else后面的语句
    count = 0
    while count <= 5 :
        count += 1
        print("Loop",count)
    
    else:
        print("循环正常执行完啦")
    print("-----out of while loop ------")
    输出
    Loop 1
    Loop 2
    Loop 3
    Loop 4
    Loop 5
    Loop 6
    循环正常执行完啦
    -----out of while loop ------
    
    #如果执行过程中被break啦,就不会执行else的语句啦
    count = 0
    while count <= 5 :
        count += 1
        if count == 3:break
        print("Loop",count)
    
    else:
        print("循环正常执行完啦")
    print("-----out of while loop ------")
    输出
    
    Loop 1
    Loop 2
    -----out of while loop ------
    

    运算符

    **算数运算符**
    
    ```python
    # +
    # -
    # *
    # / python2获取的值是整数,python3获取的是浮点数(小数2.5)
    # print(5/2)
    # //(整除-- 地板除)
    # print(5 // 2)
    # ** 幂(次方)
    # print(3**2)
    # % 模 (取余)
    # print(5 % 2)
    

    比较运算符

    # >
    # <
    # == (等于)
    # != (不等于)
    # >=
    # <=
    

    赋值运算符

    # = 赋值
    # += 自加
    # a = 10
    # a += 1 # a = a + 1
    # print(a)
    # -= 自减
    # *= 自乘
    # a = 10
    # a *= 2  # a = a * 2
    # print(a)
    # /=
    # //=
    # **=
    # %=
    

    逻辑运算符

    # and (与/和)
    # or (或)
    # not (非)
    
    # print(3 and 4)
    # print(0 and 4)
    # print(0 and False)
    
    # and 都为真的时候取and后边的值
    # and 都为假的时候取and前面的值
    # and 一真一假取假的
    
    # print(3 and 5 and 9 and 0 and False)
    # print(5 and False and 9 and 0)
    # print(1 and 2 and 5 and 9 and 6)
    
    # print(1 or 0)
    # print(1 or 2)
    # print(0 or False)
    
    # or 都为真的时候取or前边的值
    # or 都为假的时候取or后面的值
    # or 一真一假取真的
    
    # print(1 or 9 or 4 or 0 or 9)
    # print(not False)
    
    # () > not > and > or
    # 从左向右执行
    # print(9 and 1 or not False and 8 or 0 and 7 and False)
    
    

    成员运算符

    # in  存在
    # not in 不存在
    
    # s = "alexdsb"
    # if "sb" not in s:
    #     print(True)
    # else:
    #     print(False)
    

    格式化输出

    ​ 这就用到了占位符,如:%s、%d

    #%s字符串占位符:可以接收字符串,也可接收数字
    print('My name is %s,my age is %s' %('egon',18))
    #%d数字占位符:只能接收数字
    print('My name is %s,my age is %d' %('egon',18))
    print('My name is %s,my age is %d' %('egon','18')) #报错
    
    #接收用户输入,打印成指定格式
    name=input('your name: ')
    age=input('your age: ') #用户输入18,会存成字符串18,无法传给%d
    
    print('My name is %s,my age is %s' %(name,age))
    
    #注意:
    #print('My name is %s,my age is %d' %(name,age)) #age为字符串类型,无法传给%d,所以会报错
    
    name = 'fangqiang'
    msg = f'姓名:{name}'
    print(msg)    # 只适用python3.6以上的程序
    
    

    编码的问题

    python2解释器在加载 .py 文件中的代码时,会对内容进行编码(默认ascill),而python3对内容进行编码的默认为utf-8。

    ASCII(American Standard Code for Information Interchange,美国标准信息交换代码)是基于拉丁字母的一套电脑编码系统,主要用于显示现代英语和其他西欧语言,其最多只能用 8 位来表示(一个字节),即:2**8 = 256,所以,ASCII码最多只能表示 256 个符号。

    随着计算机的发展. 以及普及率的提高. 流⾏到欧洲和亚洲. 这时ASCII码就不合适了. 比如: 中⽂汉字有几万个. 而ASCII 多也就256个位置. 所以ASCII不行了. 怎么办呢? 这时, 不同的国家就提出了不同的编码用来适用于各自的语言环境. 比如, 中国的GBK, GB2312, BIG5, ISO-8859-1等等. 这时各个国家都可以使用计算机了.
    GBK, 国标码占用2个字节. 对应ASCII码 GBK直接兼容. 因为计算机底层是用英文写的. 你不支持英文肯定不行. 而英文已经使用了ASCII码. 所以GBK要兼容ASCII.
    这里GBK国标码. 前⾯的ASCII码部分. 由于使⽤两个字节. 所以对于ASCII码⽽言. 前9位都是0

    字母A:``0100` `0001` `# ASCII ``字母A:``0000` `0000` `0100` `0001` `# 国标码
    

    国标码的弊端:

      只能中国用. 日本就垮了. 所以国标码不满足我们的使用. 这时提出了一个万国码Unicode一 开始设计是每个字符两个字节. 设计完了. 发现我大中国汉字依然无法进行编码.

      只能进行扩充. 扩充成32位也就是4个字 节. 这回够了. 但是. 问题来了. 中国字9万多. 而unicode可以表示40多亿. 根本用不了. 太浪费了. 于是乎, 就提出了新的 UTF编码.可变长度编码

      Unicode(统一码、万国码、单一码)是一种在计算机上使用的字符编码。Unicode 是为了解决传统的字符编码方案的局限而产生的,

      它为每种语言中的每个字符设定了统一并且唯一的二进制编码,规定虽有的字符和符号最少由 16 位来表示(2个字节),即:2 **16 = 65536

      注:此处说的是最少2个字节,可能更多

      UTF-8,是对Unicode编码的压缩和优化,他不再使用最少使用2个字节,而是将所有的字符和符号进行分类:ascii码中的内容用1个字节保存、

      欧洲的字符用2个字节保存,东亚的字符用3个字节保存...

      UTF-16: 每个字符最少占16位.

      GBK: 每个字符占2个字节, 16位. 

    单位转换

    8bit = 1byte
    1024byte = 1KB
    1024KB = 1MB
    1024MB = 1GB
    1024GB = 1TB
    1024TB = 1PB
    1024TB = 1EB
    1024EB = 1ZB
    1024ZB = 1YB
    1024YB = 1NB
    1024NB = 1DB
    常⽤到TB就够了 
    
  • 相关阅读:
    杭电1171 Big Event in HDU(母函数+多重背包解法)
    怎样设计接口?
    未将对象引用设置到对象的实例--可能出现的问题总结
    開始Unity3D的学习之旅
    介绍一款轻量级js控件:easy.js
    Mustache 使用心得总结
    (ArcGIS API For Silverlight )QueryTask 跨层查询,和监控完整的查询!
    非常基本的SQL 内外连接
    Myeclipse它显示了一个目录的结构,而不是包
    Duanxx的Design abroad: C++矩阵运算库Eigen 概要
  • 原文地址:https://www.cnblogs.com/fengqiang626/p/11122857.html
Copyright © 2020-2023  润新知