python序列基本操作彙總

創建序列語法:

變量名 ={}

dict={}

變量名 =[]

list01=[]

變量名 =() 不可變

tuple01=()

變量名 =值

str='123'

備註:序列中除了元祖之外都可以有增刪改查的功能

#增加 字典
stu_info={'num':'001','name':'袁天罡','sex':'男'}
stu_info['age']='78'
print(stu_info)

#列表
list01=['002','李淳風','男']
list01.append(64)
print(list01)

#字符串
str01='003\t武則天\t女'
str01='003\t武則天\t女\t59'
print(str01)

#刪除
#字典
stu_info={'num':'001','name':'袁天罡','sex':'男'}
del stu_info['sex']
print(stu_info)

#列表
list01=['002','李淳風','男']
del list01[2]
print(list01)

#字符串

str01='003\t武則天\t女'

str01='003\t武則天'
print(str01)

#改

#字典
stu_info={'num':'001','name':'袁天罡','sex':'男'}
stu_info.update({'sex':'女'})
print(stu_info)

#列表
list01=['002','李淳風','男']
list01[2]='不詳'
print(list01)

#字符串
str01='003\t武則天\t女'
str01='003\t武則天\t男'
print(str01)

綜合案例演示:製作學生信息管理系統

nameinfo=[]

def printMemu():
print("="20)
print("==學生管理系統V1.0==")
print("="
20)
print("1:添加學生信息")
print("2:刪除學生信息")
print("3:顯示學生信息")
print("4:退出系統")

def add():
num=input("請輸入學生學號:")
nmae=input("請輸入學生姓名:")
sex=input("請輸入學生性別(男/女)")
newInfo={}
newInfo['num']=num
newInfo['name']=nmae
newInfo['sex']=sex
nameinfo.append(newInfo)
print(nameinfo)

def show():
print("="20)
print("學生信息如下:")
print("="
20)
print(nameinfo)

def delete():
del_num=input("請輸入要刪除的學生學號:")
for i in nameinfo:
if i['num']==del_num:
nameinfo.remove(i)

while True:
printMemu()
chose=int(input("請輸入功能相對應的數字"))
if(chose==1):
print("1:添加學生信息")
add()

if(chose==2):
    print("2:刪除學生信息")
            delete()

if(chose==3):
    print("3:顯示學生信息")
    show()
if(chose==4):
    print("4:退出系統")
    break

實現效果圖:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章