程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python Basics - List

編輯:Python

Catalog

1. Definition of list

1.1 Access any element in the list

1.2 modify 、 Add and remove list elements

2. Organization list

3. Index error

4. Operation list

4.1 Traverse the list

4.2 List of values

4.3 Use some elements of the list

4.4 Tuples


1. Definition of list

It consists of a series of elements sorted in a specific order , stay python Use in  [ ] To express , And use commas to separate elements .

eg:pets = ['cat','pig','tiger']

1.1 Access any element in the list

Just tell me the position or index of the element Python, Take the example above , Want to print out pig and tiger:

pets = ['cat','pig','tiger']
print(pets[1])
print(pets[2])

The first position of the index of the list is 0, The last element can be -1, It can also be a number that is counting the past

1.2 modify 、 Add and remove list elements

pets = ['cat','pig','tiger']
pets[0] = 'monkey' // Modify the first element
pets.append('bird') // Add elements to the end of the list , Use append
pets.insert(0,'Bus') // Insert element to start , Use insert
del pets[0] // Delete the beginning element , Use del
popped_pets = pets.pop() // Delete the element at the end
pets.remove('cat') // Delete elements... Based on values , Method remove() Delete only the first specified value . If the value to be deleted may appear more than once in the list , You need to use a loop to judge 

remarks :del and pop The difference is that :del Once deleted, it cannot be used , But use pop, After deletion, you can continue to use

2. Organization list

2.1 List permanent sort

Usage method sort(), Sort alphabetically

pets = ['cat','bird','tiger']
pets.sort()
print(pets)

The printed result is :['bird','cat','tiger'], If you want to arrange in reverse alphabetical order , It's in sort() Add parameters reverse =True that will do

2.2 Temporary sort

Usage method sorted(), Sort the list temporarily

pets = ['cat','bird','tiger']
print(sorted(pets))
print(pets)

The printed result is :

['bird','cat','tiger'] 

['cat','bird','tiger']

2.4 The list is printed backwards

Usage method reverse()

pets = ['cat','bird','tiger']
pets.reverse()
print(pets)

Print out :['tiger','bird','cat',]

2.5 Determine the length of the list

Usage method len( List name )

3. Index error

pets = ['cat','bird','tiger']
print(cars[3])

After running, it will appear IndexError, Then when the list is empty , Use -1 Index errors also occur in the index

4. Operation list

4.1 Traverse the list

4.1.1 Use for loop

name = ['Winny','vivian','coras']
for name in name:
print(name)

The input result is :

 4.1.2 Avoid indenting errors :

name = ['Winny','vivian','coras']
for name in name:
print(name)

here , After operation python Will report a mistake , Because it doesn't match the indentation .for The code executed on each element in the loop needs to be indented

4.2 List of values

4.2.1 Use function range() To print a series of numbers

for value in range(1,5)
print(value)

Print out 1,2,3,4, No printing 5

4.2.2 Using functions range() To create a list of numbers

number = list(range(1,5))
print (number)

The input result is [1,2,3,4]

4.2.3 Using functions range() Specify the step size

If you want to print 1-10 Previous even . be range(2,11,2), For from 2 Starting number , And then keep adding 2, Until the final value is reached or exceeded 11

4.2.4 Find the largest number in the list 、 The sum of the minimum and the sum

min(list_name)、max(list_name)、sum(list_name)

4.2.5 List of analytical

# Print 1-5 Squared to the list
square = [value*value for value in range(1,6)]

4.3 Use some elements of the list

4.3.1 section , And range The function is the same ,Python Stop after reaching the element before the second index you specify

name = ['Winny','kekey','coco','jos']
print(name[0:2])

The result of output and is ['Winny','kekey']

If the first index is not specified , Start from the list header ,print(name[:3]) -- The output 0,1,2 The value of the index

Empathy , No last index specified , Print to the end of the list

Print the last three elements :print(name[-3:])

4.3.2 Traversing slices , Then use for loop

4.3.3 Copy list , The method is to omit both the starting index and the ending index ([:])

name = ['Winny','kekey','coco','jos']
other_name = [:]

4.4 Tuples

4.4.1 Definition of tuple :python A value that cannot be modified is called immutable , An immutable list is called a tuple , but Use parentheses () Instead of square brackets to identify .

numbers =(0,10)
print(numbers[0])
print(numbers[1])

Results output :0 10

4.4.2 Traverse all values , Use for

4.4.3 Change tuple variable

dimensions =(200,50)

dimensions=(100,30)

be dimensions The value of the tuple is reassigned

4.4.4 Format the code

Use four spaces for each level of indentation 、 No more than is recommended per line 80 word ( stay Settings Inside code style Set up hard ware at Value )

The learning content of this article comes from 《Python Programming : From introduction to practice 》


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved