Python学习【1.3.1】-数据类型

版权声明:版权所有,翻版必究【Kevin】 https://blog.csdn.net/weixin_30935137/article/details/82709320

Python 数据类型

1)数字(Numbers)

整型: int
浮点型: float
复数:complex

a = 3
b = 2.0
c = 1.23 + 4.56J

print(type(a),type(b),type(c))

结果如下:
<class 'int'> <class 'float'> <class 'complex'>

2)字符串(String)

a = 'wujiadong'
b = "zhangdandan"
print(a,type(a))
print(b,type(b))

结果如下:
wujiadong <class 'str'>
zhangdandan <class 'str'>

3)列表(List)

a = [1,2,3,4]
b = [1,'wu',2.0,[1,2,3]]

print(a,type(a))
print(b,type(b))

结果如下:
[1, 2, 3, 4] <class 'list'>
[1, 'wu', 2.0, [1, 2, 3]] <class 'list'>


可变


4)元组(Tuple)

a = (1,2,3) 
b = (123,'wujiadong')

print(a,type(a))
print(b,type(b))

结果如下:
(1, 2, 3) <class 'tuple'>
(123, 'wujiadong') <class 'tuple'>

不可变

5)字典(Dictionary)

dic = {"name":"wujiadong","age":27}
print(dic)

结果如下:
{'name': 'wujiadong', 'age': 27}

6)集合(Set)

name1 = {'a','b','c','d'}
name2 = set('a')

print(name1,type(name1))
print(name2,type(name2))

结果如下:
{'c', 'a', 'd', 'b'} <class 'set'>
{'a'} <class 'set'>


7)布尔型(boolen)

a = True
b = False
print(a == 1)
print(b == 0)
print(2 > 3)

数据类型转换

1)基本数据类型间的转换


a = in(x) # 将x转换为整数
b = float(x) #将x转换为浮点数
c = str(x) # 将x转换为字符串
d = list(x) # 将x转换为列表
e = tupel(x) # 将x转换为元组


其它



1. 字符串(string),列表(list),元组(tuple)都属于序列(sequence)



猜你喜欢

转载自blog.csdn.net/weixin_30935137/article/details/82709320
今日推荐