python_day2

Introduction: List manipulation, tuples, shopping cart programs

1. List operation

# Author: Er

#List operations 

name = [ " name " , " age " , " hah " ]
 del name[2 ]
 print ( " delete del: " , name)

name.remove(name[0])
print("删除remove: ", name)

name.append("li")
print("追加append: ", name)

name.insert(2, "xiong")
print("插入insert: ", name)

name[0] = "info"
print("修改: ", name)

_name = ["my", "name", "is", "er"]
name.extend(_name)
print("扩展extend:", name)

_name_copy = name.copy()
print(_name_copy)

count = name.count("is")
print("is 的个数为:", count)

name.sort()
print ( " sort sort: " , name)

name.reverse()
print ( " reverse reverse: " , name)

#Get the subscript 
print ( " er's subscript is: " , name.index( " er " ))


#Tuples are similar to lists, but once created, they cannot be modified, only count, index 
names = ( " my " , " name " , " is " )
Code

operation result

E:\PY\venv\Scripts\python.exe E:/PY/day2/list_all.py
delete del: ['name', 'age']
remove remove: ['age']
Added append: ['age','li']
Insert insert: ['age', 'li', 'xiong']
Edit: ['info', 'li', 'xiong']
Extend extend: ['info', 'li', 'xiong', 'my', 'name', 'is', 'er']
['info', 'li', 'xiong', 'my', 'name', 'is', 'er']
The number of is is: 1
Sort sort: ['er', 'info', 'is', 'li', 'my', 'name', 'xiong']
Reverse reverse: ['xiong', 'name', 'my', 'li', 'is', 'info', 'er']
The subscript of er is: 6

Process finished with exit code 0

  2, copy copy

# Author:Er
import copy
name = ["my", "name", "is", "er", ["li", "xiong", "fei"]]
_name = copy.copy(name)
_name = name[:]
_name = list(name)      #All of the above are shallow copy, only one layer (can be changed individually), in the second layer (all changed at the same time) (for example, bank deposits) 
_name1 = copy.deepcopy(name   ) #deep Copy 
del name[4 ][0]
 print (_name, _name1)
 for i, j in enumerate(name):     #before the number, after the content 
    print (i, j)

for i in name:
    print(name.index(i), i)
Code

operation result

E: \ PY \ venv \ Scripts \ python.exe E: /PY/day2/new_dir/test.py
['my', 'name', 'is', 'er', ['xiong', 'fei']] ['my', 'name', 'is', 'er', ['li', ' xiong', 'fei']]
0 my
1 name
2 is
3 er
4 ['xiong', 'fei']
0 my
1 name
2 is
3 er
4 ['xiong', 'fei']

Process finished with exit code 0

  

# About the application of shallow copy
Suppose A and B have common property count = [ "name", [balance]]
A = ["A", balance]
B = ["B", balance]
In this way, AB can modify the name at any time, but because the account balance is shallowly copied from count, it is count- when consuming, but AB cannot change it.

  3. Shopping cart

# Author: Er

#shopping cart program 
product_list = [
    ( " Mobile " , 1699 ),
    ( " Computer " , 5000 ),
    ( " Bracelet " , 200 )
]

shopping_cart = []

salary = input( " Please enter the balance: " )
 if salary.isdigit():                 # Determine whether the input is a number 
    salary = int(salary)
     while True:
         print ( " **********Product list** ******** " )
         for _index, item in enumerate(product_list):
             print (_index, item)                    # index number item list content 
        print ( " *************** ************ " )
        choice_list = input( " Please enter the purchase item number: " )
         if choice_list.isdigit():
            choice_list = int(choice_list)
            if choice_list < len(product_list):
                if salary >= product_list[choice_list][1]:
                    salary -= product_list[choice_list][1]
                    shopping_cart.append(product_list[choice_list])
                    print ( " Your current balance is: " , salary)
                 else :
                     print ( " Your current balance is: %d, the balance is insufficient! " % salary) #Print           format else 
            : print
                 ( " There is no such item! " )                                        # Determine whether to quit 
        elif choice_list == " q " :
             #Print the current shopping cart information 
            print ( " Your current balance is: " , salary)
             print ( "--------Shopping Cart--------- " )
             for item in shopping_cart:
                 print (item)
             print ( " ----------------- ----- " )
            exit()                                                           #Exit else :
             print ( 
        " Imported error ! Please re-enter: " )

else :
     print ( " Incorrect input! " )
Code

operation result

E:\PY\venv\Scripts\python.exe E:/PY/day2/new_dir/shopping.py
Please enter balance: 10000
**********Product List**********
0 ('Mobile', 1699)
1 ('computer', 5000)
2 ('Bracelet', 200)
***************************
Please enter the purchase item number: 0
Your current balance is: 8301
**********Product List**********
0 ('Mobile', 1699)
1 ('computer', 5000)
2 ('Bracelet', 200)
***************************
Please enter the purchase item number: 1
Your current balance is: 3301
**********Product List**********
0 ('Mobile', 1699)
1 ('computer', 5000)
2 ('Bracelet', 200)
***************************
Please enter the purchase item number: 5
There is no such product!
**********Product List**********
0 ('Mobile', 1699)
1 ('computer', 5000)
2 ('Bracelet', 200)
***************************
Please enter the purchase item number: 3
There is no such product!
**********Product List**********
0 ('Mobile', 1699)
1 ('computer', 5000)
2 ('Bracelet', 200)
***************************
Please enter the purchase item number: q
Your current balance is: 3301
--------shopping cart---------
('Mobile', 1699)
('computer', 5000)
----------------------

Process finished with exit code 0

  *The above is for your own review only*

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324516841&siteId=291194637