python中的数据类型
python中的数据类型
Python 数据类型
| 数据类型 | 关键字 | 标志性符号 | 是否可变 | 是否为序列 | 强制类型转换的函数 |
|---|---|---|---|---|---|
| int | int | 无 | 不可变 | 否 | int() |
| float | float | 无 | 不可变 | 否 | float() |
| complex | complex | 无 | 不可变 | 否 | complex() |
| bool | bool | True/False | 不可变 | 否 | bool() |
| str | str | ’ ’ 或 " " | 不可变 | 是 | str() |
| list | list | [ ] | 可变 | 是 | list() |
| tuple | tuple | ( ) | 不可变 | 是 | tuple() |
| set | set | { } | 可变 | 否 | set() |
| frozenset | frozenset | { } | 不可变 | 否 | frozenset() |
| dict | dict | { } | 可变 | 否 | dict() |
以上是python中常见的数据类型,包括整数(int)、浮点数(float)、复数(complex)、布尔值(bool)、字符串(str)、列表(list)、元组(tuple)、集合(set)、冻结集合(frozenset)和字典(dict)。
每个数据类型都有对应的关键字和标志性符号。可变性指的是是否可以更改数据类型的值。序列指的是是否可以通过索引访问其中的元素。强制类型转换的函数用于将其他数据类型转换为指定的数据类型。
python中的序列
Python中的序列是一种复合数据类型,也是一个数据的容器,有多个数有序构成。具体包括:字符串,列表,元组。
python中的字符串
下面是字符串的一些基本操作:
字符串(str):
-
创建字符串:可以使用单引号或双引号创建字符串。
string1 = 'Hello, World!' string2 = "Python is awesome!" -
排序字符串:字符串是不可变的,因此不能直接对其进行排序。但可以使用内置函数
sorted()对字符串进行排序。string = 'python' sorted_string = ''.join(sorted(string)) print(sorted_string) # 输出:hnopty -
增加字符串:字符串是不可变的,因此不能直接在原字符串上进行添加操作。但可以通过字符串拼接或格式化来创建新的字符串。
string1 = 'Hello, ' string2 = 'World!' new_string = string1 + string2 print(new_string) # 输出:Hello, World! -
删除字符串:字符串是不可变的,因此不能直接在原字符串上进行删除操作。但可以使用切片操作或字符串方法来删除部分内容。
string = 'Hello, World!' new_string = string[:5] + string[7:] print(new_string) # 输出:Hello World! -
修改字符串:字符串是不可变的,因此不能直接修改字符串的某个字符。但可以通过切片操作和字符串方法来修改部分内容。
string = 'Hello, World!' new_string = string[:5] + 'Python!' print(new_string) # 输出:Hello Python! -
查找字符串:可以使用字符串方法
find()、index()和count()来查找字符串中的特定子串。string = 'Hello, World!' print(string.find('World')) # 输出:7 print(string.index('World')) # 输出:7 print(string.count('l')) # 输出:3 -
索引和切片字符串:可以使用索引和切片操作来访问字符串中的字符或子串。
string = 'Hello, World!' print(string[0]) # 输出:H print(string[7:]) # 输出:World! -
连接字符串:可以使用字符串拼接操作符
+或字符串方法join()来连接多个字符串。string1 = 'Hello, ' string2 = 'World!' new_string = string1 + string2 print(new_string) # 输出:Hello, World! -
重复字符串:可以使用字符串乘法操作符
*来重复字符串。string = 'Hello!' repeated_string = string * 3 print(repeated_string) # 输出:Hello!Hello!Hello! -
判断成员:可以使用关键字
in来判断一个字符串是否是另一个字符串的子串。string = 'Hello, World!' print('World' in string) # 输出:True -
长度:可以使用内置函数
len()来获取字符串的长度。string = 'Hello, World!' print(len(string)) # 输出:13
python中的列表
下边是一些列表的基本操作:
列表(list):
-
创建列表:可以使用方括号
[]来创建列表,并在其中添加元素。list1 = [1, 2, 3, 4, 5] list2 = ['apple', 'banana', 'cherry'] -
排序列表:可以使用列表的
sort()方法对列表进行排序。list = [5, 2, 4, 1, 3] list.sort() print(list) # 输出:[1, 2, 3, 4, 5] -
增加元素:可以使用列表的
append()方法在列表末尾添加元素,或使用索引和切片操作在指定位置插入元素。list = [1, 2, 3] list.append(4) print(list) # 输出:[1, 2, 3, 4] list.insert(0, 0) print(list) # 输出:[0, 1, 2, 3, 4] -
删除元素:可以使用列表的
remove()方法删除指定的元素,或使用索引和切片操作删除指定位置的元素。list = [1, 2, 3, 4, 5] list.remove(3) print(list) # 输出:[1, 2, 4, 5] del list[0] print(list) # 输出:[2, 4, 5] -
修改元素:可以使用索引操作来修改列表中的元素。
list = [1, 2, 3, 4, 5] list[2] = 10 print(list) # 输出:[1, 2, 10, 4, 5] -
查找元素:可以使用列表的
index()方法来查找指定元素的索引。list = [1, 2, 3, 4, 5] print(list.index(3)) # 输出:2 -
索引和切片列表:可以使用索引和切片操作来访问列表中的元素或子列表。
list = [1, 2, 3, 4, 5] print(list[0]) # 输出:1 print(list[2:]) # 输出:[3, 4, 5] -
连接列表:可以使用列表拼接操作符
+来连接多个列表。list1 = [1, 2, 3] list2 = [4, 5, 6] new_list = list1 + list2 print(new_list) # 输出:[1, 2, 3, 4, 5, 6] -
重复列表:可以使用列表乘法操作符
*来重复列表。list = [1, 2, 3] repeated_list = list * 3 print(repeated_list) # 输出:[1, 2, 3, 1, 2, 3, 1, 2, 3] -
判断成员:可以使用关键字
in来判断一个元素是否存在于列表中。list = [1, 2, 3, 4, 5] print(3 in list) # 输出:True -
长度:可以使用内置函数
len()来获取列表的长度。list = [1, 2, 3, 4, 5] print(len(list)) # 输出:5
python中的元组
下边是元组的一些基本操作:
元组(tuple):
-
创建元组:可以使用圆括号
()或直接用逗号分隔的值来创建元组。tuple1 = (1, 2, 3, 4, 5) tuple2 = 'apple', 'banana', 'cherry' -
排序元组:元组是不可变的,因此不能直接对其进行排序。但可以使用内置函数
sorted()对元组进行排序。tuple = (5, 2, 4, 1, 3) sorted_tuple = tuple(sorted(tuple)) print(sorted_tuple) # 输出:(1, 2, 3, 4, 5) -
增加元素:元组是不可变的,因此不能直接在原元组上进行添加操作。但可以通过元组拼接或格式化来创建新的元组。
tuple1 = (1, 2, 3) tuple2 = (4,) new_tuple = tuple1 + tuple2 print(new_tuple) # 输出:(1, 2, 3, 4) -
删除元素:元组是不可变的,因此不能直接在原元组上进行删除操作。但可以使用切片操作或元组解包来删除部分内容。
tuple = (1, 2, 3, 4, 5) new_tuple = tuple[:2] + tuple[3:] print(new_tuple) # 输出:(1, 2, 4, 5) -
修改元素:元组是不可变的,因此不能直接修改元组的某个元素。但可以通过切片操作和元组拼接来修改部分内容。
tuple = (1, 2, 3, 4, 5) new_tuple = tuple[:2] + (10,) + tuple[3:] print(new_tuple) # 输出:(1, 2, 10, 4, 5) -
查找元素:可以使用元组的
index()方法来查找指定元素的索引。tuple = (1, 2, 3, 4, 5) print(tuple.index(3)) # 输出:2 -
索引和切片元组:可以使用索引和切片操作来访问元组中的元素或子元组。
tuple = (1, 2, 3, 4, 5) print(tuple[0]) # 输出:1 print(tuple[2:]) # 输出:(3, 4, 5) -
连接元组:可以使用元组拼接操作符
+来连接多个元组。tuple1 = (1, 2, 3) tuple2 = (4, 5, 6) new_tuple = tuple1 + tuple2 print(new_tuple) # 输出:(1, 2, 3, 4, 5, 6) -
重复元组:可以使用元组乘法操作符
*来重复元组。tuple = (1, 2, 3) repeated_tuple = tuple * 3 print(repeated_tuple) # 输出:(1, 2, 3, 1, 2, 3, 1, 2, 3) -
判断成员:可以使用关键字
in来判断一个元素是否存在于元组中。tuple = (1, 2, 3, 4, 5) print(3 in tuple) # 输出:True -
长度:可以使用内置函数
len()来获取元组的长度。tuple = (1, 2, 3, 4, 5) print(len(tuple)) # 输出:5
python中的字典
在Python中,可以使用花括号({})来创建一个字典。字典由键(key)和对应的值(value)组成,键和值之间使用冒号(:)分隔,键值对之间使用逗号(,)分隔。
# 创建一个空字典
my_dict = {}
# 创建一个包含键值对的字典
my_dict = {'name': 'John', 'age': 25, 'city': 'New York'}
字典是无序的,不能直接排序。但是可以使用内置的sorted()函数对字典进行排序,返回一个根据键排序的列表。
my_dict = {'name': 'John', 'age': 25, 'city': 'New York'}
# 对字典的键进行排序
sorted_keys = sorted(my_dict.keys())
print(sorted_keys) # 输出:['age', 'city', 'name']
可以使用赋值运算符(=)来增加或修改字典中的键值对。
my_dict = {'name': 'John', 'age': 25}
# 增加一个新的键值对
my_dict['city'] = 'New York'
print(my_dict) # 输出:{'name': 'John', 'age': 25, 'city': 'New York'}
# 修改一个已有的键的值
my_dict['age'] = 26
print(my_dict) # 输出:{'name': 'John', 'age': 26, 'city': 'New York'}
可以使用del关键字来删除字典中的键值对,也可以使用pop()方法删除指定键的值。
my_dict = {'name': 'John', 'age': 25, 'city': 'New York'}
# 删除指定的键值对
del my_dict['age']
print(my_dict) # 输出:{'name': 'John', 'city': 'New York'}
# 删除指定键的值,并返回该值
age = my_dict.pop('age')
print(age) # 输出:25
可以通过赋值运算符(=)来修改字典中的键值对。
my_dict = {'name': 'John', 'age': 25, 'city': 'New York'}
# 修改指定键的值
my_dict['age'] = 26
print(my_dict) # 输出:{'name': 'John', 'age': 26, 'city': 'New York'}
可以使用键来查找字典中对应的值,也可以使用get()方法来查找键对应的值。如果键不存在,使用get()方法会返回None或指定的默认值。
my_dict = {'name': 'John', 'age': 25, 'city': 'New York'}
# 使用键来查找值
name = my_dict['name']
print(name) # 输出:John
# 使用get()方法查找值
age = my_dict.get('age')
print(age) # 输出:25
# 使用get()方法查找不存在的键
country = my_dict.get('country')
print(country) # 输出:None
# 使用get()方法查找不存在的键,并指定默认值
country = my_dict.get('country', 'Unknown')
print(country) # 输出:Unknown
字典是无序的,不能像序列类型那样使用索引和切片。只能通过键来访问字典中的值。
my_dict = {'name': 'John', 'age': 25, 'city': 'New York'}
# 使用键来访问值
name = my_dict['name']
print(name) # 输出:John
可以使用update()方法将一个字典的键值对添加到另一个字典中。
dict1 = {'name': 'John'}
dict2 = {'age': 25}
# 将dict2的键值对添加到dict1中
dict1.update(dict2)
print(dict1) # 输出:{'name': 'John', 'age': 25}
字典不支持重复操作,每个键在字典中是唯一的。
my_dict = {'name': 'John', 'age': 25}
# 重复添加键值对
my_dict['name'] = 'Mike'
print(my_dict) # 输出:{'name': 'Mike', 'age': 25}
可以使用in关键字来检查一个键是否存在于字典中。
my_dict = {'name': 'John', 'age': 25}
# 检查键是否存在
if 'name' in my_dict:
print('Name exists in the dictionary')
else:
print('Name does not exist in the dictionary')
可以使用len()函数来获取字典中键值对的数量。
my_dict = {'name': 'John', 'age': 25, 'city': 'New York'}
# 获取字典的长度
length = len(my_dict)
print(length) # 输出:3
python中的集合
集合,通常是指Python中的set数据类型。集合是一组无序且唯一的元素的集合。以下是集合的各种操作。
-
创建集合:
可以使用大括号{}或者set()函数来创建一个集合。例如:my_set = {1, 2, 3, 4, 5} print(my_set) # 输出: {1, 2, 3, 4, 5} -
排序集合:
集合是无序的,不能直接排序。如果需要排序,可以先将集合转换为列表,然后对列表进行排序。例如:my_set = {5, 3, 1, 4, 2} sorted_list = sorted(list(my_set)) print(sorted_list) # 输出: [1, 2, 3, 4, 5] -
增加元素:
可以使用add()方法向集合中添加元素,每次只能添加一个元素。例如:my_set = {1, 2, 3} my_set.add(4) print(my_set) # 输出: {1, 2, 3, 4} -
删除元素:
可以使用remove()或者discard()方法从集合中删除元素。两者的区别在于,remove()方法在元素不存在时会引发 KeyError 异常,而discard()方法不会。例如:my_set = {1, 2, 3, 4} my_set.remove(3) print(my_set) # 输出: {1, 2, 4} -
修改元素:
集合中的元素是不可变的,因此不能直接修改元素。如果需要修改元素,可以先删除原有元素,然后添加新元素。 -
查找元素:
可以使用in关键字来检查集合中是否存在某个元素。例如:my_set = {1, 2, 3} print(2 in my_set) # 输出: True -
索引和切片:
集合是无序的,因此不能通过索引或切片来访问集合中的元素。如果需要按照顺序访问元素,可以先将集合转换为列表。 -
连接集合:
可以使用union()方法或者|运算符来连接两个集合。例如:set1 = {1, 2, 3} set2 = {4, 5, 6} set3 = set1.union(set2) print(set3) # 输出: {1, 2, 3, 4, 5, 6} -
重复集合:
可以使用*运算符来重复集合中的元素。例如:my_set = {1, 2, 3} repeated_set = my_set * 3 print(repeated_set) # 输出: {1, 2, 3, 1, 2, 3, 1, 2, 3} -
成员运算:
可以使用in和not in运算符来检查元素是否属于集合。例如:my_set = {1, 2, 3} print(2 in my_set) # 输出: True print(4 not in my_set) # 输出: True -
长度:
可以使用len()函数获取集合中元素的数量。例如:my_set = {1, 2, 3} print(len(my_set)) # 输出: 3 -
加法和减法:
可以使用|运算符进行集合的并操作,使用-运算符进行集合的差操作。例如:set1 = {1, 2, 3} set2 = {2, 3, 4} union_set = set1 | set2 diff_set = set1 - set2 print(union_set) # 输出: {1, 2, 3, 4} print(diff_set) # 输出: {1}
python中的DataFrame
当涉及到数据处理和分析时,Python中最常用的库之一是pandas。pandas库提供了一个名为DataFrame的数据结构,它类似于电子表格或数据库中的表格,可以轻松地处理和分析结构化数据。
下面是DataFrame的一些基本操作:
- 创建DataFrame
可以使用多种方式创建DataFrame,最常见的是使用字典或二维数组。
import pandas as pd
# 使用字典创建DataFrame
data = {'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35],
'City': ['New York', 'London', 'Paris']}
df = pd.DataFrame(data)
# 使用二维数组创建DataFrame
data = [['Alice', 25, 'New York'],
['Bob', 30, 'London'],
['Charlie', 35, 'Paris']]
df = pd.DataFrame(data, columns=['Name', 'Age', 'City'])
- 排序DataFrame
可以使用sort_values()方法对DataFrame进行排序。
# 按照Age列进行升序排序
df_sorted = df.sort_values('Age')
- 增加、删除和修改DataFrame
可以使用以下方法对DataFrame进行增加、删除和修改操作。
# 增加一列
df['Salary'] = [5000, 6000, 7000]
# 删除一列
df = df.drop('Salary', axis=1)
# 修改某个单元格的值
df.at[0, 'Age'] = 26
- 查找DataFrame
可以使用以下方法查找DataFrame中的数据。
# 根据条件查找
df_filtered = df[df['Age'] > 30]
# 根据索引查找
row = df.loc[0]
- 索引和切片DataFrame
可以使用以下方法对DataFrame进行索引和切片操作。
# 根据列名索引
ages = df['Age']
# 根据行号索引
row = df.iloc[0]
# 切片操作
subset = df[1:3]
- 连接DataFrame
可以使用concat()方法将多个DataFrame连接在一起。
df1 = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
df2 = pd.DataFrame({'A': [7, 8, 9], 'B': [10, 11, 12]})
df_concatenated = pd.concat([df1, df2])
- 重复DataFrame
可以使用append()方法将一个DataFrame添加到另一个DataFrame的末尾。
df1 = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
df2 = pd.DataFrame({'A': [7, 8, 9], 'B': [10, 11, 12]})
df_appended = df1.append(df2)
- DataFrame成员和长度
可以使用以下方法检查DataFrame的成员和长度。
# 检查某个值是否在DataFrame中
is_member = 'Alice' in df['Name']
# 获取DataFrame的长度
length = len(df)
本文围绕Python展开,介绍了常见的数据类型,如整数、字符串、列表等,阐述了各数据类型的关键字、标志性符号、可变性等特性。还详细说明了字符串、列表、元组等序列类型,以及字典、集合、DataFrame的基本操作,包括创建、排序、增删改查等。
2205

被折叠的 条评论
为什么被折叠?



