DEV Community

Cover image for Day 2 of 30 days of Python
Blackie
Blackie

Posted on • Updated on

Day 2 of 30 days of Python

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]]
Enter fullscreen mode Exit fullscreen mode
  • 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]
Enter fullscreen mode Exit fullscreen mode
  • 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]
Enter fullscreen mode Exit fullscreen mode
  • 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:

  1. Print out a list of items in reversed form
  2. Find the sum of the items.
  3. Find the largest of the items in a list.
  4. Find the length of the list.
  5. Remove the odd number from the list.

Solutions

  1. 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)
Enter fullscreen mode Exit fullscreen mode

Output

Original list: [1, 2, 3, 4, 5]
Reversed list: [5, 4, 3, 2, 1]
Enter fullscreen mode Exit fullscreen mode
  1. Find the sum of the items.
original_list = [1, 2, 3, 4, 5]
total = sum(original_list)
print(total)
Enter fullscreen mode Exit fullscreen mode

Output

15

  1. Find the largest of the items in a list.
original_list = [1, 2, 3, 4, 5]
largest = max(original_list)
print(largest)
Enter fullscreen mode Exit fullscreen mode

Output

5

  1. Find the length of the list.
original_list = [1, 2, 3, 4, 5]
length = len(original_list)
print(length)
Enter fullscreen mode Exit fullscreen mode

Output

5

  1. 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)
Enter fullscreen mode Exit fullscreen mode

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"]}
Enter fullscreen mode Exit fullscreen mode
  • Accessing elements: Use the key enclosed in square brackets [] to access the corresponding value.
name = my_dict["name"]  # name will be "Alice"
Enter fullscreen mode Exit fullscreen mode
  • Common operations:
    • get(key, default): Get the value for a key, returning default 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, or default if the key is not found.

Exercises:

  1. Print all names, email addresses and the key-value pairs in a dict.
  2. Check if a user exist in a dict.
  3. Update the info in the dict.

Solutions:

  1. Print all names, email addresses and the key-value pairs in a dict. carbon
  2. Check if a user exist in a dict carbon (1)
  3. Update the info in the dict. carbon (2)

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.

## Solutions
Simon Says
simon-says

Sandwich Order

sandwich

Top comments (0)