IttP 2 - Dictionaries Flashcards
3 main collections of data or data structures in Python are - (3)
- Lists
- Tuples
- Dictionaries
Wha are lists? - (2)
- Lists are used to store collecitons of elements in Python
- Lists are mutable, meaning you can add, remove, or modify elements after the list is created.
What is similar about lists and tuples? 0 (2)
Lists and tuples are both used to store collections of elements in Python
You can access elements of both lists and tuples using indexing (e.g., print(my_list[0]) and slicing (e.g, start:stop:step)
What is differences about lists and tuples? - (4)
Lists are mutable, meaning you can modify, add, or remove elements after the list is created.
Tuples are immutable, meaning once they are created, their elements cannot be changed, added, or removed.
Lists are denoted by square brackets [ ].
Tuples are denoted by parentheses ( ).
How is lists defined? - (2)
defined with square brackets
(e.g.,a=[1,2,3]).
H
How is tuples defined? - (2)
defined with round brackets
e.g., (a=(1,2,3))
What are dictitonaries?
Dictionaries in Python serve as a powerful data structure for storing mappings of key-value pairs
In real world, dicitonaries link words to defintiions
The equivalent to “words” in Python dictionaries are called… and the “definitions” are called - (2)
The equivalent to “words” in Python dictionaries are called keys and the “definitions” are called values.
So “words” → “definitions” maps to keys → values.
Unlike real dictionaries where one word can have many meanings, Python dictionaries,
Each key can only have a single value
Example that only Python dicionaries, each value can have a single value associated wih it which differs from real-world dictionaires where one word can have multiple meanings - (2)
For example, in a real-world dictionary, the word “bank” can refer to a financial institution as well as the edge of a river.
However, in a Python dictionary, each key is unique, and thus, the key “bank” could only be associated with a single value. the dictionary associates each key (“bank” and “river_bank”) with a single value.
How are Python dicionaries defined?
defined using curly braces ‘{ }’
Whatt does dicitonaries consist of inside curly braces? ‘{}’? - (2)
consist of comma-separated key-value pairs.
Each key-value pair is separated by a colon :
What is general syntax of dicitonaries in Python? - (3)
my_dict is the name of the dictionary variable.
key1, key2, key3, etc., are the keys of the dictionary.
value1, value2, value3, etc., are the corresponding values associated with the keys.
The type of a dictionary is..
dict
Write a code that has an empty dicitonary in variable my_dict
Print contents of my_dict
Print type of conttentts of my_dict
What would be the output of this code?
When we print the (empty) dictionary, we see thatt
Python uses the curly braces { } to indicate that we are printing a dictionary.
To add someitems to Python dictionaries, we use the same.. - (2)
same indexing notation we saw in the list examples in the previous session : square brackets: [ ].
Python always uses square brackets for indexing across different data structures such as lists, typles; the different bracket styles are only use in the initial creation of the variable (e.g., curly braces for dictionaries and parenthses ‘()’ for tuples)
For example,
How to add key of mykey and assign value of 20 in empty dictonary called my_dict?
Write a code that has
my_dict variable with empty dictionary
Assign key of ‘mykey’ to value 20
Assign key of ‘another key’ to value 100
Assign key of ‘Averylongkeyblahblahblah’ to value 1
Assign key ‘Navin R Johnson’ to value ‘2513. Elm St’
Prints dicionary my_dict
Prints out length of string using f-string
What will be output of this code?
Most Python data types can be used as dictionary keys.
This includes
strings, numbers (integers and floats) and tuples
Altthough most Python data types (e.g., strings, numbers, tuples) can be used as dictionary keys
Mutable types like lists can not be used as keys because..
they are not hashable (i.e., their values can change, which would disrupt the internal structure of the dictionary).
In Python dicionaries, any Python data type can be used as - (2)
a value
This includes integers, floats, strings, lists, tuples, dictionaries etc…