1/70
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
What is the syntax for creating a dictionary in Python?
Use curly braces with key-value pairs: {"key": "value"}
How are keys and values separated in a dictionary?
Keys and values are separated by a colon :.
How do you access a value from a dictionary using a key?
Use bracket notation: dictionary["key"]
What error occurs when accessing a missing key with bracket notation?
A KeyError is raised.
How do you safely access a key that might not exist?
Use .get(key, default) to return a value or default.
How do you add a new key-value pair to a dictionary?
Use bracket notation: dict["new_key"] = value
What does the .update() method do for dictionaries?
It adds multiple key-value pairs at once from another dictionary.
What’s an example of using .update() on a dictionary?
dict.update({"a": 1, "b": 2})
What are common ways to update a dictionary?
Use [] for one key or .update() for multiple keys.
What does dict.keys() return?
A view object of all dictionary keys.
What does dict.values() return?
A view of all dictionary values.
How do you loop through all keys in a dictionary?
for key in dict.keys():
How do you loop through all values in a dictionary?
for value in dict.values():
What structure is ideal for representing name-distance data?
A dictionary with names as keys and distances as values.
How do you loop through a dictionary to get both key and value?
Use .items() in a loop: for k, v in dict.items():
What is the purpose of dict.pop("key")?
It removes a key and returns its value.
What happens if you use .pop() on a non-existent key?
Raises a KeyError.
What does dict.clear() do?
Removes all key-value pairs from the dictionary.
How do you check if a key exists using .keys()?
if key in dict.keys():
What does .items() return when used on a dictionary?
A view object of key-value pairs as tuples.
What is a for loop used for in Python?
To iterate over elements in a sequence.
What does range(len(list)) produce?
A sequence of index numbers for the list.
How do you access list elements using a loop and indices?
for i in range(len(list)):
list[i]
What is the more Pythonic way to iterate over a list?
for element in list:
What does each iteration in a for loop represent?
One pass through the loop's code block.
How does a for loop help with readability?
By reducing repetitive code and improving structure.
Why are for loops flexible?
They work with any iterable and adapt to changes in data size.
How do you create a list in Python?
Use square brackets with comma-separated items.
Example: my_list = [1, 2, 3]
What does the append() method do in a list?
Adds one item to the end of the list.
Example: my_list.append(4)
What does the extend() method do in a list?
Adds multiple elements from another iterable to the list.
Example: my_list.extend([5, 6])
What does the remove() method do in a list?
Removes the first matching value from the list.
Example: my_list.remove(2)
What does the insert() method do in a list?
Inserts an item at a specified index.
Example: my_list.insert(1, "apple")
What does the reverse() method do in a list?
Reverses the elements in place.
Example: my_list.reverse()
What is a list comprehension in Python?
A concise way to create a new list from an iterable.
Example: [x*2 for x in nums]
How do you use list comprehension to lowercase all words?
Use .lower() on each word inside the comprehension.
Example: [word.lower() for word in words]
How do you filter items in a list comprehension?
Add an if condition after the loop in the comprehension.
Example: [x for x in nums if x > 5]
What is a dictionary comprehension?
A shorthand to build a dictionary from an iterable.
Example: {x: x*2 for x in nums}
How do you count word occurrences using dictionary comprehension?
Use .count() in the value part.
Example: {w: words.count(w) for w in words}
What does the .pop() method do in a list?
Removes and returns the last item in the list.
Example: item = my_list.pop()
What does the clear() method do in a list?
Removes all items from the list.
Example: my_list.clear()
What value does pop() return?
It returns the item that was removed.
Example: last = actions.pop()
What value does clear() return?
It returns None.
Example: actions.clear()
How do you slice a substring using indices?
Use string[start:end] to get part of the string.
Example: s[0:3]
What happens if you omit the start index in slicing?
It defaults to 0 (start of string).
Example: s[:3]
What happens if you omit the end index in slicing?
It slices to the end of the string.
Example: s[5:]
How does negative indexing work in Python strings?
Negative indices count from the end.
Example: s[-4:]
Why are phone numbers often stored as strings?
They may include non-numeric characters like - or +.
What is a tuple in Python?
An ordered, immutable, indexed collection of elements.
How do you create a tuple in Python?
Use parentheses with comma-separated values: (42.376, -71.115)
How are latitude and longitude stored using a tuple?
coordinates = (latitude, longitude)
How do you access the first value in a tuple?
Use indexing: tuple[0]
How do you access the second value in a tuple?
Use indexing: tuple[1]
How can you unpack a tuple into two variables?
latitude, longitude = coordinates
What is the difference between a tuple and a list?
A tuple is immutable (can’t be changed); a list is mutable (can be changed).
Can you modify a tuple's value after creation?
No, tuples are immutable (cannot be changed)
What error occurs if you try to change a tuple's value?
TypeError: 'tuple' object does not support item assignment
Why use a tuple instead of a list when values won’t change?
Tuples are more memory-efficient than lists.
How do you check a tuple’s memory size in Python?
Use sys.getsizeof(tuple) (import sys)
When should you use a tuple?
When data won’t change and memory efficiency matters.
What is a while loop used for in Python?
Repeating code while a condition remains true.
What is the syntax for a while loop?
while condition:
# code block
What happens if the while loop condition is never false?
It creates an infinite loop.
When should you use a while loop over a for loop?
When you don’t know how many times to loop in advance.
How do you update a variable inside a while loop?
Reassign it inside the loop block: moisture = Sample()
How can you track iterations in a while loop?
Use a counter variable like days += 1
What is an example of a while loop condition with a variable?
while moisture > 20:
What does this print if moisture drops below 20?
python
CopyEdit
print("Time to water!")
It alerts that the plant needs watering.
Why is a while loop good for handling randomness?
It adapts to changing conditions until the requirement is met.
How do you simulate changing input in a while loop?
Re-sample or update the variable inside the loop.
How do you print the day number during each loop?
Use print("Day", days, "moisture:", moisture, "%")
What is the key benefit of while loops with unknown durations?
They repeat until conditions are met, not for a fixed number.