跳到主要内容

数据类型

Python 是一种动态类型语言,变量不需要声明类型,但每个值都有特定的类型。Python 提供了丰富的内置数据类型。

数字类型

Python 支持多种数字类型,包括整数、浮点数、复数和布尔值。

整数 (int)

整数是没有小数部分的数字,可以是正数、负数或零。

# 整数示例
x = 10 # 正整数
y = -5 # 负整数
z = 0 # 零

# 大整数(Python 自动支持大整数)
big_number = 123456789012345678901234567890

# 不同进制表示
binary = 0b1010 # 二进制(10)
octal = 0o12 # 八进制(10)
hexadecimal = 0x0A # 十六进制(10)

print(binary, octal, hexadecimal) # 输出: 10 10 10

浮点数 (float)

浮点数是带有小数部分的数字。

# 浮点数示例
pi = 3.14159
negative = -2.5
scientific = 1.23e-4 # 科学计数法(0.000123)

print(pi) # 输出: 3.14159
print(scientific) # 输出: 0.000123

# 浮点数精度问题(注意)
print(0.1 + 0.2) # 输出: 0.30000000000000004
# 使用 round() 或 decimal 模块解决精度问题
print(round(0.1 + 0.2, 2)) # 输出: 0.3

复数 (complex)

复数由实部和虚部组成,虚部用 j 表示。

# 复数示例
z1 = 3 + 4j
z2 = complex(2, 3)

print(z1) # 输出: (3+4j)
print(z1.real) # 实部: 3.0
print(z1.imag) # 虚部: 4.0
print(z1.conjugate()) # 共轭复数: (3-4j)

布尔值 (bool)

布尔值是整数的子类,只有两个值:TrueFalse

# 布尔值
is_true = True
is_false = False

print(is_true) # 输出: True
print(type(is_true)) # 输出: <class 'bool'>

# 布尔值是整数的子类
print(True + 1) # 输出: 2
print(False * 10) # 输出: 0

# 真值测试
print(bool(0)) # 输出: False
print(bool(1)) # 输出: True
print(bool("")) # 输出: False
print(bool("hello")) # 输出: True
print(bool([])) # 输出: False
print(bool([1, 2])) # 输出: True

数字运算

算术运算符

a, b = 10, 3

print(a + b) # 加法: 13
print(a - b) # 减法: 7
print(a * b) # 乘法: 30
print(a / b) # 除法: 3.3333...(浮点数)
print(a // b) # 整除: 3
print(a % b) # 取余: 1
print(a ** b) # 幂运算: 1000

数学函数

import math

# 常用数学函数
print(abs(-5)) # 绝对值: 5
print(round(3.14159, 2)) # 四舍五入: 3.14
print(pow(2, 3)) # 幂运算: 8
print(max(1, 5, 3)) # 最大值: 5
print(min(1, 5, 3)) # 最小值: 1

# math 模块
print(math.sqrt(16)) # 平方根: 4.0
print(math.floor(3.7)) # 向下取整: 3
print(math.ceil(3.2)) # 向上取整: 4
print(math.pi) # π: 3.141592653589793
print(math.e) # e: 2.718281828459045
print(math.sin(math.pi/2)) # 正弦: 1.0
print(math.log(10)) # 自然对数: 2.302585...
print(math.log10(100)) # 以10为底的对数: 2.0

字符串 (str)

字符串是不可变的字符序列。

创建字符串

# 单引号和双引号
str1 = 'Hello'
str2 = "World"

# 三引号(多行字符串)
str3 = '''
这是一个
多行字符串
'''

# 原始字符串(不转义)
path = r"C:\Users\name\Desktop"

# 字节字符串
bytes_str = b"Hello"

print(str1, str2, str3, path)

字符串操作

字符串拼接

# 使用 + 拼接
greeting = "Hello" + " " + "World"

# 使用 * 重复
repeat = "Ha" * 3 # "HaHaHa"

# 使用 join() 拼接列表
words = ["Python", "is", "awesome"]
sentence = " ".join(words) # "Python is awesome"

# 使用格式化字符串(推荐)
name = "Alice"
age = 25
message = f"我叫{name},今年{age}岁"

字符串索引和切片

text = "Hello, World!"

# 索引(从0开始)
print(text[0]) # 'H'
print(text[-1]) # '!'

# 切片 [start:end:step]
print(text[0:5]) # 'Hello'(不包含5)
print(text[7:]) # 'World!'
print(text[:5]) # 'Hello'
print(text[::2]) # 'Hlo ol!'(每隔一个字符)
print(text[::-1]) # '!dlroW ,olleH'(反转)

字符串方法

text = "  Hello, World!  "

# 大小写转换
print(text.upper()) # ' HELLO, WORLD! '
print(text.lower()) # ' hello, world! '
print(text.title()) # ' Hello, World! '
print(text.capitalize()) # ' hello, world! '
print(text.swapcase()) # ' hELLO, wORLD! '

# 去除空白
print(text.strip()) # 'Hello, World!'
print(text.lstrip()) # 'Hello, World! '
print(text.rstrip()) # ' Hello, World!'

# 查找和替换
print(text.find("World")) # 7
print(text.replace("World", "Python")) # ' Hello, Python! '
print(text.startswith(" H")) # True
print(text.endswith("! ")) # True

# 分割和连接
sentence = "Python,is,awesome"
parts = sentence.split(",") # ['Python', 'is', 'awesome']
print(", ".join(parts)) # 'Python, is, awesome'

# 判断
print("123".isdigit()) # True
print("abc".isalpha()) # True
print("abc123".isalnum()) # True
print(" ".isspace()) # True

# 格式化
print(f"{'Name':<10}{'Age'}") # 左对齐
print(f"{'Name':>10}{'Age'}") # 右对齐
print(f"{'Name':^10}{'Age'}") # 居中

字符串格式化

f-string (Python 3.6+)

name = "Alice"
age = 25
pi = 3.14159

# 基本用法
message = f"我叫{name},今年{age}岁"

# 格式化
print(f"π的值: {pi:.2f}") # π的值: 3.14
print(f"百分比: {0.256:.2%}") # 百分比: 25.60%
print(f"科学计数: {1000000:.2e}") # 科学计数: 1.00e+06

# 对齐和宽度
print(f"{name:<10}|") # Alice |(左对齐)
print(f"{name:>10}|") # Alice |(右对齐)
print(f"{name:^10}|") # Alice |(居中)

# 数字格式化
num = 1234567
print(f"{num:,}") # 1,234,567
print(f"{num:>10,}") # 1,234,567

# 表达式
print(f"5 + 3 = {5 + 3}") # 5 + 3 = 8

# 调用方法
text = "hello"
print(f"{text.upper()}") # HELLO

format() 方法

name = "Bob"
age = 30

# 位置参数
message = "我叫{},今年{}岁".format(name, age)

# 关键字参数
message = "我叫{name},今年{age}岁".format(name=name, age=age)

# 索引
message = "{0}喜欢{1},{0}也喜欢{2}".format("Alice", "Python", "Java")

% 格式化(旧式)

name = "Charlie"
age = 35

message = "我叫%s,今年%d岁" % (name, age)
print(message)

# 格式化符号
# %s - 字符串
# %d - 整数
# %f - 浮点数
# %.2f - 保留两位小数
# %x - 十六进制
# %o - 八进制

列表 (List)

列表是可变的有序序列,可以包含不同类型的元素。

创建列表

# 创建列表
numbers = [1, 2, 3, 4, 5]
fruits = ["apple", "banana", "orange"]
mixed = [1, "hello", 3.14, True] # 混合类型
empty = [] # 空列表

# 使用 list() 构造函数
numbers2 = list([1, 2, 3, 4, 5])

# 使用 range() 创建列表
range_list = list(range(5)) # [0, 1, 2, 3, 4]

列表索引和切片

fruits = ["apple", "banana", "orange", "grape", "mango"]

# 索引
print(fruits[0]) # 'apple'
print(fruits[-1]) # 'mango'

# 切片
print(fruits[1:3]) # ['banana', 'orange']
print(fruits[2:]) # ['orange', 'grape', 'mango']
print(fruits[:3]) # ['apple', 'banana', 'orange']
print(fruits[::2]) # ['apple', 'orange', 'mango']
print(fruits[::-1]) # ['mango', 'grape', 'orange', 'banana', 'apple']

# 获取长度
print(len(fruits)) # 5

列表操作

修改列表

fruits = ["apple", "banana", "orange"]

# 修改元素
fruits[0] = "pear" # ['pear', 'banana', 'orange']

# 添加元素
fruits.append("grape") # 末尾添加
fruits.insert(1, "mango") # 在位置1插入

# 扩展列表
fruits.extend(["lemon", "kiwi"]) # 添加多个元素

# 删除元素
fruits.remove("banana") # 删除指定值
fruits.pop() # 删除并返回最后一个元素
fruits.pop(0) # 删除指定位置的元素
del fruits[0] # 删除指定位置的元素

# 清空列表
fruits.clear()

列表方法

numbers = [3, 1, 4, 1, 5, 9, 2, 6]

# 排序
numbers.sort() # 原地排序(升序)
numbers.sort(reverse=True) # 原地排序(降序)
sorted_numbers = sorted(numbers) # 返回新列表

# 反转
numbers.reverse() # 原地反转
reversed_numbers = numbers[::-1] # 返回新列表

# 统计
print(numbers.count(1)) # 计数: 2
print(numbers.index(5)) # 查找索引: 4

# 复制
numbers_copy = numbers.copy() # 浅拷贝
numbers_copy2 = numbers[:] # 浅拷贝
numbers_copy3 = list(numbers) # 浅拷贝

列表运算

# 拼接
list1 = [1, 2, 3]
list2 = [4, 5, 6]
combined = list1 + list2 # [1, 2, 3, 4, 5, 6]

# 重复
repeated = [1, 2] * 3 # [1, 2, 1, 2, 1, 2]

# 成员检查
print(3 in list1) # True
print(5 not in list1) # True

列表推导式

列表推导式是创建列表的简洁方式。

# 基本语法
numbers = [x for x in range(10)] # [0, 1, 2, ..., 9]

# 带条件
evens = [x for x in range(10) if x % 2 == 0] # [0, 2, 4, 6, 8]

# 带表达式
squares = [x ** 2 for x in range(5)] # [0, 1, 4, 9, 16]

# 嵌套循环
pairs = [(x, y) for x in [1, 2] for y in [3, 4]] # [(1, 3), (1, 4), (2, 3), (2, 4)]

# 字符串操作
words = ["hello", "world", "python"]
upper_words = [word.upper() for word in words] # ['HELLO', 'WORLD', 'PYTHON']

# 嵌套列表推导式
matrix = [[i * j for j in range(3)] for i in range(3)]
# [[0, 0, 0], [0, 1, 2], [0, 2, 4]]

元组 (Tuple)

元组是不可变的有序序列,一旦创建就不能修改。

创建元组

# 创建元组
coordinates = (3, 4)
colors = ("red", "green", "blue")
single = (1,) # 单元素元组(注意逗号)
empty = () # 空元组

# 不使用括号(自动打包)
x, y, z = 1, 2, 3

# 使用 tuple() 构造函数
numbers = tuple([1, 2, 3, 4, 5])

元组操作

point = (3, 4)

# 索引和切片(与列表相同)
print(point[0]) # 3
print(point[1]) # 4

# 元组解包
x, y = point
print(x, y) # 3 4

# 嵌套元组
nested = (1, (2, 3), 4)
print(nested[1][1]) # 3

# 成员检查
print(3 in point) # True

# 获取长度
print(len(point)) # 2

# 拼接(创建新元组)
point1 = (1, 2)
point2 = (3, 4)
combined = point1 + point2 # (1, 2, 3, 4)

# 重复
repeated = (1, 2) * 2 # (1, 2, 1, 2)

元组方法

numbers = (3, 1, 4, 1, 5, 9, 2, 6)

# 索引
print(numbers.index(4)) # 2

# 计数
print(numbers.count(1)) # 2

# 其他操作
print(len(numbers)) # 8
print(max(numbers)) # 9
print(min(numbers)) # 1
print(sum(numbers)) # 31

命名元组 (Named Tuple)

命名元组提供了通过名称访问字段的功能。

from collections import namedtuple

# 定义命名元组类型
Point = namedtuple('Point', ['x', 'y'])
p = Point(3, 4)

print(p.x) # 3
print(p.y) # 4
print(p[0]) # 3

# 创建默认值
Point = namedtuple('Point', ['x', 'y'], defaults=[0, 0])
p = Point() # Point(x=0, y=0)

元组 vs 列表

# 元组:不可变、更安全、更快
coordinates = (3, 4)
# coordinates[0] = 5 # 报错:TypeError

# 列表:可变、灵活
coordinates_list = [3, 4]
coordinates_list[0] = 5 # 正常

# 使用场景
# 元组:固定数据(坐标、配置、字典键)
# 列表:动态数据(集合、队列)

字典 (Dict)

字典是键值对的无序集合(Python 3.7+ 保持插入顺序)。

创建字典

# 创建字典
person = {
"name": "Alice",
"age": 25,
"city": "New York"
}

# 使用 dict() 构造函数
person2 = dict(name="Bob", age=30, city="London")

# 空字典
empty = {}
empty_dict = dict()

# 从列表创建
items = [("name", "Charlie"), ("age", 35)]
person3 = dict(items)

访问字典

person = {"name": "Alice", "age": 25, "city": "New York"}

# 访问值
print(person["name"]) # 'Alice'
print(person.get("age")) # 25

# get() 方法(提供默认值)
print(person.get("email", "Not found")) # 'Not found'

# 获取所有键、值、键值对
print(person.keys()) # dict_keys(['name', 'age', 'city'])
print(person.values()) # dict_values(['Alice', 25, 'New York'])
print(person.items()) # dict_items([('name', 'Alice'), ('age', 25), ('city', 'New York')])

# 检查键是否存在
print("name" in person) # True
print("email" in person) # False

修改字典

person = {"name": "Alice", "age": 25}

# 添加/修改键值对
person["city"] = "New York" # 添加
person["age"] = 26 # 修改

# update() 方法
person.update({"email": "alice@example.com", "age": 27})

# 删除键值对
del person["age"] # 删除
age = person.pop("age") # 删除并返回值
person.popitem() # 删除并返回最后一个键值对(Python 3.7+)

# 清空字典
person.clear()

字典方法

person = {"name": "Alice", "age": 25, "city": "New York"}

# 遍历字典
for key in person:
print(key) # name, age, city

for key, value in person.items():
print(f"{key}: {value}")

for value in person.values():
print(value)

# 复制
person_copy = person.copy()

# 创建新字典(指定默认值)
from collections import defaultdict
counts = defaultdict(int)
counts["apple"] += 1 # 不会报错

字典推导式

# 基本语法
squares = {x: x**2 for x in range(5)} # {0: 0, 1: 1, 2: 4, 3: 9, 4: 16}

# 带条件
evens = {x: x**2 for x in range(10) if x % 2 == 0} # {0: 0, 2: 4, 4: 16, 6: 36, 8: 64}

# 转换列表
words = ["apple", "banana", "cherry"]
word_lengths = {word: len(word) for word in words} # {'apple': 5, 'banana': 6, 'cherry': 6}

# 交换键值
original = {1: "one", 2: "two", 3: "three"}
swapped = {v: k for k, v in original.items()} # {'one': 1, 'two': 2, 'three': 3}

集合 (Set)

集合是无序的不重复元素集合。

创建集合

# 创建集合
numbers = {1, 2, 3, 4, 5}
fruits = {"apple", "banana", "orange"}

# 使用 set() 构造函数
numbers2 = set([1, 2, 3, 4, 5])
numbers3 = set(range(5))

# 空集合
empty = set() # 注意:{} 是空字典

# 从字符串创建
chars = set("hello") # {'h', 'e', 'l', 'o'}

集合操作

fruits = {"apple", "banana", "orange"}

# 添加元素
fruits.add("grape")

# 删除元素
fruits.remove("banana") # 元素不存在时报错
fruits.discard("mango") # 元素不存在时不报错

# 随机删除并返回
element = fruits.pop()

# 清空集合
fruits.clear()

# 成员检查
print("apple" in fruits) # True

# 获取长度
print(len(fruits)) # 3

集合运算

set1 = {1, 2, 3, 4, 5}
set2 = {4, 5, 6, 7, 8}

# 并集
print(set1 | set2) # {1, 2, 3, 4, 5, 6, 7, 8}
print(set1.union(set2)) # {1, 2, 3, 4, 5, 6, 7, 8}

# 交集
print(set1 & set2) # {4, 5}
print(set1.intersection(set2)) # {4, 5}

# 差集
print(set1 - set2) # {1, 2, 3}
print(set1.difference(set2)) # {1, 2, 3}

# 对称差集(不共有的元素)
print(set1 ^ set2) # {1, 2, 3, 6, 7, 8}
print(set1.symmetric_difference(set2)) # {1, 2, 3, 6, 7, 8}

# 子集和超集
a = {1, 2}
b = {1, 2, 3, 4}
print(a.issubset(b)) # True
print(b.issuperset(a)) # True
print(a.isdisjoint(b)) # False(有交集)

集合方法

numbers = {3, 1, 4, 1, 5, 9, 2, 6}

# 去重(自动)
print(numbers) # {1, 2, 3, 4, 5, 6, 9}

# 复制
numbers_copy = numbers.copy()

# 增量操作
set1 = {1, 2, 3}
set2 = {3, 4, 5}
set1.update(set2) # 并集(修改原集合)
set1.intersection_update(set2) # 交集(修改原集合)
set1.difference_update(set2) # 差集(修改原集合)

frozenset(不可变集合)

# 创建不可变集合
immutable = frozenset([1, 2, 3, 4, 5])

# frozenset 不能修改
# immutable.add(6) # 报错:TypeError

# 可以作为字典的键
cache = {
frozenset([1, 2]): "value1",
frozenset([3, 4]): "value2"
}

集合推导式

# 基本语法
numbers = {x for x in range(10)} # {0, 1, 2, ..., 9}

# 带条件
evens = {x for x in range(10) if x % 2 == 0} # {0, 2, 4, 6, 8}

# 表达式
squares = {x ** 2 for x in range(5)} # {0, 1, 4, 9, 16}

# 从字符串去重
text = "hello world"
unique_chars = {char for char in text if char != ' '} # {'h', 'e', 'l', 'o', 'w', 'r', 'd'}

类型转换

Python 提供了内置函数进行类型转换。

基本类型转换

# 转换为整数
x = int(3.14) # 3
y = int("42") # 42
z = int("101", 2) # 5(二进制转换)

# 转换为浮点数
a = float(3) # 3.0
b = float("3.14") # 3.14

# 转换为字符串
s1 = str(123) # '123'
s2 = str(3.14) # '3.14'
s3 = str([1, 2, 3]) # '[1, 2, 3]'

# 转换为布尔值
print(bool(0)) # False
print(bool(1)) # True
print(bool("")) # False
print(bool("hello")) # True
print(bool([])) # False
print(bool([1, 2])) # True

序列类型转换

# 转换为列表
list1 = list("hello") # ['h', 'e', 'l', 'l', 'o']
list2 = list((1, 2, 3)) # [1, 2, 3]
list3 = list(range(5)) # [0, 1, 2, 3, 4]

# 转换为元组
tuple1 = tuple([1, 2, 3]) # (1, 2, 3)
tuple2 = tuple("hello") # ('h', 'e', 'l', 'l', 'o')

# 转换为集合
set1 = set([1, 2, 2, 3, 3, 3]) # {1, 2, 3}
set2 = set("hello") # {'h', 'e', 'l', 'o'}

# 转换为字典
items = [("name", "Alice"), ("age", 25)]
dict1 = dict(items) # {'name': 'Alice', 'age': 25}

字符串和数字之间的转换

# 字符串转数字
num1 = int("123") # 123
num2 = float("3.14") # 3.14
num3 = eval("123 + 456") # 579(慎用,有安全风险)

# 数字转字符串
str1 = str(123) # '123'
str2 = f"{123}" # '123'
str3 = format(3.14, ".2f") # '3.14'

# 进制转换
num = 255
print(bin(num)) # '0b11111111'(二进制)
print(oct(num)) # '0o377'(八进制)
print(hex(num)) # '0xff'(十六进制)

# 解析十六进制等
print(int("0xff", 16)) # 255
print(int("0o377", 8)) # 255

深拷贝 vs 浅拷贝

import copy

# 浅拷贝(复制引用)
original = [1, 2, [3, 4]]
shallow = original.copy()
shallow[2][0] = 99
print(original) # [1, 2, [99, 4]](受影响)

# 深拷贝(完全复制)
original = [1, 2, [3, 4]]
deep = copy.deepcopy(original)
deep[2][0] = 99
print(original) # [1, 2, [3, 4]](不受影响)

# 其他浅拷贝方式
list1 = [1, 2, 3]
copy1 = list1[:] # 切片
copy2 = list(list1) # 构造函数
copy3 = list1.copy() # copy方法

数据类型总结

类型可变性有序性允许重复使用场景
int不可变--整数运算
float不可变--浮点运算
str不可变文本处理
list可变动态数据集合
tuple不可变固定数据集合
dict可变是*键唯一键值对存储
set可变去重、集合运算
frozenset不可变不可变集合

*注:Python 3.7+ 字典保持插入顺序

最佳实践

  1. 选择合适的数据类型

    # 固定配置使用元组
    CONFIG = ("localhost", 8080, True)

    # 动态数据使用列表
    items = []
    items.append(new_item)

    # 去重使用集合
    unique_items = set(duplicate_items)
  2. 使用类型提示提高代码可读性

    from typing import List, Dict, Set, Tuple

    def process_data(
    numbers: List[int],
    config: Dict[str, str],
    unique: Set[int]
    ) -> Tuple[int, int]:
    return sum(numbers), len(unique)
  3. 避免在可变对象上使用默认参数

    # 错误
    def add_item(item, items=[]):
    items.append(item)
    return items

    # 正确
    def add_item(item, items=None):
    if items is None:
    items = []
    items.append(item)
    return items
  4. 使用推导式提高代码简洁性

    # 不推荐
    squares = []
    for i in range(10):
    squares.append(i ** 2)

    # 推荐
    squares = [i ** 2 for i in range(10)]
  5. 深拷贝和浅拷贝的选择

    # 简单对象使用浅拷贝
    copy_list = original_list[:]

    # 嵌套对象使用深拷贝
    import copy
    deep_copy = copy.deepcopy(nested_list)

小结

本章节介绍了 Python 的数据类型:

  • 数字类型: int, float, complex, bool
  • 字符串: 不可变字符序列, 丰富的操作方法
  • 列表: 可变有序序列, 灵活的数据集合
  • 元组: 不可变有序序列, 用于固定数据
  • 字典: 键值对集合, 快速查找
  • 集合: 无序不重复集合, 去重和集合运算
  • 类型转换: 各种类型之间的转换

掌握这些数据类型是 Python 编程的基础。下一章我们将学习控制流,包括条件语句和循环。