32

python中基本的数据类型

 4 years ago
source link: https://www.tuicool.com/articles/mAJZNvZ
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

基本数据类型

数据:描述衡量数据的状态

类型:不同的事物需要不同的类型存储

  • 整型  int

    定义:年龄,手机号码等是整数的数字

    字符串b转化为整型
    b = '12'
    print(type(b))
    b = int(b)
    print(type(b))
    ​
    <class 'str'>
    <class 'int'>
  • 浮点型  float

    定义:体重,身高等有小数点的

    f1 = 1.9  #f1 = float(1.9)
    print(type(f1))
    ​
    <class 'float'>
    ​
    字符串转化为浮点型
    f2 = '1.9'
    f2 = float(f2)
    print(type(f2))
    ​
    <class 'float'>
    ​
    ​
  • 字符串  str

    定义:存一些描述性信息,存个人爱好,个人简介

    字符串引号是没有区别的,但是不可以混用。

    如果字符串中还需要引号,就必须嵌套。# s2 = 'asas"sd"'

    s1 = 'sean'   #s1 = str(sean)
    ​
    print(type(s1))
    print(id(s1))
    print(s1)
    ​
    <class 'str'>
    2945322521392
    sean
    python2:
        str本质其实是有一个拥有8个bit位的序列    
    ​
    python3:    
        str本质其实是unicode序列    
        
           
        1024G = 1T
        1024M = 1G
        1024KB = 1M
        1024B = 1KB
        1B = 8bit

    字符串拼接是开辟一个新的内存空间,将你拼接之后的值存进去。

    s3 = 'hello'
    ​
    s4 = 'world'
    ​
    print(s3 +s4)
    ​
    helloworld
  • 列表  list

    定义:存一个或者多个不同类型的值

    在编程中数数是从0开始

    y = ['大象','美女',['read','study']]
    print(y)
    print(type(y))
    ​
    print(y[0])
    ​
    打印结果:
    ['大象', '美女', ['read', 'study']]
    <class 'list'>
    大象
    在列表中还有列表取值。
    ​
    l1 = [
        ['egon',73,'烫头',['唱','跳','rap']],
        ['alex',84,'美女']
    ]
    print(l1[0][1])
    print(l1[1][2])
    print(l1[0][3][2])
    ​
    打印结果:
    73
    美女
    rap
  • 字典类型  dict

    定义方法:通过{ }存储数据,通过key:value这种映射关系定义键值对,

    每个值通过逗号进行分隔。

    d1 = {"name": 'sean', 'age': 18}  # d1 = dict({"name":'sean','age':18})
    ​
    print(d1['name'])
    print(type(d1))
    ​
    d2 = {'name':'tank','age':73,'hobby':[ 'piao', '妹子']}
    ​
    print(d2['hobby'][0])
    ​
    打印结果:
    sean
    <class 'dict'>
    piao
  • 布尔类型

    定义:主要用于判断事物的对错

    一般布尔类型不会单独定义

    tag = True  # tag = bool(True)
    tag1 = False  # tag = bool(False)
    a = 1
    b = 1
    print(a == b)  # 等号比较的是value(值)
    print(a is b)  # is比较的是id(地址)
    ​
    打印结果:
    True
    True

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK