美文网首页
<Python Tips> 2. 数据类型

<Python Tips> 2. 数据类型

作者: llitfk_DockOne | 来源:发表于2018-07-10 22:33 被阅读5次

开启Python

➜  ~ docker run --rm -ti python:alpine python
Python 3.7.0 (default, Jul  4 2018, 02:26:27) 
[GCC 6.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

Python对象结构

Visualize your code and get live help now

可变与不可变对象

  • 值可以改变的对象是mutable, 否则为immutable
>>> age = 18
>>> id(age)
140068432251328
>>> age
18
>>> age = 30
>>> id(age)
140068432251712
>>> age
30
>>> 
>>> class Person():
...     def __init__(self, age):
...             self.age = age
... 
>>> me = Person(age=30)
>>> me.age
30
>>> id(me)
140068426091544
>>> id(me.age)
140068432251712
>>> me.age = 18
>>> id(me)
140068426091544
>>> id(me.age)
140068432251328
>>> 

内置类型: Numbers, Strings, Sequences, Collections 与 Mapping类型

Numbers
  • Integer(整数):
>>> a = 18
>>> b = 3
>>> a + b
21
>>> a - b
15
>>> a * b
54
>>> a / b
6.0
>>> a // b
6
>>> a % b
0
>>> a ** b
5832
>>> 
  • Boolean:
>>> int(True)
1
>>> int(False)
0
>>> bool(1)
True
>>> bool(-1)
True
>>> bool(0)
False
>>> not True
False
>>> not False
True
>>> True and False
False
>>> False or True
True
>>> 1 + True
2
>>> 1 - False
1
>>> 
  • Real Number(实数) (浮点型numbers):
>>> pi = 3.1415926
>>> r = 10.5
>>> area = pi * (r ** 2)
>>> area
346.36058415
>>> 
>>> import sys
>>> sys.float_info
sys.float_info(max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308, min=2.2250738585072014e-308, min_exp=-1021, min_10_exp=-307, dig=15, mant_dig=53, epsilon=2.220446049250313e-16, radix=2, rounds=1)
>>> 
>>> 0.3 - 0.1 * 3 
-5.551115123125783e-17
>>> 
  • Complex numbers(复数):
>>> c = 3.14 + 2.73j
>>> c
(3.14+2.73j)
>>> c.real
3.14
>>> c.imag
2.73
>>> c.conjugate()
(3.14-2.73j)
>>> c * 2
(6.28+5.46j)
>>> c ** 2
(2.4067000000000007+17.1444j)
>>> d = 1 + 1j
>>> c - d
(2.14+1.73j)
>>> 
  • Fractions(分数)与decimals(小数)
>>> from fractions import Fraction
>>> Fraction(100, 48)
Fraction(25, 12)
>>> Fraction(1,3) + Fraction(2,3)
Fraction(1, 1)
>>> f = Fraction(100000, 2358)
>>> f.numerator
50000
>>> f.denominator
1179
>>> 
>>> from decimal import Decimal as D
>>> D(3.1415926)
Decimal('3.14159260000000006840537025709636509418487548828125')
>>> D('3.1415926')
Decimal('3.1415926')
>>> D(0.1) * D(3) - D(0.3)
Decimal('2.775557561565156540423631668E-17')
>>> D('0.1') * D('3') - D('0.3')
Decimal('0.0')
>>> D('2.4').as_integer_ratio()
(12, 5)
>>> 
不可变序列
  • strings(字符串)与bytes(字节):
>>> str1 = 'Hello World'
>>> str2 = "Hello World"
>>> str3 = '''Hello World
... Hello World
... Hello World'''
>>> str4 = """Hello World
... Hello World
... Hello World
... Hello World
... Hello World"""
>>> len(str1)
11
>>> str4
'Hello World\nHello World\nHello World\nHello World\nHello World'
>>> print(str4)
Hello World
Hello World
Hello World
Hello World
Hello World
>>> 
>>> s = "Hello World Long Test Text"
>>> s[0]
'H'
>>> s[6]
'W'
>>> s[:6]
'Hello '
>>> s[6:]
'World Long Test Text'
>>> s[6:26:3]
'WlLge x'
>>> s[:]
'Hello World Long Test Text'
>>> 
  • tuples(元组):
>>> t=()
>>> type(t)
<class 'tuple'>
>>> one_element_tuple=(18,)
>>> three_element_tuple=(18,30,50)
>>> a,b,c=1,2,3
>>> a,b,c
(1, 2, 3)
>>>
>>> a,b=1,2
>>> a,b=b,a
>>> a,b
(2, 1)
>>>
可变序列
  • List:
>>> []
[]
>>> list()
[]
>>> [1,2,3]
[1, 2, 3]
>>> [x + 5 for x in [2,3,4]]
[7, 8, 9]
>>> list((1,3,5,7,9))
[1, 3, 5, 7, 9]
>>> list('hello')
['h', 'e', 'l', 'l', 'o']
>>> 
  • Byte arrays:
>>> bytearray()
bytearray(b'')
>>> bytearray(10)
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
>>> bytearray(range(5))
bytearray(b'\x00\x01\x02\x03\x04')
>>> me = bytearray(b'Paul')
>>> me.endswith(b'l')
True
>>> me.upper()
bytearray(b'PAUL')
>>> me.count(b'l')
1
>>> 
Set类型
  • set 可变
  • frozenset 不可变
>>> s = set()
>>> s.add(2)
>>> s.add(5)
>>> s.add(7)
>>> s
{2, 5, 7}
>>> s.add(1)
>>> s
{1, 2, 5, 7}
>>> s.remove(1)
>>> s
{2, 5, 7}
>>> s.add(5)
>>> s
{2, 5, 7}
>>> s2 = set([5,7,11])
>>> s | s2
{2, 5, 7, 11}
>>> s & s2
{5, 7}
>>> s -  s2
{2}
>>> 
>>> fs = frozenset([2,5,7])
>>> fs.add(2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'frozenset' object has no attribute 'add'
>>> fs
frozenset({2, 5, 7})
>>> 
Mapping类型 - dictionaries(字典)
>>> a = dict(A=2, B=3)
>>> b = {'A': 2, 'B': 3}
>>> c = dict(zip(['A', 'B'], [2, 3]))
>>> d = dict([('A',2), ('B',3)])
>>> e = dict({'A': 2, 'B': 3})
>>> a == b == c == d == e
True
>>> 

集合模块

  • namedTuple 工厂方法创建带有名字的tuple
  • deque 列表式容器
  • ChainMap 字典式类 用于创建多个mappings
  • Counter 字典子类 用于计数哈希对象
  • OrderedDict 带顺序的字典子类
  • defaultdict 带默认值的字典子类
  • UserDict 简易的字典对象封装
  • UserList 简易的列表对象封装
  • UserString 简易的字符串对象封装

namedtuple:

>>> from collections import namedtuple
>>> Vision = namedtuple('Vision', ['left', 'right'])
>>> vision = Vision(18,30)
>>> vision.left
18
>>>

枚举类型

>>> from enum import Enum
>>> class TrafficLight(Enum):
...     GREEN=1
...     YELLOW=2
...     RED=3
...
>>> TrafficLight.GREEN
<TrafficLight.GREEN: 1>
>>> TrafficLight.GREEN.name
'GREEN'
>>> TrafficLight.GREEN.value
1
>>>

注意点

  • 很小的值的缓存
>>> a = 1
>>> b = 1
>>> id(a) == id(b)
True
>>> a = 100000
>>> b = 100000
>>> id(a) == id(b)
False
>>>
  • O(n)
  • 索引:
>>> a = list(range(5))
>>> a
[0, 1, 2, 3, 4]
>>> len(a)
5
>>> a[len(a)-1]
4
>>> a[-1]
4
>>> a[-2]
3
>>> a[-5]
0

相关文章

网友评论

      本文标题:<Python Tips> 2. 数据类型

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