CHAPTER 6

Ace your homework & exams now with Quizwiz!

To access the first element of a list named my_list, you use the index __________.

0

True or False: Dictionaries in Python are ordered collections of items.

False (Dictionaries are unordered collections until Python 3.7, where they maintain insertion order.)

True or False: List indices in Python start at 1.

False (List indices in Python start at 0.)

True or False: A dictionary key in Python must be immutable.

True (Dictionary keys must be of an immutable type, like strings or numbers.)

True or False: Lists in Python can contain elements of different data types.

True (Lists can contain elements of various data types.)

True or False: The pop() method can be used to remove an item from a dictionary.

True (The pop() method can be used to remove an item from a dictionary by key and return the value.)

Explain how you would remove the second element from a list named items.

You can use the del statement or pop() method: del items[1] or items.pop(1)

How can you check if a particular key exists in a dictionary?

You can use the in keyword. For example: key in dictionary.

Which method is used to remove an item from a list by its value? a) remove() b) pop() c) del() d) discard()

a) remove() (The remove() method removes the first occurrence of a specified value.)

The __________ method is used to add an item to the end of a list.

append()

What is the difference between append() and extend() methods for lists?

append() adds a single element to the end of the list, whereas extend() adds elements from an iterable to the end of the list.

What is the output of the following code? my_dict = {"name": "Alice", "age": 25} print(my_dict.get("age")) a) None b) 25 c) "age" d) "Alice"

b) 25 (The get() method returns the value associated with the key "age".)

What will the following code output? fruits = ["apple", "banana", "cherry"] fruits.append("orange") print(fruits) a) ["apple", "banana", "cherry"] b) ["apple", "banana", "cherry", "orange"] c) ["apple", "banana", "cherry", "banana"] d) ["orange", "apple", "banana", "cherry"]

b) ["apple", "banana", "cherry", "orange"] (The append() method adds "orange" to the end of the list.)

Which of the following is a mutable data structure in Python? a) Tuple b) String c) List d) Integer

c) List (Lists can be modified after they are created, unlike tuples and strings.)

How do you access the value associated with a specific key in a dictionary? a) dictionary.key b) dictionary[key] c) dictionary.get(key) d) Both b and c

d) Both b and c (dictionary[key] and dictionary.get(key) are both valid ways to access a value in a dictionary.)

Write a Python function that takes a dictionary and a key, and returns the value associated with that key. If the key does not exist, return "Key not found".

def get_value(dictionary, key): return dictionary.get(key, "Key not found")

A __________ is an unordered collection of key-value pairs.

dictionary

You can use the __________ method to check if a key is present in a dictionary.

in

Write a code snippet to create a list of numbers from 1 to 10 using a list comprehension

numbers = [i for i in range(1, 11)]

The __________ method is used to remove an item from a list by its index.

pop()


Related study sets

A = student is female B = student is majoring in business

View Set

Patho Chp 24 Coronary Circulation Disorders

View Set

Telecom Chapter 8 - TCP/IP Internetworking I

View Set

Fundamentals of Nursing: Collecting and Analyzing Data

View Set

SOS World Geography -Unit 13: Final Exam

View Set