1. Python 变量和数据类型

Python-变量

  • 变量不需要声明。变量也没有类型。使用前必须初始化(赋值操作)

  • 变量在第一次赋值的时候被创建,创建之后的变量包含以下信息:

    1. 变量的标识(内存地址): The identifier used to reference the variable
    2. 变量的类型:这里所说的变量的类型,其实是指Value 的类型。The data type of the value.
    3. 变量的值: The data assigned to the variable.

Python 变量赋值的特殊写法(在一行代码中,同时赋值多个变量)

1
2
3
4
5
6
def assign_special():
a = b = c = 1
print(f"a = {a}, b = {b}, c = {c}") # a = 1, b = 1, c = 1
d, e, f = 1, 2, "john"
print(f"d = {d}, e = {e}, f = {f}") # d = 1, e = 2, f = john

上述代码的输出:

a = 1, b = 1, c = 1

d = 1, e = 2, f = john

Python-标准数据类型

Python 中有六个标准数据类型:

  1. 不可变数据:Number, string, tuple

    Number 有四种类型: int, float, complex, bool; 这四种类型均继承自numbers.Number

  2. 可变数据: list, dict, set

可变类型和不可变类型的特点:

Immutable Data Types:

  • Immutability: Cannot be changed after they are created.Any modification to an immutable obj results in the creation of a new obj.
  • Hashable: Can be used as keys in dicts and elements in sets.

Mutable Date Types:

  • Mutability: Can be changed after they are created.(add, remove, etc.)
  • Not Hashable: Cannot be used as keys in dicts or elements in sets

“In Python, an object is considered hashable if it has a hash value that remains constant during its lifetime.”


Number

1
2
3
4
5
6
7
8
9
10
def number_type():
a, b, c, d = 20, 5.5, 1 + 3j, True
print(f"type:a = {type(a)}, type:b = {type(b)}, type:c = {type(c)}, type:d = {type(d)}")
"""
type:a = <class 'int'>, type:b = <class 'float'>, type:c = <class 'complex'>, type:d = <class 'bool'>
"""
print(issubclass(int, numbers.Number)) #True
print(issubclass(float, numbers.Number)) #True
print(issubclass(complex, numbers.Number)) #True
print(issubclass(bool, numbers.Number)) #True

这里只介绍取商运算符和复数,其他略

Number中的一个特殊运算符操作//(取商) 做如下展示:

1
2
3
4
5
6
7
def division_notice():
a, b = 2, 4
c = a / b # 获取float 值
d = a // b # 只取商
e = a % b # 取余
print(f"c = {c}, d = {d}, e = {e}") # c = 0.5, d = 0, e = 2

上述代码输出:
c = 0.5, d = 0, e = 2

复数:通过j作为数字的后缀,来创建。

1
2
3
4
5
6
7
8
9
10
11
def complex_notice():
a = 2j
c = 3 + 1.5j
"""
complex.real 获取实数部分
complex.imag 获取虚数部分
"""
print(f"a = {a}, a.real = {a.real} , a.imag = {a.imag}")
## a = 2j, a.real = 0.0 , a.imag = 2.0
print(f"c = {c}, c.real = {c.real} , c.imag = {c.imag}")
## c = (3+1.5j), c.real = 3.0 , c.imag = 1.5

str,tuple 和 list,dict,set的关系

String, Tuple, List, Dictionary, and Set in Python share common characteristics because they all inherit from certain base classes that provide these functionalities. The common base classes include:

  1. Iterable: All these types inherit from the Iterable class, which allows them to be iterated over.
  2. Container: They inherit from the Container class, which allows the use of the in keyword to check for containment.
  3. Sized: They inherit from the Sized class, which provides the len() function to get the number of elements.

Here is a brief overview of the inheritance hierarchy:

  • String (str): Inherits from Sequence, which in turn inherits from Iterable, Container, and Sized.
  • Tuple (tuple): Inherits from Sequence, which in turn inherits from Iterable, Container, and Sized.
  • List (list): Inherits from MutableSequence, which in turn inherits from Sequence, Iterable, Container, and Sized.
  • Dictionary (dict): Inherits from MutableMapping, which in turn inherits from Mapping, Iterable, Container, and Sized.
  • Set (set): Inherits from MutableSet, which in turn inherits from Set, Iterable, Container, and Sized.

这也是为什么 len(),in,iterate 的操作可以在这几个数据类型上使用的原因。

用代码查看如下:

1
2
3
4
5
print(issubclass(str, (Iterable, Container, Sized)))
print(issubclass(tuple, (Iterable, Container, Sized)))
print(issubclass(list, (Iterable, Container, Sized)))
print(issubclass(dict, (Iterable, Container, Sized)))
print(issubclass(set, (Iterable, Container, Sized)))

上述结果均输出:True


1. Python 变量和数据类型
https://jackiedai.github.io/2025/03/04/011Python/002Python变量和数据类型/
Author
lingXiao
Posted on
March 4, 2025
Licensed under