1+ #####
2+ # LISTS
3+ #####
4+ 5+ # Lists are mutable collections, we can modify them.
6+ # Elements of lists are inside [ ]
7+ 8+ courses = ['History' , 'Math' , 'Physics' , 'ComputerScience' ]
9+ print (courses )
10+ print (len (courses ))
11+ print (courses [0 ]) # Usage of index in lists
12+ print (courses [3 ])
13+ 14+ # Usage of negative index
15+ print (courses [- 1 ]) # Print the last one
16+ print (courses [- 2 ])
17+ 18+ # print(courses[4]) # Index Error
19+ 20+ print ('\n ' )
21+ 22+ # SLICING
23+ print (courses [0 :2 ]) # From 0 (included) to 2 (excluded)
24+ print (courses [:2 ])
25+ print (courses [1 :])
26+ 27+ 28+ print ('\n ' )
29+ 30+ 31+ # LIST METHODS
32+ # Add Remove
33+ courses .append ('Art' ) # Add a course to the list (at the end)
34+ print (courses )
35+ 36+ courses .insert (0 , 'Algebra' ) # Add 'Algebra' element in the position 0 of the list
37+ print (courses )
38+ 39+ courses_2 = ['Education' , 'Machine Learning' ]
40+ courses .extend (courses_2 ) # We want to add multiple values (at the end)
41+ print (courses )
42+ # WARN: courses.insert(0, courses_2) will add the entire courses_2 list as a single element in the course list.
43+ 44+ courses .remove ('Art' )
45+ print (courses )
46+ 47+ item = courses .pop () # Removes the last item (list usage as a stack or a queue), it returns the value
48+ print (courses )
49+ print (item )
50+ 51+ 52+ print ('\n ' )
53+ 54+ 55+ # Order
56+ 57+ courses .reverse () # Reverse order
58+ print (courses )
59+ 60+ courses .sort () # Sort items (str in alphabetical order)
61+ print (courses )
62+ 63+ nums = [1 , 3 , 7 , 32 , 2 ]
64+ nums .sort () # Sort items (int in ascending order)
65+ print (nums )
66+ 67+ nums .sort (reverse = True ) # Equals to nums.reverse()
68+ print (nums )
69+ 70+ 71+ print ('\n ' )
72+ 73+ 74+ # If we want to sort without altering the original list
75+ nums = [1 , 3 , 7 , 32 , 2 ]
76+ sorted_nums = sorted (nums )
77+ print (sorted_nums ) # That is sorted
78+ print (nums ) # The original one wasn't touched
79+ 80+ 81+ print ('\n ' )
82+ 83+ 84+ # Min, Max, Sum
85+ print (min (nums ))
86+ print (max (nums ))
87+ print (sum (nums ))
88+ 89+ print ('\n ' )
90+ 91+ 92+ # Find
93+ courses = ['History' , 'Math' , 'Physics' , 'ComputerScience' ]
94+ print (courses .index ('Math' )) # Print 'Math' index
95+ print (courses .index ('History' ))
96+ # print(courses.index('Art')) # Error
97+ print ('Art' in courses ) # Boolean result (Is there 'Art' in courses?)
98+ 99+ 100+ print ('\n ' )
101+ 102+ 103+ # Loop
104+ for item in courses : # Cicle the list calling every single cicle the element as 'item' and each time print it.
105+ print (item )
106+ 107+ print ('\n ' )
108+ 109+ for index , course in enumerate (courses ): # Same cicle that prints also the index of the element
110+ print (index , course )
111+ 112+ print ('\n ' )
113+ 114+ for index , course in enumerate (courses , start = 1 ): # Same cicle with numbers that starts from 1 instead of 0
115+ print (index , course )
116+ 117+ 118+ print ('\n ' )
119+ 120+ 121+ # Join
122+ courses = ['History' , 'Math' , 'Physics' , 'ComputerScience' ]
123+ course_str = ', ' .join (courses ) # Join the elements of the list in a single string
124+ print (course_str ) # This is a string
125+ 126+ # Split
127+ new_list = course_str .split (", " ) # Splits the string taking ", " as sepator and returns a List of strings
128+ print (new_list ) # This is a List of strings
129+ 130+ 131+ print ('\n ' )
132+ 133+ 134+ # Empty List (2 methods)
135+ empty_list = list ()
136+ empty_list = []
137+ 138+ 139+ # --------------------> <-------------------- #
0 commit comments