We are advancing Python learning according to Learning Roadmap for Python Beginners on Tommy's blog. This time, I will deal with the latter half of [Introduction to Python] Summary of how to use list (Part 1).
--Those who are studying on Tomi-san's blog --For those who want to get an overview of how to use list
Google Colaboratory
Reference list value, slice
Number of elements in the list, maximum value, minimum value
#Reference of list value No(index)
#How to refer to the list value by element No
list_num = [1,2,3,4] #1,2,3,Create a list of 4
print(list_num[0]) #Print the left edge of the list
Execution result
1
#From the opposite
print(list_num[-1]) #Print the right edge of the list
print(list_num[-3]) #Print the third from the right of the list
Execution result
4
2
#slice
list_num = [0,1,2,3,4,5,6,7,8,9,10] #0,1,2,3,4,5,6,7,8,9,Create a list of 10
print(list_num[1:5]) #Print from 1 to 5
Execution result
[1, 2, 3, 4] *Note that it is acquired in the range up to one before the end element
When you want to get it in a jump
print(list_num[1:10:2]) #Print in 2 steps between 1 and 10
Execution result
[1, 3, 5, 7, 9]
When you want to get in 2D
list_two_dim =[[1,2,3],[4,5,6],[7,8,9]]
print(list_two_dim[0][1])
Execution result
2
0: 1, 2, 3 In other words, the first column of the 0th row is printed. So it will be 2 1: 4, 5, 6 The side of the row is also 0, 1, 2 from the left 2: 7, 8, 9
#Number of elements in the list len()
list_num = [0, 1, 2, 3, 4] #0,1,2,3,4 list creation
print(len(list_num)) #len()In list_Print the number of elements in num
Execution result
5
#Maximum value
list_num = [0,4,2,5,2,6]
print(max(list_num))
#minimum value
print(min(list_num))
Next time, I plan to add append, expend, insert and delete clear, pop, remove, del.
[Introduction to Python] Summary of how to use list (Part 1)