Skip to content

Final-PythonFoundationHomework/Tuples-Sets-Enumerate-Zip-and-Lists

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Python Data Structures Practice

1. Tuple Tasks

1.1 Create a tuple of 3 favorite colors and print the second one

Input: ("blue", "green", "red")
Output: "green"

1.2 Swap the values of two variables using a tuple

Input: a = 5, b = 10
Output: a = 10, b = 5

1.3 Count how many times 3 appears in the tuple

Input: (1, 3, 3, 7, 3)
Output: 3

1.4 Check if the number 7 exists in a tuple

Input: (1, 2, 3, 4)
Output: False

1.5 Find the oldest person from a list of tuples

Input: [("Ali", 20), ("Sara", 25), ("John", 22)]
Output: ("Sara", 25)

2. Set Tasks

2.1 Remove duplicates from a list using a set

Input: [1, 2, 2, 3, 4, 4, 5]
Output: {1, 2, 3, 4, 5}

2.2 Check if two sets have any common elements

Input: set1 = {1, 2, 3}, set2 = {4, 5, 3}
Output: True

2.3 Find the union of two sets

Input: {1, 2}, {2, 3}
Output: {1, 2, 3}

2.4 Find the difference between two sets

Input: {1, 2, 3}, {2, 3, 4}
Output: {1}

2.5 Check if a set is a subset of another

Input: {1, 2} in {1, 2, 3, 4}
Output: True

3. Enumerate Tasks

3.1 Use enumerate to print each item in a list with its index

Input: ["apple", "banana", "cherry"]
Output:
0 apple
1 banana
2 cherry

3.2 Count how many even numbers are at even indexes

Input: [2, 4, 6, 3, 8]
Output: 2

3.3 Replace items in a list with their index using enumerate

Input: ["a", "b", "c"]
Output: [0, 1, 2]

3.4 Print only items at odd indexes

Input: ["x", "y", "z", "w"]
Output: ["y", "w"]

3.5 Create a dictionary from a list using enumerate

Input: ["cat", "dog", "fish"]
Output: {0: "cat", 1: "dog", 2: "fish"}

4. Zip Tasks

4.1 Combine two lists into a list of tuples using zip

Input: [1, 2], ["a", "b"]
Output: [(1, "a"), (2, "b")]

4.2 Add corresponding numbers from two lists

Input: [1, 2, 3], [4, 5, 6]
Output: [5, 7, 9]

4.3 Create a dictionary from two lists

Input: ["name", "age"], ["Alice", 30]
Output: {"name": "Alice", "age": 30}

4.4 Zip three lists together

Input: [1], [2], [3]
Output: [(1, 2, 3)]

4.5 Find which students passed using names and scores

Input: names = ["Ali", "Sara"], scores = [45, 85]
Output: ["Sara"]  # Assume passing score is 60

5. List Tasks

5.1 Add an element to the end of a list

Input: [1, 2, 3], add 4
Output: [1, 2, 3, 4]

5.2 Remove the first element from a list

Input: ["a", "b", "c"]
Output: ["b", "c"]

5.3 Sort a list in descending order

Input: [5, 2, 9, 1]
Output: [9, 5, 2, 1]

5.4 Count how many times "apple" appears in a list

Input: ["apple", "banana", "apple"]
Output: 2

5.5 Replace all even numbers with 0

Input: [1, 2, 3, 4]
Output: [1, 0, 3, 0]

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages