学习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里,变量是没有数据类型的,我们所说变量的数据类型,其实是变量对应的值的数据类型。也就是说,对于同一个变量,其查询的类型会随赋予变量的内容的改变而改变。