pythonoutput
pythonoutput
tasks = [
["Task A", 1, "Pending"],
["Task B", 2, "In Progress"],
["Task C", 1, "Pending"]
]
high_priority = [task[0] for task in tasks if task[1] == 1]
tasks[0][2] = "In Progress"
print(high_priority)
print(tasks)
Answer:
What is the output of this tuple unpacking and concatenation for a flight
itinerary?
flight = (101, ("JFK", "LAX"), ("2025-04-27", "08:00"))
(dep, arr), (date, _) = flight[1:]
new_itinerary = flight[:1] + ((dep, "SFO"),) + ((date, "10:00"),)
print(new_itinerary)
Answer:
{'Movie B'}
{'Movie A', 'Movie B'}
What is the output of this nested dictionary update for a booking system?
bookings = {
"Flight101": {"passenger": "Alice", "seats": ["A1", "A2"]},
"Flight102": {"passenger": "Bob", "seats": ["B1"]}
}
bookings["Flight101"]["seats"].append("A3")
bookings["Flight103"] = {"passenger": "Charlie", "seats": []}
print(bookings["Flight101"]["seats"])
print(len(bookings["Flight103"]["seats"]))
Answer:
What is the output of this list sorting with a custom key for a leaderboard?
players = [
{"name": "Alice", "score": 100, "time": 120},
{"name": "Bob", "score": 100, "time": 110},
{"name": "Charlie", "score": 90, "time": 130}
]
players.sort(key=lambda x: (-x["score"], x["time"]))
print([player["name"] for player in players])
Explanation: The sort() method uses a key function sorting by score (descending,
hence -x["score"]) and time (ascending) as a tiebreaker. Alice and Bob have the
same score (100), but Bob’s time (110) is less than Alice’s (120), so Bob comes
first. Charlie’s lower score (90) places him last. The comprehension extracts
names: ["Bob", "Alice", "Charlie"].
What is the output of this tuple in a set operation for unique coordinates?
points = {(1, 2), (3, 4), (1, 2)}
other_points = {(3, 4), (5, 6)}
unique_points = points.symmetric_difference(other_points)
print(unique_points)
Answer: {1, 3}
Explanation: The set comprehension creates a set of id values from products where
category is "Electronics". Products with IDs 1 and 3 match, so the result is {1,
3}. Sets are unordered, but the values are unique.
Answer:
What is the output of this nested list modification for a seating chart?
seating = [
["A1", "A2", None],
["B1", None, "B3"]
]
seating[0][2] = seating[1][0]
seating[1].pop(0)
print(seating)
Why can’t a list be used as a dictionary key, and what alternative would you use?
Alternative: Use a tuple, which is immutable and hashable, as a key if the list’s
contents are fixed.
Code:
Use Case: In a game, use a tuple key (position = (x, y)) to map coordinates to game
objects, ensuring immutability.
Q7: How would you reverse a list in Python, and what are the differences between
the methods?
Code:
lst = [1, 2, 3]
# Method 1: reverse()
lst_copy = lst.copy() # Avoid modifying original
lst_copy.reverse()
# lst_copy is [3, 2, 1]
# Method 2: slicing
reversed_lst = lst[::-1]
# reversed_lst is [3, 2, 1]
# Method 3: reversed()
reversed_lst2 = list(reversed(lst))
# reversed_lst2 is [3, 2, 1]
Differences:
In-Place vs. New List: reverse() modifies the original list, while [::-1] and
reversed() create new lists.
Memory: reverse() is memory-efficient (O(1) space), while [::-1] and reversed() use
O(n) space for the new list.
Flexibility: reversed() returns an iterator, useful for lazy evaluation.