Python基本变量类型
时间: 2024-04-22 08:20:33 浏览: 22
Python支持以下几种基本变量类型[^1]:
1. 整数(int):表示整数值,例如:1, 2, 3。
2. 浮点数(float):表示带有小数点的数值,例如:1.5, 2.7, 3.0。
3. 字符串(str):表示文本数据,用引号括起来,例如:"hello", 'world'。
4. 布尔值(bool):表示真(True)或假(False)的值。
5. 列表(list):表示有序的元素集合,用方括号括起来,例如:[1, 2, 3]。
6. 元组(tuple):表示有序的不可变元素集合,用圆括号括起来,例如:(1, 2, 3)。
7. 集合(set):表示无序的唯一元素集合,用大括号括起来,例如:{1, 2, 3}。
8. 字典(dict):表示键值对的集合,用大括号括起来,例如:{"name": "John", "age": 25}。
以下是一个演示Python基本变量类型的例子:
```python
# 整数
x = 10
print(type(x)) # 输出:<class 'int'>
# 浮点数
y = 3.14
print(type(y)) # 输出:<class 'float'>
# 字符串
name = "John"
print(type(name)) # 输出:<class 'str'>
# 布尔值
is_true = True
print(type(is_true)) # 输出:<class 'bool'>
# 列表
numbers = [1, 2, 3]
print(type(numbers)) # 输出:<class 'list'>
# 元组
point = (10, 20)
print(type(point)) # 输出:<class 'tuple'>
# 集合
fruits = {"apple", "banana", "orange"}
print(type(fruits)) # 输出:<class 'set'>
# 字典
person = {"name": "John", "age": 25}
print(type(person)) # 输出:<class 'dict'>
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)