1. Defining arrays
City = [] #定义一个空数组
name = [' Jack ',' Panda ', 'Bob ',' Franck ']
# based on the subscript to find the element, the index of the first element is 0, the last element subscript is -1
Print (name[2])
Print (name[-1])
2. Add elements
City.append (' Beijing ') # Add an element at the end of the list
City.insert (0,' Shanghai ') # Add an element at the specified position
3. Modifying elements
city[' Nanjing ' # Modify the Subscript 1 element to Nanjing, if the specified subscript does not exist, will error
4. Deleting elements
City = [‘Shanghai‘,‘Beijing‘,‘Shenzhen‘]
City.pop (-1)#Specify subscript Delete Element
Del city[-1]# Specify subscript delete element
City.remove (‘Shanghai‘)# Delete the specified element
City.clear ()# Clear list
5. Query elements
Print (city[0]) # Specify location
# Gets the index of the element, and if the element is not found, an error occurs. If a list has more than one element, the index of the first element is returned
Print (City.index (' Shenzhen '))
Print (City.count (' Shenzhen ')) # See how many times the element appears in the list
6. Reversal
My_list = [' python ',' jmeter ',' Charles ',' postman ']
# Reverse the original list , but don't return anything.
Print (my_list)
7. Sorting
NUMS1 = [22,11,5,77,99,3,7,8,9,10]
NUMS2 = [' F ',' E ',' B ',' H ',' I ',' Q ',' P ',' K ',' V ',' C ']
Nums1.sort ()# Ascending
Nums1.sort (reverse = True) # Descending
8. Merging arrays
Print (NUMS1+NUMS2)
# upload elements from the NUMS2 array to the array nums1
Print (NUMS1)
9. Copying an array
Print (nums1*2)
Print (' Jack ' *3)
10. Array Exercises
#Verifying that the user name is legitimate
# (1)Enter user name
# (2) If the account is present, the hint has been registered and if it does not exist, it can be registered.
# (3) cannot be empty
# (4) user name length between 2-12
# (5) enter up to 3 times
All_user = [‘Ju‘,‘Nuo Hanyang‘,‘Li Nina‘,‘Zheng Taona‘]
For IInchRange3):
Username =Input‘Please enter your user name:'). Strip ()
IfLen (username) >2andLen (username) <13:
If All_user.count (username) >0:
Print‘User name already exists‘)
Else
Print (' can register ')
Break
Else
print (' user name input not valid ')
Else
print (' excessive number of errors ')
Python Learning ==> Array (i)