Python 默认拥有以下内置数据类型:
文本类型: | str |
数值类型: | int, float, complex |
序列类型: | list, tuple, range |
映射类型: | dict |
集合类型: | set, frozenset |
布尔类型: | bool |
二进制类型: | bytes, bytearray, memoryview |
1.文本类型
#字符串
str1="A"
str2='B'
str3="china"
str4='china'
print(str1)
print(type(str1))
print(type(str2))
print(type(str3))
print(type(str4))
print(str2)
print(str3)
print(str4)
print(str1,str2,str3,str4)
输出结果:单个字符和多个字符都是str类型,不用声明类型,直接赋值
======
A
<class 'str'>
<class 'str'>
<class 'str'>
<class 'str'>
B
china
china
A B china china
>>>
字符串格式化函数:format() h和{}符号
name="china"
txt="The greatset county is {} in the world"
print(txt.format(name))
#多个值格式化
name="bill"
age=63
hobby="basketball"
str="His name is {} ,he is {} years old, and he like playing {}"
print(str.format(name,age,hobby))
#设置数字类型的格式:.2f 保留两位小数
str="His name is {} ,he is {:.2f} years old, and he like playing {}"
print(str.format(name,age,hobby))
输出结果:
The greatset county is china in the world
His name is bill ,he is 63 years old, and he like playing basketball
His name is bill ,he is 63.00 years old, and he like playing basketball
2.数值类型
数值类型包括 int 、float 、complex三种
#数值类型包括 int float complex
x1 = 8 # int
x2=127343827481538175158
x3=-4758
y1 = 7.5 # float
y2=7.63743264
y3=2e8
y4=-2.874589485
z1 = 5j # complex
z2=-7j
z3=3+6j
print(x1,y1,z1)
print(x2,y2,z2)
print(x3,y3,y4,z3)
print(type(x1),type(y1),type(z1))
print(type(x2),type(y2),type(z2))
print(type(x3),type(y3),type(y4),type(z3))
#类型转换
x4=float(x1)
y5=int(y1)
z4=complex(y1)
print(x4,y5,z4)
print(type(x4),type(y5),type(z4))
可以使用 int()、float() 和 complex() 方法从一种类型转换为另一种类型
输出结果为:
8 7.5 5j
127343827481538175158 7.63743264 (-0-7j)
-4758 200000000.0 -2.874589485 (3+6j)
<class 'int'> <class 'float'> <class 'complex'>
<class 'int'> <class 'float'> <class 'complex'>
<class 'int'> <class 'float'> <class 'float'> <class 'complex'>
8.0 7 (7.5+0j)
<class 'float'> <class 'int'> <class 'complex'>
3.序列类型
list, tuple, range
- 列表(List)是一种有序和可更改的集合。允许重复的成员。
- 元组(Tuple)是一种有序且不可更改的集合。允许重复的成员。
x = ["apple", "banana", "cherry"] | list |
x = ("apple", "banana", "cherry") | tuple |
x = range(6) | range |
list=["red","green","yellow"]
tuple=("red","green","yellow")
range(12)
range(0,12,3)
range(12,0,3)
print(list)
print(tuple)
for i in range(12):
print(i)
print("||")
for i in range(0,12,3):
print(i)
print("||")
for i in range(12,0,-3):
print(i)
输出结果:
['red', 'green', 'yellow']
('red', 'green', 'yellow')
0
1
2
3
4
5
6
7
8
9
10
11
||
0
3
6
9
||
12
9
6
3
4.映射类型
#基本语法
x={"name":"China","Location":"East"}
print(x)
print(type(x))
输出结果:
{'name': 'China', 'Location': 'East'}
<class 'dict'>
5.集合类型
集合类型有set和frozenset两种
- 集合(Set)是一个无序和无索引的集合。没有重复的成员。
#set基本语法
x={"China","USA","Russia"}
print(x)
#frozenset基本语法
y={("China","USA","Russia")}
print(y)
输出结果:无序
{'USA', 'Russia', 'China'}
{('China', 'USA', 'Russia')}
6.布尔类型
大多数值都为 True
如果有某种内容,则几乎所有值都将评估为 True。
除空字符串外,任何字符串均为 True。
除 0 外,任何数字均为 True。
除空列表外,任何列表、元组、集合和字典均为 True。
除空值(例如 ()、[]、{}、""、数字 0 和值 None)外,没有多少值会被评估为 False。当然,值 False 的计算结果为 False。
#bool
print(bool("abc"))
print(bool(10))
print(bool(-100))
print(bool(0.0005))
print(bool(["red","yellow","blue"]))
print(bool(False))#区分大小写,首字母大写
print(bool(None))
print(bool(0))
print(bool(""))#空字符
print(bool(()))#空tuple
print(bool([]))#空list
print(bool({}))#空set
输出结果:
True
True
True
True
True
False
False
False
False
False
False
False
7.二进制类型
bytes, bytearray, memoryview
#二进制类型
x = b"Hello"
y=bytearray(5)
z=memoryview(bytes(6))
print(x)
print(y)
print(z)
输出结果:
b'Hello'
bytearray(b'\x00\x00\x00\x00\x00')
<memory at 0x000001DC6840EA08>