Python example of CRUD operation function based on list list implementation
- 2020-06-23 00:53:04
- OfStack
This article gives an example of Python's CRUD operation based on list list. To share for your reference, specific as follows:
This article before seeing your first understand python basic knowledge, such as control flow, variables, data types, list, tuples, dictionary basic operation, the purpose of this article is mainly or consolidate python basis, so if you don't understand the python students proposed first to understand, read the article again, of course there are writing bad place a lot of flowers and applause. Without further ado, understand the needs first
The requirements are as follows:
1. Interface list
Welcome to User management systems
1 Add user
2 Delete user
3 Modify the user
4 Query the user
5 exit
2. Delete, modify and query according to id
3. There is no deletion, modification or query item, corresponding prompt is given, and corresponding prompt is given for the operation result
4. Initialize 3 users
5. The user has attributes id, name,password, and USES three lists to save data
The code is as follows:
# User management system
# Initialize the 3 A user
ids = ['1','2','3']
names =[' zhang 3',' li 4',' Wang Wu ']
pws =['root','abc123','123456']
# Create a menu
print("=== Welcome to User management systems ===")
print("1 Add user ")
print("2 Delete user ")
print("3 Modify the user ")
print("4 Query the user ")
print("5 exit ")
type = int(input(" Please select: "))
while type>=1 and type<=5:
if type==1:
id = input(" Please enter the user's id:")
name = input(" Please enter the user's name: ")
pw = input(" Please enter user password: ")
ids.append(id)
names.append(name)
pws.append(pw)
print(" Added successfully! ")
elif type==2:
# Determine if the user is present ids There are
id = input(" Please enter the user's id:")
if id in ids:
# Gets the subscript value
index = ids.index(id)
ids.pop(index)
names.pop(index)
pws.pop(index)
print(" Delete the success ")
else:# No user prompt was found
print(" No deleted user found !")
elif type==3:
# Determine if the user is present ids There are
id = input(" Please enter the user's id:")
if id in ids:
# Gets the subscript value
index = ids.index(id)
names[index]= input(" Please enter the user's name :")
pws[index]= input(" Please enter the user's password :")
print(" Modify the success ")
else:# No user prompt was found
print(" No modifications were found !")
elif type==4:
# Determine if the user is present ids There are
id = input(" Please enter the user's id:")
if id in ids:
# Gets the subscript value
index = ids.index(id)
print(" The user's id:",ids[index])
print(" The user name :",names[index])
print(" The user password :",pws[index])
else:# No user prompt was found
print(" No user was queried !")
elif type==5:
break
type = int(input(" Please select: "))
else:
print(" Incorrect input! ")
Of course, here is the simple version, and the object-oriented version and io version will be updated in the future
More about Python related content interested readers to view this site project: "common database operations Python skills summary", "Python mathematical operation skills summary", "Python data structure and algorithm tutorial", "Python function using techniques", "Python string skills summary", "Python introduction and advanced tutorial" and "Python file and directory skills summary"
I hope this article has been helpful in Python programming.