study day02
程序员文章站
2022-03-23 14:58:57
...
python day02 python的特征数据类型
列表(list)
- 特点:
使用与Java元组的使用方法类似
列表具有异构性,可以存储不同数据类型的值
列表是动态的,长度可以改变 - 创建:
>>>list1=[2555,'bean','豆子',"上ke"]
print(list1)
>>>E:\Pycharmworkplace\untitled\venv\Scripts\python.exe "E:/Pycharmworkplace/untitled/studytest/study 02.py"
[2555, 'bean', '豆子', '上ke']
- 删除:
del(list):
list1=[2555,'bean','豆子',"上ke"]
del(list1)
print(list1)
>>>NameError: name 'list1' is not defined
- del(list)[i]:
list1=[2555,'bean','豆子',"上ke"]
del(list1)[1]
print(list1)
>>>E:\Pycharmworkplace\untitled\venv\Scripts\python.exe "E:/Pycharmworkplace/untitled/studytest/study 02.py"
[2555, '豆子', '上ke']
- len(list)
list1=[2555,'bean','豆子',"上ke"]
del(list1)[1]
print(list1)
print(len(list1))
>>>E:\Pycharmworkplace\untitled\venv\Scripts\python.exe "E:/Pycharmworkplace/untitled/studytest/study 02.py"
[2555, '豆子', '上ke']
3
- 常用操作:
list1=[2555,'bean','豆子',"上ke"]
list2=[22,4566,'bean']
list3=[27,66,58,59]
del(list1)[1]
print(list1)
print(len(list1))#求列表长度
print(2555 in (list1))#判断2555是否在list1中
print((list1[0:3:2]))#切片 将list1中元素从list1[0]开始到list[3]结束 步长为2
print(list1+list2)#连接两个列表
print(max(list3))#返回列表最大值 min(list)同
>>>
E:\Pycharmworkplace\untitled\venv\Scripts\python.exe "E:/Pycharmworkplace/untitled/studytest/study 02.py"
[2555, '豆子', '上ke']
3
True
[2555, '上ke']
[2555, '豆子', '上ke', 22, 4566, 'bean']
66
list.extend(list2)#在list末尾加上list2相当于list+list2
list.insert(i,x)#在list[i]出插入值x,其余元素后移
list.count(x)#返回x在list中出现的次数
list.sort()#将list中元素升序排序
list.revrese()#将list中元素顺序反转
(列表解析)
【例】输入学生成绩判定学生等级
list=[]
number=int(input("请输入学生数:"))
for i in range(0,number,1):
list.append(int(input("请输入成绩")))
for i in range(0,number,1):
if list[i]>=90:
print("等级A",list[i])
elif list[i]>=60:
print("等级B",list[i])
else:
print("等级C",list[i])
>>>
E:\Pycharmworkplace\untitled\venv\Scripts\python.exe "E:/Pycharmworkplace/untitled/studytest/study 02.py"
请输入学生数:4
请输入成绩48
请输入成绩88
请输入成绩94
请输入成绩98
等级C 48
等级B 88
等级A 94
等级A 98
元组(tuple)
- 特点:
元组可以有序的存储不同类型的数据 如字符串 数字 甚至元组
元组不可改变 创建后不能做任何修改
元组运算比列表快
写保护 数据安全 - 创建:
tuple=(1,"hello",55)
- 基本操作:
tuple=(1,"hello",55,50,88)
tuple1=(49,86,48,78,85)
print(tuple[0:3])#切片
print(tuple+tuple1)#连接
print(len(tuple))#求长度
print("hello" in tuple)#判断hello是否存在
print(max(tuple1))#求最大值
>>>E:\Pycharmworkplace\untitled\venv\Scripts\python.exe "E:/Pycharmworkplace/untitled/studytest/study 02.py"
(1, 'hello', 55)
(1, 'hello', 55, 50, 88, 49, 86, 48, 78, 85)
5
True
86
- 元组与列表的相互转换:
Python 3.5.1 (v3.5.1:37a07cee5969, Dec 6 2015, 01:54:25) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> list1=[22,55,33,4,57]
>>> tup=tuple(list1)
>>> print(tup)
(22, 55, 33, 4, 57)
##lis=list(tuple1)
字典(dictionary)
- 特点:
字典是包含索引的一个集合 是键(key)和值(value)的集合 - 创建:
(键必须是唯一不可变的数据类型 如 数字 字符串 元组)
a={key1 : value1,key2 :value2}
>>>dict1={"java":520,'python':521}
dict2={(1,2):["A","B"],(3,4):["C",'D'],(5,6):["E","F"]}
dict3=([('sape',515),('ddd',989),("soshj",886)])
print(dict1,"\n",dict2,"\n",dict3,)
>>>
E:\Pycharmworkplace\untitled\venv\Scripts\python.exe "E:/Pycharmworkplace/untitled/studytest/study 02.py"
{'python': 521, 'java': 520}
{(1, 2): ['A', 'B'], (5, 6): ['E', 'F'], (3, 4): ['C', 'D']}
[('sape', 515), ('ddd', 989), ('soshj', 886)]
还可以用关键字形式创建字典 但键只能是字符串型。
- 字典的基本操作:
dict1={"java":520,'python':521}
dict2={(1,2):["A","B"],(3,4):["C",'D'],(5,6):["E","F"]}
dict3=([('sape',515),('ddd',989),("soshj",886)])
print(dict1,"\n",dict2,"\n",dict3,)
print(dict1["java"])#访问字典中的值
print("java" in dict1)#判断dict1中是否存在键"java"
dict1["age"]=22#在字典中添加一个键值对
print(dict1)
dict1["java"]=561#更新键"name"的值
print(dict1)
del dict2[(1,2)]#删除键值对
print(dict2)
>>>
E:\Pycharmworkplace\untitled\venv\Scripts\python.exe "E:/Pycharmworkplace/untitled/studytest/study 02.py"
{'java': 520, 'python': 521}
{(1, 2): ['A', 'B'], (5, 6): ['E', 'F'], (3, 4): ['C', 'D']}
[('sape', 515), ('ddd', 989), ('soshj', 886)]
520
True
{'java': 520, 'python': 521, 'age': 22}
{'java': 561, 'python': 521, 'age': 22}
{(5, 6): ['E', 'F'], (3, 4): ['C', 'D']}
- 常用操作:
dict1={"java":520,'python':521}
dict2={(1,2):["A","B"],(3,4):["C",'D'],(5,6):["E","F"]}
dict3=([('sape',515),('ddd',989),("soshj",886)])
print(dict1.keys())#返回字典中所有key的“列表”
print(dict1.values())#返回字典中所有value的“列表”
print(dict1.items())#返回键与值的列表
print(dict1.get("java"))#返回字典中对键对应的值
print(dict1.get("bean"))#若key不存在 返回None
##遍历字典
for key in dict1.keys():
print("key=%s, value=%s" %(key,dict1[key]))
##字典更新:
dict1.update(dict2)#将两个字典组合
print(dict1)
>>>
E:\Pycharmworkplace\untitled\venv\Scripts\python.exe "E:/Pycharmworkplace/untitled/studytest/study 02.py"
dict_keys(['java', 'python'])
dict_values([520, 521])
dict_items([('java', 520), ('python', 521)])
520
None
key=java, value=520
key=python, value=521
{(1, 2): ['A', 'B'], (5, 6): ['E', 'F'], 'java': 520, (3, 4): ['C', 'D'], 'python': 521}
字典清空dict.clear()
集合(set)
- 特点:
集合是不可重复的无序元素集
元素是非变量 - 创建:
#直接创建:
set1={1,5,65,34,26}
set2={"python","java",'bean'}
set3={1,5,3,"python"}
print(set1,"\n",set2,"\n",set3)
#字符串创建
set4=set("python&java")#有两个a 自动去重
print(set4)
#列表或元组创建
set5=set([1,"java","都"])
set6=set((1,2,"python"))
print(set5,"\n",set6)
>>>
E:\Pycharmworkplace\untitled\venv\Scripts\python.exe "E:/Pycharmworkplace/untitled/studytest/study 02.py"
{65, 26, 34, 5, 1}
{'python', 'java', 'bean'}
{1, 3, 'python', 5}
{'n', 'v', 'p', 'o', 'y', 't', '&', 'j', 'h', 'a'}
{'都', 1, 'java'}
{1, 2, 'python'}
- 集合的操作:
set1={1,5,65,34,26}
set2={"python","java",'bean'}
set3={1,5,3,"python"}
set1.add("dd")#添加元素
print(set1)
set1.remove(65)#删除指定元素,若指定元素不存在就报错
print(set1)
set1.discard("dd")#删除指定元素 若不存在则不做任何事
print(set1)
set1.update(set2)#更新
print(set1)
print(set1.pop())#任意删除一个元素
print(set1)
print(set1&set2)#两个集合求交集
print(set2|set3)#求并集
print(set2-set3)#求差集
print(set2^set3)#求对称差
print("java" in set2)
print("java" in set3)
>>>
E:\Pycharmworkplace\untitled\venv\Scripts\python.exe "E:/Pycharmworkplace/untitled/studytest/study 02.py"
{65, 34, 1, 5, 'dd', 26}
{34, 1, 5, 'dd', 26}
{34, 1, 5, 26}
{34, 1, 'python', 5, 'bean', 'java', 26}
34
{1, 'python', 5, 'bean', 'java', 26}
{'python', 'bean', 'java'}
{1, 'python', 'java', 'bean', 3, 5}
{'java', 'bean'}
{3, 1, 'java', 'bean', 5}
True
False
=END||2018-10-16 23:52:40=====
上一篇: STL string 类总结
下一篇: JQuery学习笔记
推荐阅读
-
2020.07.20 Python study Day 1
-
php study
-
JavaEE基础day02 1.定义Java中的变量 四类八种 2.变量定义和使用的注意事项 3.数据类型的转换、强制数据类型转换4.算数运算符、比较运算符、逻辑运算符、赋值运算符、三元运算符
-
坚持代码实现数据结构DAY02——线性表及其实现
-
php study plan personal,studyplan
-
荐 14天数据分析与机器学习实践之Day02——数据分析处理库Pandas应用总结
-
Further Study of Rugular Expresions第1/2页
-
day02、Requests模块
-
分布式学习day02
-
IOS开发控件视图day02:动态创建按钮、按钮状态、按钮动画