学习python的第二天之数据类型

学习python的第二天之数据类型

数据类型

  • Numbers(数字)

    num_int = 45 #int 整数类型
    num_float = 3.1415926 #float 浮点数类型
    num_complex = 1 + 2j #complex 复数类型
    
  • String(字符串)

    字符串的内容需要用‘ ‘或“ ”或’‘’ ‘’‘或”“” “”“引起来。

    string_1 = 'hello' #str 字符串类型
    string_2 = "hello"
    string_3 = '''hello'''
    string_4 = """hello"""
    string_5 = '45' #str 字符串类型
    
  • bool(布尔类型)

    bool_true_1 = True #True 布尔类型
    bool_true_2 = 2 > 1 #True 布尔类型
    bool_False_1 = False #False 布尔类型
    bool_False_2 = 2 < 1 #False 布尔类型
    
  • List(列表)

    list_1 = ['apple', 'pear', 'orange', 'banana'] #list 列表类型
    
  • Dictionary(字典)

    dictionary = {'name': 'Tom', 'age': 18, 'addr': 'china'} #dict 字典类型
    
  • Tuple(元组)

    tuple_1 = (0, 2, 4, 6, 8, 10, 1, 3, 5, 7, 9) #tuple 元组类型
    
  • Set(集合)

    set_1 = {1, 'hello', 2, '呵呵', 3, True}
    
Python3 已经废弃long类型

查看数据类型

使用内置type()类

a = 12
b = 3.14
c = 'hello'
d = True
e = ['apple', 'pear', 'orange', 'banana']
f = {'name': 'Tom', 'age': 18, 'addr': 'china'}
g = (0, 2, 4, 6, 8, 10, 1, 3, 5, 7, 9)
h = {1, 'hello', 2, '呵呵', 3, True}
print(type(a)) #--> <class 'int'>
print(type(b)) #--> <class 'float'>
print(type(c)) #--> <class 'str'>
print(type(d)) #--> <class 'bool'>
print(type(e)) #--> <class 'list'>
print(type(f)) #--> <class 'dict'>
print(type(g)) #--> <class 'tuple'>
print(type(h)) #--> <class 'set'>

在Python里,变量是没有数据类型的,我们所说变量的数据类型,其实是变量对应的值的数据类型。也就是说,对于同一个变量,其查询的类型会随赋予变量的内容的改变而改变。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值