Python之编码_格式化输出_运算符

一.编码:
  单位:bit,byte,KB,MB,GB,TB...
        1byte(字节) == 8bit(位)
        1KB == 1024byte
        1MB == 1024KB
        1GB == 1024MB
  ascii码:一个字节8位,不支持中文
   -- 内置小方法:chr(65) --> 'A';ord('a') --> 97
  GBK(国标码):一个中文字占2个字节
  unicode(万国码):一个中文字占4个字节
  UTF8:一个中文字占3字节,python3默认编码
    Python3中的encode和decode
      print('你好'.encode('utf-8')) --> b'\xe4\xbd\xa0\xe5\xa5\xbd'
      print('你好'.encode('unicode-escape')) --> b'\\u4f60\\u597d'
      
      print(b'\xe4\xbd\xa0'.decode()) --> 你
      print('\u4f60')  --> 你
      print(b'\u4f60'.decode('unicode-escape')) -- 你
      print('\u4f60'.decode('unicode-escape')) -- 异常报错

二.格式化输出
   # %s 字符串占位符  %-ms 右侧补空格,%ms 左侧补空格(m为数值)
     print('我%-10s左手python,右手java,天下无敌' %('码神'))
       # 我码神        左手python,右手java,天下无敌
     print('我%10s左手python,右手java,天下无敌' %('码神'))
       # 我        码神左手python,右手java,天下无敌
     print('格式化输出%d%%' %(80))
       # 格式化输出80%
   # %d 数值占位符
   
   python2.6之后引入format()函数
     print('我{0}左手{1},右手{2},天下无敌'.format("codegod", "python", "java"))
     print('我{0}左手{1:*^10},右手{2},天下无敌'.format("codegod", "python", "java"))  # 输出指定长度字符串,空位补"*"
     # 对齐方式 -- < 左对齐 > 右对齐 ^ 居中 = (只用于数字) 小数点后补齐
     print("{0:g}".format(3.141592653))  # 格式化为通用格式
     print("{0:e}".format(3.141592653))  # 格式化为科学计数
     print("{0:%}".format(0.75))  # 格式化为百分数
     print("1 / 3  = {0:.3}".format(1 / 3))  # 指定显示小数点位数
     print("I say {0:20} to henry".format("hello"))  # 输出指定长度,左对齐
     print("输出一个花括号 -- {}".format("{}"))  # 当参数只有一个时,可以不写序号
     print("使用名称代替序号输出:{a},{b},{c}".format(b="我是名称b的内容",c="我是名称c的内容",a="我是名称a的内容"))  # 可不按顺序给参数
     import sys
     print(sys.platform,sys.version)
     print("当前平台是:{0.platform},当前版本是:{0.version}".format(sys))
     -----------------------输出结果--------------------------------------------------
     我codegod左手python,右手java,天下无敌
     我codegod左手**python**,右手java,天下无敌
     3.14159
     3.141593e+00
     75.000000%
     1 / 3  = 0.333
     I say hello                to henry
     输出一个花括号 -- {}
     使用名称代替序号输出:我是名称a的内容,我是名称b的内容,我是名称c的内容
     win32 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)]
     当前平台是:win32,当前版本是:3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)]
三.运算符
   数学运算符
     + - * / %(取余|取模) //(整除) **(幂运算)
     python2除法运算根据数据类型得到不同的值
         5 / 2 == 2  5.0 / 2 == 2.5
     python3除法运算统一位浮点数运算
-----------------------------------------------
   比较运算符
     等于==  不等于!=(<>)  >  <  >=  <=
   赋值运算符
      = += -= *= /= %= //= **=
   逻辑运算符-- 优先级:() > not > and > or;同优先级从左到右
      and -- 逻辑与
         有一个非真则整体非真
      or -- 逻辑或
         有一个为真则整体为真
      not -- 逻辑非
          原为真则not为假,原为假则not为真
   真值表:
      --------------------------
      |  x  |  y  | and |  or  |
      --------------------------
      |  0  |  0  |  0  |  0   |
      --------------------------
      |  0  |  1  |  0  |  1   |
      --------------------------
      |  1  |  1  |  1  |  1   |
      --------------------------
      |  1  |  0  |  0  |  1   |
      --------------------------
   # x or y -- x 为真 则返回x
     print(1 or 2) # 输出 1
     print(0 or 2) # 输出 2
   # x and y -- x 为真 则返回y
     print(1 and 2) # 输出 2
     print(0 and 2) # 输出 0
     
   # print(2 or 1 < 3 and 2) # 输出 2
      -- 比较运算得1 -> 1 and 2 得2 -> 2 or 2 输出2

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值