Day 2: Lists and Dictionaries
Mastering lists and dictionaries in Python involves understanding their functionalities, properties, and common operations. Here's a comprehensive breakdown to get you started:
Lists:
-
Definition: An ordered collection of items enclosed in square brackets
[]
. Elements can be of any data type, including other lists and dictionaries. - Creation:
my_list = [1, "hello", 3.14, [True, False]]
-
Accessing elements: Use zero-based indexing starting from
[0]
. Negative indices access from the end ([-1]
is the last element).
first_element = my_list[0] # first_element will be 1
last_element = my_list[-1] # last_element will be [True, False]
-
Slicing: Extract a sublist using colon (
:
) notation.[start:end:step]
:-
start
(inclusive): index of the first element to include (defaults to 0). -
end
(exclusive): index of the element after the last element to include (defaults to the end of the list). -
step
: the difference between consecutive elements to include (defaults to 1).
-
sublist = my_list[1:3] # sublist will be ["hello", 3.14]
-
Common operations:
-
append(element)
: Add an element to the end of the list. -
insert(index, element)
: Insert an element at a specific index. -
remove(element)
: Remove the first occurrence of an element. -
pop(index)
: Remove and return the element at a specific index (or the last element by default). -
len(list)
: Get the length of the list (number of elements). -
sort()
: Sort the list in ascending order (modifies the original list). -
reversed()
: Return a reversed iterator for the list (doesn't modify the original list).
-
Exercises:
- Print out a list of items in reversed form
- Find the sum of the items.
- Find the largest of the items in a list.
- Find the length of the list.
- Remove the odd number from the list.
Solutions
- Print out a list of items in reversed form
original_list = [1, 2, 3, 4, 5]
reversed_list = original_list[::-1]
print("Original list:", original_list)
print("Reversed list:", reversed_list)
Output
Original list: [1, 2, 3, 4, 5]
Reversed list: [5, 4, 3, 2, 1]
- Find the sum of the items.
original_list = [1, 2, 3, 4, 5]
total = sum(original_list)
print(total)
Output
15
- Find the largest of the items in a list.
original_list = [1, 2, 3, 4, 5]
largest = max(original_list)
print(largest)
Output
5
- Find the length of the list.
original_list = [1, 2, 3, 4, 5]
length = len(original_list)
print(length)
Output
5
- Remove odd numbers from the list
original_list = [1, 2, 3, 4, 5]
even_number = [num for num in original_list if num%2 == 0]
print(even_number)
Output
[2, 4]
Dictionaries:
-
Definition: An unordered collection of key-value pairs enclosed in curly braces
{}
. Keys must be unique and immutable (e.g., strings, numbers, tuples), while values can be any data type. - Creation:
my_dict = {"name": "Alice", "age": 30, "hobbies": ["reading", "hiking"]}
-
Accessing elements: Use the key enclosed in square brackets
[]
to access the corresponding value.
name = my_dict["name"] # name will be "Alice"
-
Common operations:
-
get(key, default)
: Get the value for a key, returningdefault
if the key is not found. -
keys()
: Return a view of all keys in the dictionary. -
values()
: Return a view of all values in the dictionary. -
items()
: Return a view of all key-value pairs as tuples. -
in
: Check if a key exists in the dictionary. -
update(dict2)
: Update the dictionary with key-value pairs from another dictionary (dict2
). -
pop(key, default)
: Remove the key-value pair and return the value, ordefault
if the key is not found.
-
Exercises:
- Print all names, email addresses and the key-value pairs in a dict.
- Check if a user exist in a dict.
- Update the info in the dict.
Solutions:
- Print all names, email addresses and the key-value pairs in a dict.
- Check if a user exist in a dict
- Update the info in the dict.
Projects
- Simon Says: To create this program, define a list called simon_says that contains five different instructions for the user to follow. The program should then prompt the user to pick a number between 0 and 4, and store the user's input in a variable called index. The program should use the value of index to select an instruction from the simon_says list, and then print the instruction to the screen using the print function. The program should display a message that says "Simon says..." followed by the selected instruction.
- Sandwich Order: Create a program to calculate a sandwich order and print the total cost.
Sandwich Order
Top comments (0)