小码哥的IT人生

Python 数据类型

Python基础 2022-06-06 12:52:26小码哥的IT人生shichen

Python 数据类型

内置数据类型

在编程中,数据类型是一个重要的概念。

变量可以存储不同类型的数据,并且不同类型可以执行不同的操作。

在这些类别中,Python 默认拥有以下内置数据类型:

文本类型: str
数值类型: int, float, complex
序列类型: list, tuple, range
映射类型: dict
集合类型: set, frozenset
布尔类型: bool
二进制类型: bytes, bytearray, memoryview

获取数据类型

您可以使用 type() 函数获取任何对象的数据类型:

示例代码:

打印变量 x 的数据类型:

x = 10
print(type(x))

完整实例:

x = 10
print(type(x))

设置数据类型

在 Python 中,当您为变量赋值时,会设置数据类型:

示例 数据类型 试一试
x = "Hello World" str 试一试
x = 29 int 试一试
x = 29.5 float 试一试
x = 1j complex 试一试
x = ["apple", "banana", "cherry"] list 试一试
x = ("apple", "banana", "cherry") tuple 试一试
x = range(6) range 试一试
x = {"name" : "Bill", "age" : 63} dict 试一试
x = {"apple", "banana", "cherry"} set 试一试
x = frozenset({"apple", "banana", "cherry"}) frozenset 试一试
x = True bool 试一试
x = b"Hello" bytes 试一试
x = bytearray(5) bytearray 试一试
x = memoryview(bytes(5)) memoryview 试一试

设定特定的数据类型

如果希望指定数据类型,则您可以使用以下构造函数:

示例 数据类型 试一试
x = str("Hello World") str 试一试
x = int(29) int 试一试
x = float(29.5) float 试一试
x = complex(1j) complex 试一试
x = list(("apple", "banana", "cherry")) list 试一试
x = tuple(("apple", "banana", "cherry")) tuple 试一试
x = range(6) range 试一试
x = dict(name="Bill", age=36) dict 试一试
x = set(("apple", "banana", "cherry")) set 试一试
x = frozenset(("apple", "banana", "cherry")) frozenset 试一试
x = bool(5) bool 试一试
x = bytes(5) bytes 试一试
x = bytearray(5) bytearray 试一试
x = memoryview(bytes(5)) memoryview 试一试

完整实例1:

x = "Hello World"
#display x:
print(x)
#display the data type of x:
print(type(x))

完整实例2:

x = 20
#display x:
print(x)
#display the data type of x:
print(type(x))

完整实例3:

x = 20.5
#display x:
print(x)
#display the data type of x:
print(type(x))

完整实例4:

x = 1j
#display x:
print(x)
#display the data type of x:
print(type(x))

完整实例5:

x = ["apple", "banana", "cherry"]
#display x:
print(x)
#display the data type of x:
print(type(x))

完整实例6:

x = ("apple", "banana", "cherry")
#display x:
print(x)
#display the data type of x:
print(type(x))

完整实例7:

x = range(6)
#display x:
print(x)
#display the data type of x:
print(type(x))

完整实例8:

x = {"name" : "Bill", "age" : 63}
#display x:
print(x)
#display the data type of x:
print(type(x))

完整实例9:

x = {"apple", "banana", "cherry"}
#display x:
print(x)
#display the data type of x:
print(type(x))

完整实例10:

x = frozenset({"apple", "banana", "cherry"})
#display x:
print(x)
#display the data type of x:
print(type(x))

完整实例11:

x = True
#display x:
print(x)
#display the data type of x:
print(type(x))

完整实例12:

x = b"Hello"
#display x:
print(x)
#display the data type of x:
print(type(x))

完整实例13:

x = bytearray(5)
#display x:
print(x)
#display the data type of x:
print(type(x))

完整实例14:

x = memoryview(bytes(5))
#display x:
print(x)
#display the data type of x:
print(type(x))

完整实例15:

x = str("Hello World")
#display x:
print(x)
#display the data type of x:
print(type(x))

完整实例16:

x = int(20)
#display x:
print(x)
#display the data type of x:
print(type(x))

完整实例17:

x = float(20.5)
#display x:
print(x)
#display the data type of x:
print(type(x))

完整实例18:

x = complex(1j)
#display x:
print(x)
#display the data type of x:
print(type(x))

完整实例19:

x = list(("apple", "banana", "cherry"))
#display x:
print(x)
#display the data type of x:
print(type(x))

完整实例20:

x = tuple(("apple", "banana", "cherry"))
#display x:
print(x)
#display the data type of x:
print(type(x))

完整实例21:

x = range(6)
#display x:
print(x)
#display the data type of x:
print(type(x))

完整实例22:

x = dict(name="John", age=36)
#display x:
print(x)
#display the data type of x:
print(type(x))

完整实例23:

x = set(("apple", "banana", "cherry"))
#display x:
print(x)
#display the data type of x:
print(type(x))

完整实例24:

x = frozenset(("apple", "banana", "cherry"))
#display x:
print(x)
#display the data type of x:
print(type(x))

完整实例25:

x = bool(5)
#display x:
print(x)
#display the data type of x:
print(type(x))

完整实例26:

x = bytes(5)
#display x:
print(x)
#display the data type of x:
print(type(x))

完整实例27:

x = bytearray(5)
#display x:
print(x)
#display the data type of x:
print(type(x))

完整实例28:

x = memoryview(bytes(5))
#display x:
print(x)
#display the data type of x:
print(type(x))

版权所有 © 小码哥的IT人生
Copyright © phpcodeweb All Rights Reserved
ICP备案号:苏ICP备17019232号-2  

苏公网安备 32030202000762号

© 2021-2024