目录
一、 数据类型介绍
二、 简单结构
三、 多维结构
·【列表】、元组、集合、字典
·列表、【元组】、集合、字典
·列表、元组、【集合】、字典
·列表、元组、集合、【字典】
一、数据类型介绍
Python中的数据类型主要包括:
1.整数(int):表示整数,例如:x = 2。
2.浮点数(float):表示带有小数点的数值,例如:y = 1.23。
3.布尔值(bool):表示真(True)或假(False)的值,用于逻辑运算,例如:is_true = True。
4.字符串(str):表示文本数据,可以使用单引号或双引号表示,例如:text = "Welcome, Python!"。
5.列表(list):有序的可变容器,可以包含不同数据类型的元素,例如:colorlist = [99, 88, 'red']。
6.元组(tuple):有序的不可变容器,例如:my_tuple = (3, 2, 'orange')。
7.集合(set):无序的可变容器,不允许重复元素,例如:my_set = {1, 2, 3}。
8.字典(dict):无序的键值对集合,例如:my_dict = {'name': 'Jay', 'age': 35}。
9.复数(complex):包含实部和虚部的数值,例如:z = 4 + 4j。
10.字节串(bytes):以字节为单位的不可变序列,例如:b = b'python'。
11.字节数组(bytearray):以字节为单位的可变序列,例如:ba = bytearray(b'python')。
简单结构:整数、浮点数、布尔值、字符串
多维结构:列表、元组、集合、字典
其他类型特定情况下才使用到,暂不介绍。
二、简单结构
使用一个简单例子介绍,有一个学生姓名为Judy,年龄是22,成绩是89.5,判断是否大于90分可以根据此构建四个变量模型age/score/boolScore/name(格式:整数/浮点数/布尔值/字符串),并进行格式化输出,同步输出变量模型对应格式,如代码所示。
# 整数
age = 22
# 浮点数
score = 89.5
# 布尔值 分数是否大于等于90
boolScore = True if score >= 90 else False
# 字符串
name = "Judy"
print(f"age:{age},score:{score},boolScore:{boolScore},name:{name}")
print(f"age:{type(age)},score:{type(score)},boolScore:{type(boolScore)},name:{type(name)}")
>>>runfile('C:/xxx/learnvar.py',wdir='C:/xxx/learn')
age:22,score:89.5,boolScore:False,name:Judy
age:<class 'int'>,score:<class 'float'>,boolScore:<class 'bool'>,name:<class 'str'>
三、多维结构
·【列表】、元组、集合、字典
# --------------------------------------------
# 列表(list):有序的可变容器,可以包含不同数据类型的元素
# type(colorslist) <class 'list'>
# --------------------------------------------
列表基本操作,索引访问列表
>numbers = [1,2,3,4]
>print(numbers[1],numbers[0:2],numbers[0:],"Bob" in numbers)
2 [1, 2] [1, 2, 3, 4] False
列表迭代,比如输出偶数
for number in numbers:
if number % 2 == 0:
print(number) # 返回 2 4
列表修改
>colors = ['red','yellow','green','blue']
>print(colors)
>colors[0] = "burgundy" # 索引修改
>colors[1:3] = ["orange","magenta"] # 切片赋值
>print(colors)
>colors[1:3] = ["orange","magenta","aqua"]
>print(colors)
['red', 'yellow', 'green', 'blue']
['burgundy', 'orange', 'magenta', 'blue']
['burgundy', 'orange', 'magenta', 'aqua', 'blue']
列表增加list.insert
>colors = ['red','yellow','green','blue']
>print(colors)
>colors.insert(1,"orange") # 在索引1位置插入
>print(colors)
>colors.insert(10,"violet") # 超过索引位置插入,直接插到末尾
>print(colors)
>colors.insert(-1,"indigo") # 插入倒数第一个
>print(colors)
['red', 'yellow', 'green', 'blue']
['red', 'orange', 'yellow', 'green', 'blue']
['red', 'orange', 'yellow', 'green', 'blue', 'violet']
['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet']
列表删除list.pop,append末尾增加
>colors.pop(3)
>print(colors)
>colors.pop(-1) # 负值索引
>print(colors)
>colors.pop() # 删除最后一个
>print(colors)
>colors.append("indigo")
>print(colors)
['red', 'orange', 'yellow', 'blue', 'indigo', 'violet']
['red', 'orange', 'yellow', 'blue', 'indigo']
['red', 'orange', 'yellow', 'blue']
['red', 'orange', 'yellow', 'blue', 'indigo']
列表统计
>nums = [1,2,3,4,5]
>print(sum(nums),max(nums),min(nums))
列表扩展列表推导式
>numbers = (1,2,3,4,5)
>squares = [num**2 for num in numbers]
>print(numbers,squares)
15 5 1
(1, 2, 3, 4, 5) [1, 4, 9, 16, 25]
·列表、【元组】、集合、字典
# --------------------------------------------
# 元组(tuple):有序的不可变容器
# type(first_tuple) <class 'tuple'>
# --------------------------------------------
元组定义和输出,类型,长度,切片访问
>first_tuple = (1,2,3)
>print(first_tuple,type(first_tuple),len(first_tuple),first_tuple[0],first_tuple[0:3])
(1, 2, 3) <class 'tuple'> 3 1 (1, 2, 3)
元组遍历,迭代
> for item in first_tuple:
> print(item*2) # 返回 2 4 6
元组 打包解包 = 两端元素数量相等,批量赋值
coordinates = 5.21,8.29
x,y = coordinates
a,b,c,d = 1,2,3,4
元组使用in检查是否包含某值
coo = tuple('coordinates')
print("o" in coo) # True
print("a" in coo) # True
print("z" in coo) # False
·列表、元组、【集合】、字典
# --------------------------------------------
# 集合(set):无序的可变容器,不允许重复元素
# type(s) <class 'set'>
# --------------------------------------------
集合创建一个集合
>s = {1, 2, 3, 4, 5}
>print(s)
{1, 2, 3, 4, 5}
集合添加元素
>s.add(6)
>print(s)
{1, 2, 3, 4, 5, 6}
集合移除元素
>s.remove(3)
>print(s)
{1, 2, 4, 5, 6}
集合的并集操作
>s1 = {3, 4, 5}
>unions = s | s1
>print(s,s1,unions)
{1, 2, 4, 5, 6} {3, 4, 5} {1, 2, 3, 4, 5, 6}
集合的交集操作
>intersections = s & s1
>print(s,s1,intersections)
{1, 2, 4, 5, 6} {3, 4, 5} {4, 5}
集合的差操作
>differences = s - s1
>print(s,s1,differences)
{1, 2, 4, 5, 6} {3, 4, 5} {1, 2, 6}
集合的对称差操作(异或运算)
>symmetric_differences = s ^ s1
>print(s,s1,symmetric_differences)
{1, 2, 4, 5, 6} {3, 4, 5} {1, 2, 3, 6}
集合的成员检查
>print(2 in s) # 返回 True
集合的长度
>print(len(s),len(s1)) # 返回5,3
集合的遍历
for item in s1:
print(item) # 返回 3 4 5
·列表、元组、集合、【字典】
# --------------------------------------------
# 字典(dict):无序的键值对集合,列表不能作为字典的键
# type(capitals) <class 'dict'>
# --------------------------------------------
字典 创建字典
>capitals = {
> "California":"Sacramento",
> "New York":"Albany",
> "Texas":"Austin",
>}
>print(capitals)
{'California': 'Sacramento', 'New York': 'Albany', 'Texas': 'Austin'}
字典增加
>capitals["Colorado"] = "Denver"
>print(capitals)
{'California': 'Sacramento', 'New York': 'Albany', 'Texas': 'Austin', 'Colorado': 'Denver'}
字典修改
>capitals["Texas"] = "Houston"
>print(capitals)
{'California': 'Sacramento', 'New York': 'Albany', 'Texas': 'Houston', 'Colorado': 'Denver'}
字典删除
>del capitals["Texas"]
>print(capitals)
{'California': 'Sacramento', 'New York': 'Albany', 'Colorado': 'Denver'}
字典键判断
>print("Arizona" in capitals,"California" in capitals)
False True
字典通过key,value,items()访问读取
>for key in capitals:
> print(key)
California
New York
Colorado
>for state in capitals:
> print(f"The capital of {state} is {capitals[state]}")
The capital of California is Sacramento
The capital of New York is Albany
The capital of Colorado is Denver
>for state,capital in capitals.items():
> print(f"The capital of {state} is {capital}")
The capital of California is Sacramento
The capital of New York is Albany
The capital of Colorado is Denver
下篇预告:python基础篇-函数