zoukankan      html  css  js  c++  java
  • Python 数据类型

    这里我们主要描述基础的数值类型、序列类型和散列类型。每种数据类型后续有详解。


    1.数值类型

    数值类型就是我们平常用来做计算的数字类型,在python中有如下几种数值类型:

     1.1整型:

    >>> a = 123
    >>> type(a)
    <class 'int'>
    >>> 
    

    1.2浮点型:

    >>> f1 = 1.23
    >>> type(f1)
    <class 'float'>
    >>> 
    

    1.3布尔型:

    >>> b1 = True
    >>> type(b1)
    <class 'bool'>
    >>> 

    1.4复数型:

    >>> c1 = 1+2.3j     #复数型(在python中用小写 j ,表示虚部,用其他的字母不行)
    >>> type(c1)
    <class 'complex'>
    >>> 

     


    2.序列类型

    在python中三种序列类型,序列类型简单来说就是对象里面的元素是有顺序的。

     2.1字符串(str)

    s1 = 'ab12!'
    >>> s1
    'ab12!'
    >>> type(s1)
    <class 'str'>
    >>> 

    2.2列表(list)

    >>> li = ['abc',123,'!']
    >>> li
    ['abc', 123, '!']
    >>> type(li)
    <class 'list'>
    >>> 
    

    2.3元组(tuple)

    >>> tu = ('abc',123,2.2,'!')
    >>> tu
    ('abc', 123, 2.2, '!')
    >>> type(tu)
    <class 'tuple'>
    >>> 
    

     


    3.散列类型 

     3.1集合(set)

    注意创建空集合要使用工厂方法se = set(),不能使用se = {}

    >>> se = {'2',1,23}
    >>> se
    {'2', 1, 23}
    >>> type(se)
    <class 'set'>
    >>> se1 = set()
    >>> type(se1)
    <class 'set'>
    >>> 
    >>> se2 = {}
    >>> type(se2)
    <class 'dict'>
    >>> 
    

     3.2字典(dict) 

    >>> di = {'a':'a1','b':22,33:44}
    >>> di
    {'a': 'a1', 'b': 22, 33: 44}
    >>> type(di)
    <class 'dict'>
    >>> 
    

     


    区分可变与不可变的数据类型: 

    可变:列表、集合、字典

    不可变:int(整型)、浮点型、复数、布尔型、字符串、元组。

    >>> 
    >>> #1,整型是不可变的,因为它的值发生变化后,对应的id也随着改变了。
    >>> a = 123
    >>> type(a)
    <class 'int'>
    >>> id(a)
    1473998880
    >>> a =234
    >>> id(a)
    1474002432
    >>> 
    >>>#2,浮点型是不可变的,因为它的值发生变化后,对应的id也随着改变了。
    >>> f = 1.2
    >>> type(f)
    <class 'float'>
    >>> id(f)
    5777976
    >>> f = 1.23
    >>> id(f)
    5776296
    >>> 
    >>> #3,复数是不可变的,因为它的值发生变化后,对应的id也随着改变了。
    >>> c = 1+2.3j
    >>> type(c)
    <class 'complex'>
    >>> id(c)
    45359024
    >>> c = 2+2.3j
    >>> id(c)
    45358032
    >>> 
    >>> #4,字符串是不可变的,因为它的值发生变化后,对应的id也随着改变了。
    >>> s = 'abc'
    >>> type(s)
    <class 'str'>
    >>> id(s)
    34884664
    >>> s=s.replace('c','3')
    >>> s
    'ab3'
    >>> id(s)
    45947680
    >>> 
    >>> #5,列表是可变的,因为它的值发生变化后,对应的id没有改变。
    >>> li = [1,2,3]
    >>> type(li)
    <class 'list'>
    >>> id(li)
    46027144
    >>> li.append('a')
    >>> li
    [1, 2, 3, 'a']
    >>> id(li)
    46027144
    >>> 
    >>> #6,元组是不可变的。
    >>> tu = (1,2,3,'abc',[7,8,9])
    >>> type(tu)
    <class 'tuple'>
    >>> 
    >>> #7,集合是可变的,因为它的值发生变化后,对应的id没有改变。
    >>> se = {1,2,'abc'}
    >>> type(se)
    <class 'set'>
    >>> id(se)
    45495016
    >>> se.update('a')
    >>> se
    {1, 2, 'a', 'abc'}
    >>> id(se)
    45495016
    >>> 
    >>> #8,字典是可变的,因为它的值发生变化后,对应的id没有改变。
    >>> di = {'a':1,'b':2}
    >>> type(di)
    <class 'dict'>
    >>> id(di)
    46042424
    >>> di['a'] = 'a'
    >>> di
    {'a': 'a', 'b': 2}
    >>> id(di)
    46042424
    >>> 
    >>> #9,布尔型是不可变的。
    >>> b = True
    >>> type(b)
    <class 'bool'>
    >>> id(b)
    1473503456
    >>> b = False
    >>> id(b)
    1473503488
    

      

  • 相关阅读:
    luogu P2827 蚯蚓
    CHOI1001/1002 火车进出栈问题
    hdoj4699 Editor
    反弹shell监控
    AppScan 9.0.3.6 crack
    Spectre & Meltdown Checker – CPU芯片漏洞检查脚本Linux版
    Microsoft IIS WebDav 'ScStoragePathFromUrl' Remote Buffer Overflow (CVE-2017-7269)
    Shodan新手使用指南
    The Art of Subdomain Enumeration (转)
    DDOS攻击方式总结 (转)
  • 原文地址:https://www.cnblogs.com/longxd/p/8488392.html
Copyright © 2011-2022 走看看