Data structures are structures used for holding various data together. Built in data structures in Python are: list, tuple and dictionary. There are more but our focus is on those three.
Lists hold an ordered collection of elements. Imagine list as shopping list where you can add, remove or search for something you want to buy. List is a mutable data type. Mutable means we can change the value of element in a list. Making a list in Python is done using the following syntax:
Graphically our shopping list is represented as:
Every element has its own index. Counting indexes starts from 0. Syntax for accessing elements is
Example: Printing first element in a list:
When you buy something then you want to remove it from a list.
Example: Removing element from a list by value:
shoppingList.remove['milk'] print shoppingList
Sometimes you need to remove element using index. For that purpose we use pop().
Example: Removing element from a list using index:
shoppingList.pop(2) print shoppingListIf you don't specify index then last element will be removed from a list.
What if you want to remove elements with index in range from 1 to 3?
Example: Removing elements in range from 1 to 3
del shoppingList[1:4] print shoppingList
Be careful when you are using slicing. Index of last element you want to delete must be increased by 1.
Example: Checking does element exists in a list
print 'apples' in shoppingList
Returned value is boolean type. Boolean type has only two values: True or False. When you are using it in if statements make sure you type it correctly (true is wrong. True is correct)
To get better understanding of lists we will write an application which will exchange values of first element in a list with last element in a list.
firstElement=shoppingList lastElement=shoppingList[-1] shoppingList=lastElement shoppingList[-1]=firstElement print shoppingList
Index -1 is used to get last element in a list.
Use debugger and go through code if you don't understand the code.
Just like we can access elements in a list we can access the letters in a string. Remember: Strings are immutable. It means you can do the following using a list:
shoppingList=”Fruit” print shoppingListNow, list looks like this:
The value of apple is rewritten with value “Fruit”. This is not true for strings.
The following is legal:
name='Tux' print name
Adding element to a list:
Here are few statements you should test:
name=”This is tricky” print name[8:] print name[-1] print name[:-1] print name[:4]
This could help you to understand slicing in Python:
Tuples are immutable. Creating tuples is similar to list creation. Instead of using [ and ] we use ( and )
Example: Creating tuple called workingDays
Creating tuple with one element is a little bit different than usual creation.
After element you must put comma as shown in example.
Example: Tuple concatenation
numberSet1=(1,2,3) numberSet2=(4,5,6) print numberSet1+numberSet2
The output is (1,2,3,4,5,6)
Checking is element in a list:
print 1 in numberSet1
Dictionaries are mutable. Dictionaries consists of pairs of keys and their values.
Example: Creating dictionary
Pairs in a dictionary are called items.
Printing value of key codecall:
Deleting dictionariy elements:
Removing all entries in dictionary:
Today, there is no challenge. In next tutorial we will do programming project.