namesList = ['Java','Python','Android','C',"C ","C#","Object c","Swift"] print(namesList)
testList = [1, 'AI'] print(testList)
namesList = ['Java','Python','Android','C',"C ","C#","Object c","Swift"] print(namesList[0]) print(namesList[1]) print(namesList[2]) print(namesList[3])
Java Python Android C
namesList = ['Java','Python','Android','C',"C ","C#","Object c","Swift"] for name in namesList: print(name)
Java Python Android C C C# Object c Swift
namesList = ['Java','Python','Android','C',"C ","C#","Object c","Swift"] listSize=len(namesList) i=0 while i<listSize: print(namesList[i]) i =1
Java Python Android C C C# Object c Swift
#定义变量A,默认有3个元素 A = ['Shell','VC','VB'] print("-----添加之前,列表A的数据-----") for tempName in A: print(tempName) #提示、并添加元素 temp = input('请输入要添加的名称:') A.append(temp) print("-----添加之后,列表A的数据-----") for tempName in A: print(tempName)
-----添加之前,列表A的数据----- Shell VC VB 请输入要添加的名称:PHP -----添加之后,列表A的数据----- Shell VC VB PHP
#定义编程语言变量lang1,默认有3个元素 lang1 = ['Python','C ','Java'] #定义编程语言变量lang2,默认有3个元素 lang2=['JavaScript',"Go","SQL"] #lang1添加lang2 lang1.append(lang2) print(lang1) #输出结果是:['Python', 'C ', 'Java', ['JavaScript', 'Go', 'SQL']] lang1.extend(lang2) print(lang1) #输出结果是['Python', 'C ', 'Java', ['JavaScript', 'Go', 'SQL'], 'JavaScript', 'Go', 'SQL']
#定义编程语言变量lang1,默认有3个元素 lang1 = ['Python','C ','Java'] #在第2个位置添加Rust lang1.insert(2,"Rust") print(lang1) #输出结果:['Python', 'C ', 'Rust', 'Java']
#定义变量A,默认有3个元素 A = ['Python','C ','Java'] print("-----修改之前,列表A的数据-----") for tempName in A: print(tempName) #修改元素 A[1] = 'Scratch' print("-----修改之后,列表A的数据-----") for tempName in A: print(tempName)
-----修改之前,列表A的数据----- Python C Java -----修改之后,列表A的数据----- Python Scratch Java
#待查找的列表 nameList = ['Python','C ','Java',"Kotlin","MATLAB","Fortran"] #获取用户要查找的名称 findName = input('请输入要查找的名称:') #查找是否存在 if findName in nameList: print('在字典中找到了相同的名字') else: print('没有找到')
请输入要查找的名称:java 没有找到
#待查找的列表 nameList = ['Python','C ','Java',"Kotlin","MATLAB","Fortran"] #获取用户要查找的名称 #nameList.index("python",1,3) print(nameList.index("Java")) print(nameList.count("MATLAB")) print(nameList.count("C"))
2 1 0
nameList = ['Python','C ','Java',"Kotlin","MATLAB","Fortran"] print('------删除之前------') for tempName in nameList: print(tempName) del nameList[2] print('------删除之后------') for tempName in nameList: print(tempName)
------删除之前------ Python C Java Kotlin MATLAB Fortran ------删除之后------ Python C Kotlin MATLAB Fortran
nameList = ['Python','C ','Java',"Kotlin","MATLAB","Fortran"] print('------删除之前------') for tempName in nameList: print(tempName) nameList.pop() print('------删除之后------') for tempName in nameList: print(tempName)
------删除之前------ Python C Java Kotlin MATLAB Fortran ------删除之后------ Python C Java Kotlin MATLAB
nameList = ['Python','C ','Java',"Kotlin","MATLAB","Fortran"] print('------删除之前------') for tempName in nameList: print(tempName) nameList.remove("Kotlin") print('------删除之后------') for tempName in nameList: print(tempName)
------删除之前------ Python C Java Kotlin MATLAB Fortran ------删除之后------ Python C Java MATLAB Fortran
>>> a = [1, 4, 2, 3] >>> a [1, 4, 2, 3] >>> a.reverse() >>> a [3, 2, 4, 1] >>> a.sort() >>> a [1, 2, 3, 4] >>> a.sort(reverse=True) >>> a [4, 3, 2, 1]
programNames = [['Python','C ','Java'], ['nginx','Apache','Cloudflare','OpenResty'],['Oracle','MySQL','Microsoft SQL Server','PostgreSQL','MongoDB','Redis']]
#encoding=utf-8 import random # 定义一个列表用来保存3个办公室 offices = [[],[],[]] # 定义一个列表用来存储8位老师的名字 names = ['A','B','C','D','E','F','G','H'] i=0 for name in names: index = random.randint(0,2) offices[index].append(name) i=1 for tempNames in offices: print('办公室%d的人数为:%d'%(i,len(tempNames))) i =1 for name in tempNames: print("%s"%name,end='') print("\n") print("-"*20)