Lists and Tuples (Part 1) - "Python"

Hello everyone uu from CSDN, today Xiaoyalan’s content is a list in Python, let’s enter the world of lists


What is a list, what is a tuple

create list

access index

slice operation

iterate over list elements


What is a list, what is a tuple

In programming, it is often necessary to use variables to store/represent data .

If the number of data that needs to be expressed in the code is relatively small, we can directly create multiple variables.

num1 = 10

num2 = 20

num3 = 30

#......

But sometimes, the code needs to represent a lot of data, and you don’t even know how many data to represent. At this time, you need to use a list.

Lists are a way for programmers to represent/save data in batches in code

Just like when we go to the supermarket to buy spicy sticks, if we only buy one or two sticks of spicy sticks, then we just grab the spicy sticks and leave.

But if you buy ten or eight sticks at a time, it is not easy to hold them with your hands at this time, and the supermarket owner will give us a bag.

This bag is equivalent to a list

The hot strips in the bag are the data we want to represent

Compared with lists, tuples are very similar, except that which elements are placed in the list can be modified and adjusted, and the elements placed in the tuple are set when the tuple is created, and cannot be modified and adjusted.

The list is to buy spicy sticks in bulk. After the bag is packed, you can open the bag at any time, and then add more spicy sticks or take out some spicy sticks.

The tuple is to buy packaged spicy strips. After the manufacturer produces the spicy strips, the package is fixed so much and cannot be changed.

 


 create list

There are two main ways to create lists.

  • Create directly using literal values, [ ] means an empty list
  • Use list() to create
a=[]
print(type(a))

b=list()
print(type(b))

If you need to set the initial value inside, you can directly write it in [ ].

Use between elements to split

a=[1,2,3,4]
print(a)

The elements stored in the list are allowed to be of different types. (This is quite different from C++ Java).

a = [1, 'hello', True,[4,5,6]]
print(a)

Because list itself is a built-in function in Python, it is not appropriate to use list as a variable name.


access index

Any element in the list can be accessed through the subscript access operator [ ].

 We call the numbers filled in [ ] subscripts or indexes.

 

alist = [1, 2, 3, 4]
print(alist[2])

Note: The subscript starts counting from 0, so the subscript is 2, which corresponds to the element 3.  

Not only can the content of the element be read through the subscript, but also the value of the element can be modified.

alist = [1, 2, 3, 4]
alist[2] = 100
print(alist)

If the subscript is outside the valid range of the list, an exception will be thrown.

alist = [1, 2, 3, 4]
print(alist[100])

Because the subscript starts from 0, the effective range of the subscript is [0, list length - 1]. Use the len function to get the number of elements in the list.

alist = [1, 2, 3, 4]
print(len(alist))

 The subscript can take a negative number. It means "the penultimate element"

alist = [1, 2, 3, 4]
print(alist[3])
print(alist[-1])

 alist[-1] is equivalent to alist[len(alist) - 1]


slice operation

The subscript operation is to take out the first element in it at a time.

By slicing, a group of consecutive elements is taken out at a time, which is equivalent to getting a sublist

Use [ : ] for slice operations.

alist = [1, 2, 3, 4]
print(alist[1:3])

 

The 1:3 in alist[1:3] means [1, 3) such a closed-before-open-back interval composed of subscripts.

That is, it starts from the element with subscript 1 (2) and ends with the element with subscript 3 (4), but does not include the element with subscript 3.

So the final result is only 2, 3

  

Front and back boundaries can be omitted in slice operations

alist = [1, 2, 3, 4]

print(alist[1:])  # 省略后边界, 表示获取到列表末尾

print(alist[:2])  # 省略前边界, 表示从列表开头获取

print(alist[:-1]) # 此处切片中的下标也可以写成负数

print(alist[:])   # 省略两个边界, 表示获取到整个列表.

The slicing operation can also specify the "step size", that is, "how many steps to increment the subscript after each element is accessed"

alist = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(alist[::1])
print(alist[::2])
print(alist[::3])
print(alist[::5])

The step size specified by the slicing operation can also be a negative number, at this time, the elements are fetched from the back to the front. It means "after each element is accessed, the subscript will be decremented by a few steps"

alist = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(alist[::-1])
print(alist[::-2])
print(alist[::-3])
print(alist[::-5])

If the number filled in the slice is out of bounds, there will be no negative effect. Only the elements that meet the conditions will be obtained as much as possible.

alist = [1, 2, 3, 4]
print(alist[100:200])

 


 iterate over list elements

"Traversing" refers to taking out elements one by one, and then processing them separately.

The easiest way is to use a for loop

alist = [1, 2, 3, 4]
for elem in alist:
   print(elem)

 

 

You can also use for to generate subscripts according to the range, and access by subscript

alist = [1, 2, 3, 4]
for i in range(0, len(alist)):
  print(alist[i])

 It is also possible to modify the value of a list:

alist = [1, 2, 3, 4, 5 ]
for i in range(0, len(alist)):
  alist[i]=alist[i]+10
print(alist)

 

 You can also use a while loop. Manually control the change of the subscript

alist = [1, 2, 3, 4]
i = 0
while i < len(alist):
  print(alist[i])
  i += 1

 


Alright, this is the end of Xiao Yalan's learning content for today, let's continue to work hard! ! !

 

Guess you like

Origin blog.csdn.net/weixin_74957752/article/details/130137379