数据类型就是变量值的不同类型,不同的变量值会有不同的数据类型。
整数型(int)就是普通的整数数字,如-1,12,180等。可以进行加减乘除、逻辑判断(大于,小于)。
height = 180
print(height)
print(id(height))
print(type(height))
180
1459580816
<class 'int'>
浮点型(float)就是带小数点的数字,如-12.3,-555.345等,可以进行加减乘除、逻辑判断(大于,小于)。
score = 75.5
print(score)
print(id(score))
print(type(score))
75.5
4447536
<class 'float'>
用‘‘,"",单引号和双引号括起来的文本就是字符串类型,只能+、*和逻辑判断(大于,小于)
name = 'stone'
print(name)
print(id(name))
print(type(name))
stone
19983584
<class 'str'>
name = 'stone'
print(name*10)
stonestonestonestonestonestonestonestonestonestone
字符串的乘法只能乘以数字。
name = 'stone'
age = '19'
print(name+age)
stone19
列表(list)[]内使用逗号分隔开多个元素,元素可以为任何数据类型
hobby_list = ['reading','fishing','looking','listening']
# 索引值 0 1 2 3
print(hobby_list)
print(id(hobby_list))
print(type(hobby_list))
['reading', 'fishing', 'looking', 'listening']
19875280
<class 'list'>
hobby_list = ['reading','fishing','looking','listening']
print(hobby_list[0])
print(hobby_list[-1])
reading
listening
定义:在{}内用逗号分隔开多个元素,每一个元素都是key:value的格式,其中value是任意格式的数据类型,key由于具有描述性的作用,所以key通常是字符类型。
作用:用来存取多个值,按照key:value的方式存值,取的时候通常用key而非索引去取值,key对value具有描述性的作用。
取值:字典取值方式不再依赖索引,而是依赖于key,通过[key]即可获取key对应的value值。
# 字典套列表
stone_info = {'name':'stone','femal':'male','age':26,'compoy_list':['nick','shanghai',29]}
print(stone_info['age'])
print(stone_info['compoy_list'][1])
26
shanghai
# 字典套字典
stone_info = {'name':'stone','femal':'male','age':26,'company_info':{'name1':'nick','place':'shanghai','age1':29}}
print(stone_info['age'])
print(stone_info['company_info']['place'])
26
shanghai
students = [
{'name' :'nick','age':29,'hobby':'female'},
{'name':'stone','age':18,'hobby':'listening'}]
print(students[1]['name'])
stone
True or False
布尔类型一般不用于打印、定义变量 ,用于判断条件结果。只有0,None,空,False的布尔值为False,其余的为True。
print(True)
print(type(True))
True
<class 'bool'>
print(bool(0))
print(bool('stone'))
print(bool(1>2))
print(bool(1==1))
False
True
False
True
原文:https://www.cnblogs.com/zuihoudebieli/p/10897261.html