New Python day two

Python list creation

Create an empty list for example: fruit = []
Create a list with elements, for example: Fruit = ['apply ',' Banana ',' cherry ']

Finding Python list elements

print(fruit)    #Get all elements
print(fruit[:-1])#Equivalent to the effect above
print(fruit[:])  #Equivalent to the effect above
print(fruit[0])  #Getting a single element
print(fruit[0:3])#Get 0 actually~2 Elements between Subscripts(Head and tail)
print(fruit[:3]) #Equivalent to the effect above
print(fruit[-1]) #Get the last element
print(fruit[-2:-1])#Get only the next to last element
print(fruit[-2:])#From the last to the last

Adding and modifying Python list elements

fruit.append('pear')   #Add elements at the end of the list
fruit.insert(1,'grape')#Add element at subscript 1,Move other elements one bit back
fruit[0]='tomato'     #Substitution of elements

Deletion of Python list elements

fruit.remove('tomato') #Remove an element by name
del fruit[0]           #Remove element with subscript
fruit.pop(index=0)     #The effect is the same as above,Remove last by default
fruit.clear()          #Empty all elements of the list

Python list other functions

print(fruit.index('apple'))  #Search for subscript
print(fruit.count('apple'))  #Statistics of the number of elements
fruit.sort()         #according to ascall Code sorting
fruit.reverse()       #Reverse list
fruit.extend([1,2,3])  #Add list to fruit End of list
del fruit           #Delete list
fruit2=fruit.copy()      #Layer 1 independent replication,The second layer copies the memory address
# fruit2=fruit[:]        #Ditto(Role can be associated with account)
# fruit2=list(fruit)     #Ditto
import copy
fruit2=copy.deepcopy(fruit)#True full replication

Loop of Python list

for i in fruit:
for i in range(len(fruit)):
for index,data in enumerate(fruit):

Creating Python tuples

Create an empty tuple for example: fruit = ()
Create a tuple with elements, for example: Fruit = ('apply', 'Banana', 'cherry')

Use of Python tuples

Tuple is a read-only list that can't be added, deleted or modified, but can be queried (through the search of the list above)

Python trial ox knife

How to write the shopping cart program: the user enters the interface to input the amount - > print the list of goods - > select the goods - > deduct the money - > join the purchase cart - > Exit

#readme:Run program input amount,Enter product list,Choose digital purchase,Press q Sign out
#Input of amount
while True:
    cash=input('How much money do you need?:')
    if cash.isdigit():
        print('Please enter a positive integer!')
while cash>=0:
    #Print product list
    for i in range(len(commodity)):
        print('%s. %s Price $%s'%(i+1,commodity[i][0],commodity[i][1]))
    print('Your amount:$%s'%(cash))
    #Conditions for stopping purchase
    if commodity_id=='q':
    if commodity_id.isdigit():
        commodity_id = int(commodity_id)
        print('Please enter a positive integer!')
    if commodity_id > len(commodity) or commodity_id < 1:
        print('No items')
    comm_index = commodity_id-1
    #Main logical judgment
    if int(commodity[comm_index][1]) <= cash:
        cash = cash - int(commodity[comm_index][1])
        print('Successful purchase%s'%(commodity[comm_index][0]))
        print('I'm sorry you don't have enough money')
print('------Your shopping cart------')
for i in shopping_car:
print('Your balance $%s'%(cash))

Tags: Python

Posted on Mon, 04 May 2020 07:05:11 -0400 by Kerry Kobashi