编程语言
首页 > 编程语言> > python基本数据类型

python基本数据类型

作者:互联网

1、整型int
作用:年龄、等级、各种号码
age = 18
print(type(age))

2、浮点型float
作用:记录薪资、身高、体重等
salary = 5.5
print(type(salary))
height = 1.81
weight = 73.5

3、字符串str
作用:记录名字、字符相关的描述性的内容
name = "eg on你"
name = 'eg on你'
msg = """
eg on你
"""

msg= "my name is 'egon'"
print(msg)

4、列表类型list
作用:按照位置存放多个值,可以用索引取指定位置的值
hobbies = "read music play"
0 1 2
hobbies = ["read", "music", "play"]
print(hobbies,type(hobbies))
print(hobbies[0])
print(hobbies[1])

0 1 2 3
l = [10,3.3,"aaa",[666,777]]
print(l[3])
print(l[-1])

print(l[3][0])

5、字典类型dict
作用:按照key:value的形式存放多个值,其中value可以是任意类型,而key通常是str类型
info = ["egon",18,"male",10]

info = {
"name":"egon",
"age":18,
"gender":"male",
"level":10
}
print(info["age"])

dic = {
"k1":111,
"k2":[666,"xxx",{"kkk":7777}]
}

print(dic["k2"][-1]['kkk'])


6、布尔类型bool
(1)显式的布尔值:
x = True
y = False
print(type(x))

age1 = 18
age2 = 20
print(age1 > age2)
print(age1 == age2)

(2)隐式的布尔值: 0、None、空的布尔值为False,其余均为真
if 0:
print('ok')

标签:基本,name,python,age,数据类型,布尔值,hobbies,print,type
来源: https://www.cnblogs.com/lcdandcht/p/14538346.html