Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3.2 Python 基础语法之数据类型和变量 #14

Open
ShannonChenCHN opened this issue Jul 16, 2018 · 5 comments
Open

3.2 Python 基础语法之数据类型和变量 #14

ShannonChenCHN opened this issue Jul 16, 2018 · 5 comments

Comments

@ShannonChenCHN
Copy link
Owner

ShannonChenCHN commented Jul 16, 2018

  • 数据类型和变量
    • 变量赋值
    • 关键字
    • 变量命名
    • 命名规范
  • 字符串和编码
  • 常用的集合数据类型
    • 有序集合
      • list
      • tuple
      • range
    • dict
    • set

关联 issue

@ShannonChenCHN
Copy link
Owner Author

ShannonChenCHN commented Aug 15, 2018

数据类型和变量

一、数据类型

1. 整数和浮点数

1.1 整数

  • 整数跟 C 语言中类似
  • 正数负数:1,100,-8080,0
  • 进制:0xff00,0xa5b4c3d2

1.2 浮点数

  • 浮点数其实就是小数。

小数为什么被称为浮点数?

  • 常规写法:1.23,-9.01
  • 科学计数法:1.23e9,1.2e-5

数字类型支持的操作:

Operation Result Notes Full documentation
x + y sum of x and y    
x - y difference of x and y    
x * y product of x and y    
x / y quotient of x and y    
x // y floored quotient of x and y (1)  
x % y remainder of x / y (2)  
-x x negated    
+x x unchanged    
abs(x) absolute value or magnitude of x   abs()
int(x) x converted to integer (3)(6) int()
float(x) x converted to floating point (4)(6) float()
complex(re, im) a complex number with real part re, imaginary part im. im defaults to zero. (6) complex()
c.conjugate() conjugate of the complex number c    
divmod(x, y) the pair (x // y, x % y) (2) divmod()
pow(x, y) x to the power y (5) pow()
x ** y x to the power y (5)  

2. 字符串

' 或者 " 包围起来的文本就是字符串:'I am Chinese.'"I am from 'China'."

  • 转义字符
  • 字面量前缀
    • u
    • b
    • r:表示 raw string
  • 多行的字符串:用 '''....'''格式表示多行的字符串,可以替代字符串中的\n

3. 布尔值

  • 布尔值:TrueFalse
  • 布尔值运算符:andornot

4. 空值

Python 中只有一个表示空的值——None

5. 自定义对象

二、变量

在计算机内部,任何数据都可以看作是一个“对象”,而变量就是在程序中用来指向这些数据“对象”的,对变量赋值就是把变量和数据关联起来。

变量名可以包含大小写字母、数字和下划线 _,但是不能用数字开头。

在 Python 中,一个变量可以被重复赋值,而且可以是不同类型的值。

动态语言 VS. 静态语言
在静态语言中(比如 C、Java),定义变量时必须指定变量类型,如果赋值时类型不匹配,编译器就会报错。
而动态语言(比如 Python、JavaScript),在定义变量时不需要指定变量类型,而且一个变量可以被赋不同类型的值。

三、常量

常量是指值是固定不变的变量。比如数学常数 π。

不同于 C 语言中的 const 常量,Python 中并没有一种机制来确保常量的值不被改变。

按照惯例,我们通常会将常量名用大写表示。例如:

PI = 3.1415926

四、保留字(Reserved Words)

以下单词作为保留字,不能作为变量/常量名:

False      await      else       import     pass
None       break      except     in         raise
True       class      finally    is         return
and        continue   for        lambda     try
as         def        from       nonlocal   while
assert     del        global     not        with
async      elif       if         or         yield

参考

@ShannonChenCHN
Copy link
Owner Author

ShannonChenCHN commented Aug 15, 2018

字符串和编码

一、字符编码

1. 为什么需要对字符进行编码?有哪些常用的编码方式?

  • 因为计算机只能处理二进制数字,所以要想处理文字,必须先将文本转成数字,计算机才能识别和存储。因此,我们就需要一个数字与字符之间的映射关系表。
  • 编码是将文字转成数字,相应地,当计算机需要读取本地的或者接受传输过来的被编码过的字符串,就需要解码。
  • 字符编码发展史:自然语言 -> ASCII 编码 / GB2312 编码 / Shift_JIS 编码/... -> Unicode 编码(统一编码、通用字符集) -> UTF-8 / UTF-16/...(“可变长编码”、节省空间)
  • UTF-8 使用 1~6 个字节为每个字符进行编码,一般编码一个英文字符只需要 1 个字节,编码 1 个中文字符要 3 个字节

2. 如何设置Python 文件的编码、解码方式

在文件开头加入 # -*- coding: <编码方式名> -*- 或者 #coding=<编码方式名> 就可以指定 Python 文件内容的编码方式,以便解释程序知道读取代码时用何种方式解码。

注意:

  • Python3.X 源码文件默认使用utf-8编码,所以可以正常解析中文,无需指定 UTF-8 编码。
  • 如果你使用的是 windows 上的编辑器,在保存文件时需要设置文件存储的格式为 UTF-8 without BOM,否则会在执行文件时出现解码错误信息。

例如:

#!/usr/bin/python
#coding=utf-8

print ('你好,世界')

二、Python 中的字符串

Python 中字符串通常就是 str 对象,str 是不可变的。

Python 的字符串有以下几种写法:

  • Single quotes: 'allows embedded "double" quotes'
  • Double quotes: "allows embedded 'single' quotes"
  • Triple quoted: '''Three single quotes''', """Three double quotes"""

Triple quoted strings may span multiple lines - all associated whitespace will be included in the string literal.

  • 编码和解码:encode()decode() 方法
  • 计算字符串的长度:len() 函数
  • 'xyz'b'xyz' 的区别:前者是 str 类型,后者是 bytes 类型
  • 即便表示的是相同的字符串,str 类型和 bytes 类型的长度是不一样的。比如:len('abc')len(b'abc')

1. 访问字符串中的值

2. 字符串更新

3. 转义字符

4. 字符串运算符

5. 字符串格式化

6. 三引号(triple quotes)

7. Unicode 字符串

8. 字符串内建函数

  • strip
  • rfind:返回字符串最后一次出现的位置(从右向左查询),如果没有匹配项则返回-1。

参考


问题

  1. 如何去掉字符串中间的空白和换行?

使用下面这段代码可以去掉开头和结尾的任何空白、制表符和换行等字符:

s = s.strip(' \t\n\r')

参考: How do I trim whitespace?

@ShannonChenCHN
Copy link
Owner Author

ShannonChenCHN commented Aug 16, 2018

有序类型

Python 中有 3 种常用的有序类型:list,tuple 和 range,其中 list 是可变的,tuple 和 range 不可变。

有序类型都支持的一些操作(摘自官方文档):

Operation Result Notes
x in s True if an item of s is equal to x, else False (1)
x not in s False if an item of s is equal to x, else True (1)
s + t the concatenation of s and t (6)(7)
s * n or n * s equivalent to adding s to itself n times (2)(7)
s[i] ith item of s, origin 0 (3)
s[i:j] slice of s from i to j (3)(4)
s[i:j:k] slice of s from i to j with step k (3)(5)
len(s) length of s  
min(s) smallest item of s  
max(s) largest item of s  
s.index(x[, i[, j]]) index of the first occurrence of x in s (at or after index i and before index j) (8)
s.count(x) total number of occurrences of x in s  

可变类型的有序类型支持的一些操作(摘自官方文档):

Operation Result Notes
s[i] = x item i of s is replaced by x  
s[i:j] = t slice of s from i to j is replaced by the contents of the iterable t  
del s[i:j] same as s[i:j] = []  
s[i:j:k] = t the elements of s[i:j:k] are replaced by those of t (1)
del s[i:j:k] removes the elements of s[i:j:k]from the list  
s.append(x) appends x to the end of the sequence (same as s[len(s):len(s)] = [x])  
s.clear() removes all items from s (same as del s[:]) (5)
s.copy() creates a shallow copy of s (same as s[:]) (5)
s.extend(t) or s += t extends s with the contents of t (for the most part the same as s[len(s):len(s)] = t)  
s *= n updates s with its contents repeated n times (6)
s.insert(i, x) inserts x into s at the index given by i(same as s[i:i] = [x])  
s.pop([i]) retrieves the item at i and also removes it from s (2)
s.remove(x) remove the first item from s where s[i] is equal to x (3)
s.reverse() reverses the items of s in place (4)

一、list

list 是一个有序类型,支持增删改查元素,类似于 C 语言中的数组,但更强大。

创建列表的几种方式:

  • Using a pair of square brackets to denote the empty list: []
  • Using square brackets, separating items with commas: [a], [a, b, c]
  • Using a list comprehension: [x for x in iterable]
  • Using the type constructor: list() or ist(iterable)

除了上面列出的有序类型和可变有序类型共有的一些操作之外,list 还有一个 sort() 方法可以将 list 中的元素进行排序。

示例代码:

classmates = ['Michael', 'Bob', 'Tracy']

# 数组长度
print(classmates)
print(len(classmates))

# 获取第一个元素和最后一个元素
print(classmates[0])
print(classmates[-1])

# 在末尾添加一个新元素
classmates.append('Adam')
print(classmates)

# 在制定位置插入一个新元素
classmates.insert(1, 'Jack')
print(classmates)

# 删除末尾的元素
classmates.pop()
print(classmates)

# 删除指定位置的元素
classmates.pop(1)
print(classmates)

# 更新指定位置的元素
classmates[1] = 'Sarah'
print(classmates)

# 排序
classmates.sort()

二、tuple

tuple 跟数组类似,也是一个有序类型,但是是不可变的。

我们可以通过以下几种方式来创建 tuple:

  • Using a pair of parentheses to denote the empty tuple: ()
  • Using a trailing comma for a singleton tuple: a, or (a,)
  • Separating items with commas: a, b, c or (a, b, c)
  • Using the tuple() built-in: tuple() or tuple(iterable)

值得注意的是,当表示一个只有一个元素的 tuple 时,不能直接写成 (1) 这种形式,必须要加一个逗号(1,)

Tuple 支持上面列出的所有有序集合的操作。

示例代码:

t1 = (1, 2)
t2 = (1, )

三、range

range 是一个不可变的有序集合范围,range 通常用来在 for 循环中循环指定次数。

示例代码:

>>> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list(range(1, 11))
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> list(range(0, 30, 5))
[0, 5, 10, 15, 20, 25]
>>> list(range(0, 10, 3))
[0, 3, 6, 9]
>>> list(range(0, -10, -1))
[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
>>> list(range(0))
[]
>>> list(range(1, 0))
[]

>>> r = range(0, 20, 2)
>>> r
range(0, 20, 2)
>>> 11 in r
False
>>> 10 in r
True
>>> r.index(10)
5
>>> r[5]
10
>>> r[:5]
range(0, 10, 2)
>>> r[-1]
18

参考

@ShannonChenCHN
Copy link
Owner Author

ShannonChenCHN commented Aug 20, 2018

dict

Python 中的 dict 跟 Objective-C 中的 NSDictionary 类似,都是存储 key-value 键值对的集合。

>>> d = {'Michael': 95, 'Bob': 75, 'Tracy': 85}
>>> d['Michael']
95
>>> d['Adam'] = 67
>>> d['Adam']
67

常用操作:

len(d)
d[key]
d[key] = value
del d[key]
key in d
key not in d
iter(d)
clear()
copy()
classmethod fromkeys(seq[, value])
get(key[, default])
items()
keys()
pop(key[, default])
popitem()
setdefault(key[, default])
update([other])
values()

One more thing

使用字面量语法读取 dict 的 key-value 键值对时,如果该 key 不存在,会抛出异常:

>>> d = {'name': 'James'}
>>> d['age']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'age'

有两种方法可以避免。
第一种是通过 in 表达式判断 key 是否存在:

>>> 'age' in d
False

另一种方法是使用 get()方法来获取 key 对应的 value,如果key不存在,会返回 None,或者自己指定的 value:

value = d.get(key)
value = d.get(key, "empty")

参考:

@ShannonChenCHN
Copy link
Owner Author

ShannonChenCHN commented Aug 21, 2018

set

和 dict 类似,set 也是一组 key 的集合,但不存储 value。由于 key 不能重复,所以,在 set 中不能有重复的 key。

set 支持的操作:

len(s)

x in s

x not in s

isdisjoint(other)

issubset(other) 或者 set <= other

set < other

issuperset(other) 或者 set >= other

set > other

union(*others) 或者 set | other | ...


intersection(*others) 或者 set & other & ...


difference(*others) 或者 set - other - ...

symmetric_difference(other) 或者 set ^ other

copy()

示例代码:

>>> s = set([1, 2, 3])
>>> s
{1, 2, 3}

>>> s = set([1, 1, 2, 2, 3, 3])
>>> s
{1, 2, 3}

>>> s.add(4)
>>> s
{1, 2, 3, 4}
>>> s.add(4)
>>> s
{1, 2, 3, 4}

>>> s.remove(4)
>>> s
{1, 2, 3}

>>> s1 = set([1, 2, 3])
>>> s2 = set([2, 3, 4])
>>> s1 & s2
{2, 3}
>>> s1 | s2
{1, 2, 3, 4}

参考

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant