Python 数据类型


Python 数据类型

数据类型整理

类型类型名称说明示例对应Java类型
int整数整数42、-7byte, short, int, long
float浮点数小数3.14、-0.001float, double
bool布尔布尔True 或 Falseboolean
str字符串字符串“hello”、“Python 3.9”char, String
list列表可变序列[1, 2, 3]、[“a”, “b”, “c”]Array
tuple元组不可变序列(1, 2, 3)、(“x”, “y”)Array
dict字典键值对集合{“name”: “Alice”, “age”: 30}Map
set集合不重复元素集合{1, 2, 3}、{“apple”, “banana”}Set
NoneType空类型没有值或尚未初始化Nonenull

- int:整数

# 创建整数
a = 42
b = -7

# 常用方法
print(abs(a))        # 输出: 42(绝对值)
print(pow(a, 2))     # 输出: 1764(a的平方)
print(max(a, b))     # 输出: 42(最大值)
print(min(a, b))     # 输出: -7(最小值)

- float:浮点数

# 创建浮点数
x = 3.14
y = -0.001
z = 2.0

# 常用方法
import math
print(round(x, 1))      # 输出: 3.1(四舍五入)
print(math.sqrt(16))    # 输出: 4.0(平方根)
print(abs(y))           # 输出: 0.001(绝对值)
print(max(x, y, z))     # 输出: 3.14(最大值)

- bool:布尔

# 创建布尔值
is_valid = True
is_active = False

# 常用方法
print(bool(1))      # 输出: True(1转换为True)
print(bool(0))      # 输出: False(0转换为False)
print(not is_valid) # 输出: False(取反)

- str:字符串

# 创建字符串
greeting = "hello"
name = "Python"

# 常用方法
print(len(greeting))        # 输出: 5(字符串长度)
print(greeting.upper())     # 输出: HELLO(转大写)
print(greeting.lower())     # 输出: hello(转小写)
print(greeting + " " + name) # 输出: hello Python(字符串拼接)
print(name.replace("P", "J"))  # 输出: Jython(替换字符)

- list:列表

# 创建列表
numbers = [1, 2, 3, 4, 5]
fruits = ["apple", "banana", "cherry"]

# 常用方法
numbers.append(6)          # 在列表末尾添加元素
print(numbers)             # 输出: [1, 2, 3, 4, 5, 6]

fruits.remove("banana")    # 删除指定元素
print(fruits)              # 输出: ['apple', 'cherry']

numbers.sort()             # 排序列表
print(numbers)             # 输出: [1, 2, 3, 4, 5, 6]

print(fruits[1])           # 输出: cherry(访问指定索引)

- tuple:元组

# 创建元组
coordinates = (1, 2, 3)
person = ("Alice", 30, "Engineer")

# 常用方法
print(coordinates[0])        # 输出: 1(访问指定索引)

print(person.count("Alice")) # 输出: 1(计数)
print(person.index(30))     # 输出: 1(查找元素的索引)

- dict:字典

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

# 常用方法
print(person["name"])        # 输出: Alice(访问指定键的值)

person["age"] = 31          # 更新值
print(person)                # 输出: {'name': 'Alice', 'age': 31, 'city': 'New York'}

person["job"] = "Engineer"   # 添加新的键值对
print(person)                # 输出: {'name': 'Alice', 'age': 31, 'city': 'New York', 'job': 'Engineer'}

print(person.keys())         # 输出: dict_keys(['name', 'age', 'city', 'job'])(返回字典的所有键)
print(person.values())       # 输出: dict_values(['Alice', 31, 'New York', 'Engineer'])(返回字典的所有值)

print(person.get("age"))     # 输出: 31(获取键的值,若不存在则返回None)

- set:集合

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

# 常用方法
fruits.add("orange")         # 添加元素
print(fruits)                # 输出: {'apple', 'banana', 'cherry', 'orange'}

fruits.remove("banana")      # 删除指定元素
print(fruits)                # 输出: {'apple', 'cherry', 'orange'}

numbers.discard(3)           # 删除元素,若不存在不会抛出错误
print(numbers)               # 输出: {1, 2, 4, 5}

union_set = fruits.union({"pear", "grape"})  # 集合并集
print(union_set)             # 输出: {'apple', 'banana', 'cherry', 'orange', 'pear', 'grape'}

- NoneType:空类型

a = None
print(type(a))				 # 输出:<class 'NoneType'>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值