python3的六种数据类型
时间: 2023-12-29 20:24:56 浏览: 47
Python3中有六种标准数据类型,分别是:
1. Number(数字):包括整型、浮点型、复数。
2. String(字符串):由一系列字符组成,可以使用单引号、双引号或三引号来表示。
3. List(列表):由一系列有序的元素组成,可以包含不同类型的元素。
4. Tuple(元组):与列表类似,但是元素不能修改。
5. Set(集合):由一系列无序的、唯一的元素组成。
6. Dictionary(字典):由键值对组成,键和值可以是不同类型的数据。
以下是一个例子,展示了如何定义和使用这六种数据类型:
```python
# Number
num1 = 10 # 整型
num2 = 3.14 # 浮点型
num3 = 2 + 3j # 复数
# String
str1 = 'Hello, world!'
str2 = "I'm a student."
str3 = '''This is a multi-line string.
You can use triple quotes to define it.'''
# List
list1 = [1, 'apple', 3.14, True]
list2 = [4, 5, 6]
list3 = list1 + list2 # 连接两个列表
list1.append('banana') # 在列表末尾添加元素
list2[1] = 'orange' # 修改列表中的元素
# Tuple
tuple1 = (1, 'apple', 3.14, True)
tuple2 = (4, 5, 6)
# Set
set1 = {1, 2, 3, 4, 5}
set2 = {4, 5, 6, 7, 8}
set3 = set1.union(set2) # 合并两个集合
set1.add(6) # 向集合中添加元素
# Dictionary
dict1 = {'name': 'Alice', 'age': 20, 'gender': 'female'}
dict2 = {'name': 'Bob', 'age': 25, 'gender': 'male'}
dict1['age'] = 21 # 修改字典中的值
```