What's the difference between list, tuples and dictionaries in Python?
Lists
Lists are what they seem – a list of values. Each one of them is numbered, starting from zero – the first one is numbered zero, the second 1, the third 2, etc. You can remove values from the list, and add new values to the end.
Example: Your many cats’ names.
[code]
“A list”
cats = [‘Tom’, ‘Snappy’, ‘Kitty’, ‘Jessie’, ‘Chester’]
“If you want to print for example the first name on the list you would do the following”
print cats[0]
“Also if you want to add a name to the list you would do this”
cats.append(‘Catherine’)
“To remove a name you would do this”
del cats[0]
[/code]
Tuples
Tuples are just like lists, but you can’t change their values. The values that you give it first up, are the values that you are stuck with for the rest of the program. Again, each value is numbered starting from zero, for easy reference.
Example: the names of the months of the year.
[code]
“Here is an example of a tuple”
months = (‘January’,’February’,’March’,’April’,’May’,’June’,
‘July’,’August’,’September’,’October’,’November’,’ December’)
[/code]
“So that’s the difference between lists and tuples. A list can be modified but a tuple cannot be modified in anyway(unless changed in the source code of the program)”
Dictionaries:
Dictionaries are similar to what their name suggests – a dictionary. In a dictionary, you have an ‘index’ of words, and for each of them a definition. In python, the word is called a ‘key’, and the definition a ‘value’. The values in a dictionary aren’t numbered – tare similar to what their name suggests – a dictionary. In a dictionary, you have an ‘index’ of words, and for each of them a definition. In python, the word is called a ‘key’, and the definition a ‘value’. The values in a dictionary aren’t numbered – they aren’t in any specific order, either – the key does the same thing. You can add, remove, and modify the values in dictionaries.
Example: telephone book.
[code]
“Here is a simple phonebook”
phonebook = {‘Andrew Parson’:8806336,
‘Emily Everett’:6784346, ‘Peter Power’:7658344,
‘Lewis Lame’:1122345}
“To add a name to the phonebook you would do the following”
phonebook[‘Gingerbread Man’] = 1234567
“If you want to remove a name you would do the same as you would do in a list”
del phonebook[‘Andrew Parson’]
[/code]
Lists are what they seem – a list of values. Each one of them is numbered, starting from zero – the first one is numbered zero, the second 1, the third 2, etc. You can remove values from the list, and add new values to the end.
Example: Your many cats’ names.
[code]
“A list”
cats = [‘Tom’, ‘Snappy’, ‘Kitty’, ‘Jessie’, ‘Chester’]
“If you want to print for example the first name on the list you would do the following”
print cats[0]
“Also if you want to add a name to the list you would do this”
cats.append(‘Catherine’)
“To remove a name you would do this”
del cats[0]
[/code]
Tuples
Tuples are just like lists, but you can’t change their values. The values that you give it first up, are the values that you are stuck with for the rest of the program. Again, each value is numbered starting from zero, for easy reference.
Example: the names of the months of the year.
[code]
“Here is an example of a tuple”
months = (‘January’,’February’,’March’,’April’,’May’,’June’,
‘July’,’August’,’September’,’October’,’November’,’ December’)
[/code]
“So that’s the difference between lists and tuples. A list can be modified but a tuple cannot be modified in anyway(unless changed in the source code of the program)”
Dictionaries:
Dictionaries are similar to what their name suggests – a dictionary. In a dictionary, you have an ‘index’ of words, and for each of them a definition. In python, the word is called a ‘key’, and the definition a ‘value’. The values in a dictionary aren’t numbered – tare similar to what their name suggests – a dictionary. In a dictionary, you have an ‘index’ of words, and for each of them a definition. In python, the word is called a ‘key’, and the definition a ‘value’. The values in a dictionary aren’t numbered – they aren’t in any specific order, either – the key does the same thing. You can add, remove, and modify the values in dictionaries.
Example: telephone book.
[code]
“Here is a simple phonebook”
phonebook = {‘Andrew Parson’:8806336,
‘Emily Everett’:6784346, ‘Peter Power’:7658344,
‘Lewis Lame’:1122345}
“To add a name to the phonebook you would do the following”
phonebook[‘Gingerbread Man’] = 1234567
“If you want to remove a name you would do the same as you would do in a list”
del phonebook[‘Andrew Parson’]
[/code]