doraemon的python 列表
作者:互联网
1.列表
-
公共功能:
-
len
-
索引
-
切片
-
步长
-
for循环
-
练习题
# content = input("请输入内容:")
# content = content.split("+")
# v1 = int(content[0])
# v2 = int(content[1])
# v = v1 + v2
# print(v) -
删除
user = ["刘丹妮","刘佳","刘新宇"]
#方式一
user.pop(0)
print(user)
#方式二
del user[1]
print(user)
注意:
字符串本身不能修改或者删除【不可变类型】
列表是可变类型
-
-
独有功能
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)
-
总结
-
增
-
append / insert
-
-
删
-
remove/pop/clear/del
-
-
改
-
user[3] = "新值"
-
-
查
-
索引/切片
-
-
-
列表的嵌套
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.公共功能
-
索引
-
切片
-
步长
-
for循环
-
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