1/15
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
|---|
No study sessions yet.
and - statement
Evaluates to True if both conditions are True
or - statement
Evaluates to True if at least one condition is True
type()
Function to confirm the data type of a variable
How do you check if the substring "apple" is in the string assigned to fruits? fruits = "banana, cherry, apple"
"apple" in fruits
Which line of code correctly checks the data type of item_price to see if it is a float?
Which method is used to add an item to the end of a list in Python?
append()
How do you get a value from a dictionary without knowing if the key exists, to avoid an error?
dictionary.get(“key”)
# List of recent sales descriptions
recent_sales = [
"Buy 2 Get 1 Free: Red Bull",
"50% Off Holiday Cookies",
"$1 Off Baking Soda"
]
# Grocery inventory
inventory = {
"Holiday Cookies": [(434, 1.99), 30, False, recent_sales[1]],
"Ice Cream Sandwich": [(556, 0.89), 14, True, None]
}
# Value to check
print(inventory["Holiday Cookies"][3]) → What will the following print statement return?
What will the following code output?
my_list = [1, 2, 3]
my_list.append([4, 5])
print(my_list)
How can you retrieve a value from a dictionary (dict) by its key (key)?
dict[key]
dict.get(key)
What is the difference between a tuple and a list?
The primary difference is that lists are mutable (can be changed) while tuples are immutable (cannot be changed).
What will this code do?
my_tuple = (1, 2)
my_tuple[1] = 5
print(my_tuple)
Tuples are immutable, so attempting to change an element directly will result in an error.
What will this code do?
my_list = [1, 2, 3]
new_list = my_list
new_list.append(4)
print(my_list)
It will print [1, 2, 3, 4]. Assigning new_list to my_list makes new_list a reference to my_list. Therefore, changes to new_list also affect my_list.
How do you update a value (value) in a dictionary? (dict)
dict[key] = value
How would you change "green" to "yellow"?
colors = ["red", "green", "blue"]
colors[1] = “yellow”
To change an element in a list, you can directly access it using its index. The index of "green" is 1, so using colors[1] = "yellow" updates "green" to "yellow". The other options are incorrect because they either reference the wrong index or use nonexistent methods for lists.