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

Python3数据类型 #81

Open
Qingquan-Li opened this issue Feb 21, 2018 · 0 comments
Open

Python3数据类型 #81

Qingquan-Li opened this issue Feb 21, 2018 · 0 comments
Labels

Comments

@Qingquan-Li
Copy link
Owner

Qingquan-Li commented Feb 21, 2018

Python3 六个标准的数据类型。
四种数据结构:元组、列表、集合、字典。


通过 type() 函数查看数据类型:

>>> print(type(1))
<class 'int'>

>>> import math
>>> print(type(math.pi))
<class 'float'>

>>> print(type(True and False))
<class 'bool'>

>>> print(type(complex(1, 2)))
<class 'complex'>
>>> print(type('1'))
<class 'str'>
>>> print(type((1,2,3)))
<class 'tuple'>
>>> print(type([1,2,3]))
<class 'list'>
>>> print(type({1,2,3}))
<class 'set'>
>>> print(type({'a':1,'b':2,'c':"123"}))
<class 'dict'>

Number(数字)

Python3 支持 intfloatboolcomplex(复数)

  1. Int:整数类型。在Python 3里,只有一种整数类型 int ,表示为长整型,没有 python2 中的 Long。
  2. Float:浮点类型。数值的除法 / 总是返回一个浮点数,要获取整数使用 // 操作符。在混合计算时, Python 会把整型转换成为浮点数。
  3. Bool:布尔类型。在 Python2 中是没有布尔型的,它用数字 0 表示 False,用 1 表示 True。到 Python3 中,把 True 和 False (注意首字母须大写)定义成关键字了,但它们的值还是 1 和 0,它们可以和数字相加。
  4. Complex:复数。 Python 还支持复数,复数由实数部分和虚数部分构成,可以用 a + bj ,或者 complex(a,b) 表示, 复数的实部 a 和虚部 b 都是浮点型。

String(字符串)

  1. Python 中的字符串用单引号 ' 或双引号 " 括起来,同时使用反斜杠 \ 转义特殊字符。
  2. Python 使用反斜杠 \ 转义特殊字符,如果你不想让反斜杠发生转义,可以在字符串前面添加一个 r ,表示原始字符串。
  3. 另外,反斜杠 \ 可以作为续行符,表示下一行是上一行的延续。也可以使用 """ """ 或者 ''' ''' 跨越多行。

Tuple(元组)

  1. 元组(tuple)与列表类似,不同之处在于元组的元素不能修改。元组写在小括号 () 里,元素之间用逗号隔开。
  2. 元组中的元素类型也可以不相同
  3. 示例:
    >>> letters = ('a','b','c','d','e','f','g')
    >>> letters[0]
    'a'

List(列表)(类似 Java List 集合接口)

  1. 列表是写在方括号 [] 之间、用逗号分隔开的元素列表。
  2. 列表可以完成大多数集合类的数据结构实现。列表中元素的类型可以不相同,它支持数字,字符串甚至可以包含列表(所谓嵌套)。
  3. 列表中的元素是可以改变的。
  4. Python 没有单独的字符类型,一个字符就是长度为 1 的字符串。内置函数 len() 返回字符串长度。
  5. 示例:
    >>> Weekday = ['Monday','Tuesday','Wednesday','Thursday','Friday']
    >>> print(Weekday[0])
    Monday

Sets(集合)(类似 Java Set 集合接口)

  1. 集合(set)是一个无序不重复元素的序列。
  2. 使用大括号 {} 或者 set() 函数创建集合,注意:创建一个空集合必须用 set() 而不是 {} ,因为 {} 是用来创建一个空字典。
  3. 集合不能被切片也不能被索引,除了做集合运算之外,集合元素可以被添加还有删除:
    >>> a_set = {1,2,3,4}
    >>> a_set.add(5)
    >>> print(a_set)
    {1, 2, 3, 4, 5}
    >>> a_set.discard(5)
    >>> print(a_set)
    {1, 2, 3, 4}

https://stackoverflow.com/questions/2831212/python-sets-vs-lists :

  • Lists are slightly faster than sets when you just want to iterate over the values.
  • Sets, however, are significantly faster than lists if you want to check if an item is contained within it. They can only contain unique items though.

Dictionary(字典)(类似 Java Map 集合接口)

  1. 字典是一种映射类型,它的元素是键值对。
  2. 字典的关键字必须为不可变类型,且不能重复。
  3. 创建空字典使用 {}
  4. 示例:
    >>> NASDAQ_code = {
    ...     'BIDU':'Baidu',
    ...     'SINA':'Sina',
    ...     'YOKU':'Youku'
    ... }
    >>> print(NASDAQ_code)
    {'BIDU': 'Baidu', 'YOKU': 'Youku', 'SINA': 'Sina'}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant