0
点赞
收藏
分享

微信扫一扫

[1]Python基础语法-【2】数据类型

李雨喵 2023-03-21 阅读 88

在 Python 中,有多种不同的数据类型可供使用。这些数据类型包括数字、字符串、列表、元组、字典和集合。在本教程中,我们将介绍这些数据类型的定义和基本操作。

数字

数字是 Python 中的基本数据类型之一。Python 支持整数、浮点数和复数。下面是一些数字的示例:

x = 5        # 整数
y = 3.14 # 浮点数
z = 2 + 3j # 复数

数字类型支持基本算术操作,例如加法、减法、乘法和除法。例如:

x = 5
y = 3

print(x + y) # 输出 8
print(x - y) # 输出 2
print(x * y) # 输出 15
print(x / y) # 输出 1.6666666666666667

除了基本算术操作外,数字类型还支持其他一些操作,例如取模、幂和整除。例如:

x = 5
y = 3

print(x % y) # 输出 2
print(x ** y) # 输出 125
print(x // y) # 输出 1

字符串

字符串是 Python 中的另一种基本数据类型。字符串是由单个字符组成的序列。在 Python 中,可以使用单引号或双引号来定义字符串。例如:

name = 'Alice'
message = "Hello, world!"

可以使用加号(+)将字符串连接起来。例如:

first_name = 'John'
last_name = 'Doe'

full_name = first_name + ' ' + last_name
print(full_name) # 输出 John Doe

还可以使用方括号和索引号来访问字符串中的单个字符。例如:

message = 'Hello, world!'
print(message[0]) # 输出 H
print(message[7]) # 输出 w

列表

列表是 Python 中的一种复合数据类型。列表是由多个值组成的序列,这些值可以是数字、字符串或其他类型。可以使用方括号来定义列表。例如:

numbers = [1, 2, 3, 4, 5]
fruits = ['apple', 'banana', 'orange']

可以使用索引号来访问列表中的单个元素。例如:

numbers = [1, 2, 3, 4, 5]
print(numbers[0]) # 输出 1

fruits = ['apple', 'banana', 'orange']
print(fruits[1]) # 输出 banana

列表还支持一些其他的操作,例如添加和删除元素。例如:

numbers = [1, 2, 3, 4, 5]
numbers.append(6) # 添加元素 6
print(numbers) # 输出 [1, 2, 3, 4, 5, 6]

fruits = ['apple', 'banana', 'orange']
fruits.remove('banana') # 删除元素 'banana'
print(fruits)

元组

元组是 Python 中的另一种复合数据类型。元组是由多个值组成的序列,但是与列表不同的是,元组是不可变的,即一旦创建,就不能修改。可以使用圆括号来定义元组。例如:

coordinates = (1, 2, 3)
colors = ('red', 'green', 'blue')

可以使用索引号来访问元组中的单个元素。例如:

coordinates = (1, 2, 3)
print(coordinates[0]) # 输出 1

colors = ('red', 'green', 'blue')
print(colors[1]) # 输出 green

由于元组是不可变的,因此无法添加、删除或修改元素。

字典

字典是 Python 中的一种复合数据类型。字典是由键-值对组成的集合,其中每个键都唯一地标识一个值。可以使用花括号来定义字典。例如:

person = {'name': 'Alice', 'age': 30, 'city': 'New York'}

可以使用方括号和键来访问字典中的值。例如:

person = {'name': 'Alice', 'age': 30, 'city': 'New York'}
print(person['name']) # 输出 Alice
print(person['age']) # 输出 30

可以使用等号来添加或修改字典中的键-值对。例如:

person = {'name': 'Alice', 'age': 30, 'city': 'New York'}
person['occupation'] = 'teacher' # 添加键-值对
person['age'] = 31 # 修改键-值对
print(person) # 输出 {'name': 'Alice', 'age': 31, 'city': 'New York', 'occupation': 'teacher'}

可以使用 del 关键字来删除字典中的键-值对。例如:

person = {'name': 'Alice', 'age': 30, 'city': 'New York'}
del person['city'] # 删除键-值对
print(person) # 输出 {'name': 'Alice', 'age': 30}

集合

集合是 Python 中的一种数据类型,它是由唯一的元素组成的无序集合。可以使用花括号或 set() 函数来定义集合。例如:

numbers = {1, 2, 3, 4, 5}
colors = set(['red', 'green', 'blue'])

集合支持一些基本操作,例如添加和删除元素。例如:

numbers = {1, 2, 3, 4, 5}
numbers.add(6) # 添加元素 6
print(numbers) # 输出 {1, 2, 3, 4, 5, 6}

colors = set(['red', 'green', 'blue'])
colors.remove('green') # 删除元素 'green'
print(colors) # 输出 {'red', 'blue'}

集合还支持一些其他的操作,例如求并集、交集和差集。例如:

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 & set2) # 输出 {4, 5}

# 求差集
print(set1 - set2) # 输出 {1, 2, 3}

总结

以上是 Python 中数字、字符串、列表、元组、字典、集合的基本定义和常见操作的介绍,这些数据类型在实际的程序开发中经常用到,熟练掌握它们的定义和操作对于编写高效的 Python 程序非常重要。

举报

相关推荐

0 条评论