Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions python basics/lists.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""
Basic comprehension about lists in Python and methods
"""

#create empty list
numbers = []

#Initialize with some values, or strings
numbers = [1, 2, 3]

#Inserting values at the end of the list with 'append' method
numbers.append('four')

"""
Or you can add a new value at a given position in list
The parameters of 'insert' are the position and the value
"""
numbers.insert(0, 'zero')

#You can print a list
print(numbers)

"""
You can remove an item if it's on the list by its value
(if its not on the list it will raise a ValueError)
"""
numbers.remove('four')

#Removing an item by its index
del(numbers[0])

#Count the occurences of a given value in the list
print(numbers.count(1))

#you can reverse the list
numbers.reverse()
print(numbers)

#if the list is unordered, you can sort it
numbers.sort()