We are advancing Python learning according to Learning Roadmap for Python Beginners on Tommy's blog. This time, I will deal with the first 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
How to make a list and how to refer to it Lists are ** sequenced and mutable **
Tuple and range are immutable. You can enclose a tuple variable in list () to make it a new variable
list can also be put in list
1
list_num = [1,2,3,4] # 1,2,3,Make a list with 4
print(list_num) # list_In num display
Execution result
[1, 2, 3, 4]
2 In addition to lists, sequence types include tuples, ranges, etc., but these are immutable and data cannot be added. Enclose the tuple variable in list () to make it a new variable (the value assigned at the time of declaration cannot be changed) Conversion of tuples to list type
tuple_samp = (1,2,3,4)
print(tuple_samp)
print(type(tuple_samp)) #Type confirmation
list_samp = list(tuple_samp)
print(list_samp)
print(type(list_samp))
Execution result
(1, 2, 3, 4)
<class 'tuple'>
[1, 2, 3, 4]
<class 'list'>
Same for range
range_samp = range(4)
print(range_samp)
print(type(range_samp))
list_samp = list(range_samp)
print(list_samp)
print(type(list_samp))
Execution result
range(0, 4)
<class 'range'>
[1, 2, 3, 4]
<class 'list'>
3
list_0 = [1,2,3]
list_1 = [4,5,6]
list_2 = [7,8,9]
list_two_dim = [list_0,list_1,list_2] #Put the list in the list
print(list_two_dim)
Execution result
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
[Introduction to Python] Summary of how to use list (Part 1)
Recommended Posts