## Lists
amazon_cart = [
'notebooks',
'sunglasses',
'toys',
'grapes',
'bikes'
]
print(amazon_cart[1]) # sunglasses
# List slicing - note that list slicing produces a new list and does not change the original one
print(amazon_cart[1:4:2]) # sunglasses, grapes - The stop parameter is not included so 1:4 means indexes 1, 2, 3
print(amazon_cart[0::2]) # Prints all items with even indexes
# Lists are muttable unlike strings:
amazon_cart[0] = 'laptops'
print(amazon_cart)
new_cart = amazon_cart[:] # This notation creates a copy of the amazon list and assigns it to the new_cart.
# Writing new_cart = amazon_cart will make both point to the samelist meaning that changes in new_cart will cause changes in the
# amazon_cart since they both actually the same object
# Matrix
matrix = [
[1, 2, 3],
[2, 4, 6],
[7, 8, 9]
]
print(matrix[1][1]) # second row, second column
basket = [1, 2, 3, 4, 5]
# list's length:
print(len(basket))
# adding items - append - changes the original list. Not creating a new one
basket.append(6)
print(basket)
# adding - insert - changes the original list. Not creating a new one
# The insert method adds the given object in the given index
basket.insert(1, 7) # insert the value 7 in the second place
print(basket)
# Extend - Adding a sequence of itmes (iterable) to an existing list:
basket.extend([8, 9])
print(basket)
# Remove item. The pop function returns the removed object
basket.pop() # Pops the value in the end of the list. To remove a value in a specific index we specify a value in the parenthesis:
basket.pop(0) # removes the first item in the list
print(basket)
# To remove an item according to its value and not its index we use the remove method:
basket.remove(7) # The remove method does not return anything. Just modifies the list
print(basket)
basket.clear() # removes all the items in the list creating an empty list (does not return any value)
print(basket)
basket = ['a', 'b', 'c', 'd', 'e', 'd']
# Getting the index of the first object with the specified value:
# print(basket.index('d', 1, 3)) # get the index of the character 'd'. Start looking from index 1 to index 3
# (3 is not included so we will get a message that the item is not in the list)
# Another way of checking existance:
print('x' in basket) # Returns False
print(basket.count('d')) # How many times the letter 'd' appears in the list (1)
basket.sort() # Modifies the list
print(basket)
# In order to sort without modifying the list we use the sorted built=in function:
basket = ['a', 'x', 'f', 'd', 'b']
print(sorted(basket))
print(basket)
# reversing the items in the list (not sorting it in the opposite direction):
basket.reverse() # Modifies the list
print(basket)
print(basket[::-1]) # returns a reversed list without modifying the original list
print(basket)
# Creating a list with values from 1 to 99:
print(list(range(1, 100))) # start included, stop is not included
# Combining a list into a string:
print(' '.join(['Hi,', 'my', 'name', 'is', 'Eran'])) # This adds the space between all items in the list
# List unpacking
a, b, c, *other, d = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print(a) # 1
print(b) # 2
print(c) # 3
print(other) # [4, 5, 6, 7, 8]
print(d) # 9
# Dictionary
dictionary = {
'a': 1,
'b': 2,
'x': 3
}
print(dictionary['b']) # print the value of 'b' (2)
dictionary = {
123: [1, 2, 3],
True: 'hello'
}
print(dictionary[123])
print(dictionary[True])