美文网首页
Python 基础

Python 基础

作者: 婉卿容若 | 来源:发表于2017-03-08 17:36 被阅读100次

学习资料

Python之旅

技能树.png

相关链接

Python - 基本数据类型


笔记

字符编码

基本概念
  • character: 一个信息单位,各种文字和符号的总称
  • charater set: 字符的集合. 常用字符集: ASCII(128个字符),GB2312(中国国家标准的简体中文字符集),Unicode (包括世界各国语言中使用到的所有字符)等
  • character encoding: 对字符集中的字符,将其编码为特定的二进制数.
概念 概念描述 举例
字符 一个信息单位,各种文字和符号的总称 ‘中’, ‘a', ‘1', '$', ‘¥’, ...
字符集 字符的集合 ASCII 字符集, GB2312 字符集, Unicode 字符集
字符编码 将字符集中的字符,编码为特定的二进制数 ASCII 编码,GB2312 编码,Unicode 编码
字节 计算机中存储数据的单元,一个 8 位(bit)的二进制数 0x01, 0x45, ...”
常见字符编码简介
  1. ASCII 编码(American Standard Code for Information Interchange,美国信息互换标准编码)
  2. GBK 编码
  3. Unicode 编码: 为每种语言的每个字符设定了独一无二的二进制编码. 使用十六进制数字,并且在数字前面加上前缀** U+**.更多符号对应表查询 unicode.org
  4. UTF-8 编码: 一种针对 Unicode 的可变长度字符编码 <= Unicode 浪费资源(四个字节) => UTF-8(字符用一/二/三/四个字节来表示),UTF-16(字符用两个或四个字节表示),UTF-32(字符用四个字节表示).

Python2的默认编码是 ascii, Python3的默认编码是 utf-8
查询 mac 上安装的 Python 的当前版本(命令行操作)
python + 回车
查询当前版本的 Python 支持的编码
import sys
sys.getdefaultencoding()

操作.png
Python2 中的字符类型
  • basestring 是 str 和 unicode 的父类
  • str 类型的字符串有多种编码方式,默认是 ascii
  • Unicode 类型的字符串使用** u'...' ** 的形式表示
Unicode 和 str 关系图.png

两种字符串的转换:

  • 把 UTF-8 编码表示的字符串 'xxx' 转换为 Unicode 字符串 u'xxx' 用 decode('utf-8') 方法:
>>>'中文'.decode('utf-8')
 u'\u4e2d\u6587”
  • 把 u'xxx' 转换为 UTF-8 编码的 'xxx' 用 encode('utf-8') 方法:
>>> u'中文'.encode('utf-8')
'\xe4\xb8\xad\xe6\x96\x87”
UnicodeEncodeError & UnicodeDecodeError 根源
  • 在进行同时包含 str 类型和 unicode 类型的字符串操作时,Python2 一律都把 str 解码(decode)成 unicode 再运算(隐式解码成 unicode,我们需要手动将其解码成 utf-8),这时就很容易出现 UnicodeDecodeError。
  • 如果函数或类等对象接收的是 str 类型的字符串,但你传的是 unicode,Python2 会默认使用 ascii 将其编码成 str 类型再运算(我们需要手动将其进行 utf-8编码),这时就很容易出现 UnicodeEncodeError。
小结
  • UTF-8 是一种针对 Unicode 的可变长度字符编码,它是 Unicode 的实现方式之一
  • Unicode 字符集有多种编码标准,比如 UTF-8, UTF-7, UTF-16。
  • 在进行同时包含 str 类型和 unicode 类型的字符串操作时,Python2 一律都把 str 解码(decode)成 unicode 再运算。
  • 如果函数或类等对象接收的是 str 类型的字符串,但你传的是 unicode,Python2 会默认使用 ascii 将其编码成 str 类型再运算。

输入输出

输入
  • raw_input: 无论我们输入一个字符串/数值还是表达式直接返回一个字符串 => Python3中移除
    raw_input
>>> name = raw_input('please enter your name:')
please enter your name:roni
>>> name
'roni'
>>> type(name)
<type 'str'>
>>> num = raw_input('please enter your id:')
please enter your id:123333
>>> num
'123333'
>>> type(num)
<type 'str'>
>>> sum = raw_input('plaese enter a+b:')
plaese enter a+b:3+6
>>> sum
'3+6'
>>> type(sum)
<type 'str'>
  • input: **Python3中 input就是 Python2 中的raw_input **
    本质: 调用 input 实际上是通过 raw_input 再调用 eval 函数实现的. eval通常用来执行一个字符串表达式并返回表达式的值
    输入字符串需要使用引号把他们括起来,输入数值返回数值,输入表达式会对表达式进行计算
    input 在 Python2中的定义
def input(prompt):
       return (eval(raw_input(prompt)))

eval

>>> eval('1+9')
10
>>> a = 1
>>> eval('a+10')
11
>>>

input

>>> name = input('please input you name:')
please input you name:'roni'
>>> name
'roni'
>>> type(name)
<type 'str'>
>>> num = input('please input your id:')
please input your id:123344
>>> num
123344
>>> type(num)
<type 'int'>
>>> sum = input('please input a+b:')
please input a+b:3+6
>>> sum
9
>>> type(sum)
<type 'int'>
>>> sum02 = input('please input a+b:')
please input a+b:'3'+'4'
>>> sum02
'34'
>>> type(sum02)
<type 'str'>
>>>
输出

Python2中的print 是一个语句(statement),而Python3中的print 是一个函数

  • 简单输出: 直接在print后面加上数字/字符串/列表等对象
    在 Python2中 print 可以加括号也可以不加
>>> print 123yii123
>>> print 'roni'
roni
>>> x = 10
>>> print x
10
>>> d = {'a': 1, 'b': 2}
>>> print d
{'a': 1, 'b': 2}
>>> print(123)
123
>>> print(d)
{'a': 1, 'b': 2}
>>>
  • 格式化输出
    限制精度
>>> s = 'hello'
>>> l = len(s)
>>> print('the length of %s is %d' % (s,l))
the length of hello is 5
>>> pi = 3.14159
>>> print('%10.3f' % pi) # 字段宽度为10, 精度3 
     3.142
>>> print('%010.3f' % pi) # 用0 现充空白
000003.142
>>> print('%+f' % pi) # 显示正负号
+3.141590
>>> print('%+10.3f' % pi)
    +3.142
>>>
  • 换行输出: print 默认换行输出,如果不想换行,在末尾加上一个,
    Python2中的 print
>>> for i in range(0,3):
...  print i
...
0
1
2
>>> for i in range(0,3):
...  print i,
...
0 1 2 # 注意会默认加上一个空格
>>>

Python3中的 print => print必须加括号,否则会抛 SyntaxError

>>> for i in range(0, 3):
...     print(i)
...
0
1
2
>>> for i in range(0, 3):
...     print(i, end='')    # 加上一个 end 参数
...
012

小结
  • 在 Python2 中,raw_input 会读取控制台的输入,并返回字符串类型。
  • 在 Python2 中,如无特殊要求建议使用 raw_input() 来与用户交互。
  • 在 Python3 中,使用 input 处理输入,如有特殊要求,可以考虑加上 eval。

说明

PS: 以上内容均来自Python之旅.这里只是个人笔记.

相关文章

网友评论

      本文标题:Python 基础

      本文链接:https://www.haomeiwen.com/subject/tzuygttx.html