编程语言
首页 > 编程语言> > doraemon的python 列表

doraemon的python 列表

作者:互联网

1.列表

append,在列表最后追加一个元素

#_author:Doraemon Liu
# -*- coding:utf-8 -*-

# 列表的最后添加元素
"""user = []
name = input("请输入你的姓名:")
user.append(name)
print(user)
"""
# 用户名与密码的录入
users = []
for i in range(0,3):
   name = input("请输入你的姓名:")
   users.append(name)
   print(users)

用户名和密码的校验
username = input("请出入你的账号")
password = input("请出入你的密码")
for a in users:
   result = users.split(",")
   user = users[0]
   pwd = users[1]
   if user == username and pwd == password:
       print("登录成功")
       break

 

insert.在指定位置插入元素

user = ["刘丹妮","刘佳","刘新宇"]
user.insert(1,"小黑")
print(user)

 

remove/pop删除指定元素

user = ["刘丹妮","刘佳","刘新宇"]
user.remove("刘佳")
print(user)
user = ["刘丹妮","刘佳","刘新宇"]
user.pop(0)
print(user)
user = ["刘丹妮","刘佳","刘新宇"]
user.pop()  #默认删除最后一个
print(user)

 

clear清空

user = ["刘丹妮","刘佳","刘新宇"]
user.clear()
print(user)
user = ["liudanni",0,[11,222,333,444],[1,["jl","kl",2],5]]
user[0]
user[0][2]
user[3][-1]

2.元组

1.元组的书写规范

user = [11,222,33,"liudanni"]
user = (11,222,33,"liudanni")

2.公共功能

  1. 索引

  2. 切片

  3. 步长

  4. for循环

  5. len

3.独有功能(无)

4.特殊:元组中的元素不可被修改、删除

v1 = (11,22,33)
v1[1] = 999  #错误
v1 = 999   #正确

#嵌套
v2 = (11,22,33,(11,22,33))
v[-1][1] = 99   #错误
v2[-1] = 99   #正确

v3 = (11,22,33,[11,22,33])
v3[3] = 99  #错误 这里嵌套的是列表
v3[3][1] = 99  #正确

 

标签:11,users,python,doraemon,列表,33,user,print,刘佳
来源: https://www.cnblogs.com/doraemon548542/p/11131755.html