Chapter 5: Lists and Dictionaries
45 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What is the result of calling the sort method on a list?

  • It returns the sorted elements as a tuple.
  • It does not change the original list.
  • It alters the original list in place. (correct)
  • It returns a new list with sorted elements.
  • Which of the following methods is classified as a mutator method?

  • reverse (correct)
  • len
  • count
  • map
  • What value does a mutator method return upon execution?

  • It returns the original list.
  • It returns None. (correct)
  • It returns the length of the list.
  • It returns the modified list.
  • Consider the following list: example = [4, 2, 10, 8]. What will example look like after executing example.sort()?

    <p>[2, 4, 8, 10]</p> Signup and view all the answers

    When comparing elements in a list for sorting, which operation is primarily used?

    <p>both greater than (&gt;) and less than (&lt;)</p> Signup and view all the answers

    What is the primary difference between a list and a dictionary in Python?

    <p>Lists allow manipulation of data values by position, while dictionaries associate data values with keys.</p> Signup and view all the answers

    Which of the following is a method that can manipulate a list in Python?

    <p>append()</p> Signup and view all the answers

    In what scenario would a dictionary be preferred over a list?

    <p>When you need to store data in pairs of related values.</p> Signup and view all the answers

    Which statement best describes the access of items in a list?

    <p>Items must be accessed by their index values.</p> Signup and view all the answers

    What is a key characteristic of dictionaries compared to lists?

    <p>Dictionaries associate values with unique keys.</p> Signup and view all the answers

    How do you define a simple function that returns a value in Python?

    <p>def function_name(): return value;</p> Signup and view all the answers

    Which method would you use to remove an item from a list in Python?

    <p>remove()</p> Signup and view all the answers

    What type of data can be stored in a list in Python?

    <p>Any data type, including mixed types.</p> Signup and view all the answers

    What does the operator 'in' do when applied to a list?

    <p>It checks if an element is present in the list</p> Signup and view all the answers

    When replacing an element in a list using the subscript operator, what is being referenced?

    <p>An element’s position within the list</p> Signup and view all the answers

    What will be the output of 'print([1, 2, 3, 4])' in Python?

    <p>[1, 2, 3, 4]</p> Signup and view all the answers

    What will the following code snippet produce? 'numbers = [2, 3, 4, 5]; for index in range(len(numbers)): numbers[index] = numbers[index] ** 2'

    <p>[4, 9, 16, 25]</p> Signup and view all the answers

    What does the method L.append(element) accomplish?

    <p>Adds element to the end of L</p> Signup and view all the answers

    Which statement about lists is true?

    <p>Lists can have their elements added, removed, or replaced.</p> Signup and view all the answers

    If L contains 4 elements, which index would L.insert(index, element) place the element at if index is 5?

    <p>4</p> Signup and view all the answers

    What is the result of the operation 'words = sentence.split()' where sentence is 'This example has five words.'?

    <p>['This', 'example', 'has', 'five', 'words.']</p> Signup and view all the answers

    After executing 'for index in range(len(words)): words[index] = words[index].upper()', what will 'words' contain?

    <p>['THIS', 'EXAMPLE', 'HAS', 'FIVE', 'WORDS.']</p> Signup and view all the answers

    What would L.pop() return if L is currently [3, 5, 7]?

    <p>7</p> Signup and view all the answers

    Which of the following accurately describes L.extend(aList)?

    <p>Adds the elements of aList to the end of L</p> Signup and view all the answers

    What happens if you attempt to access an index in a list that does not exist?

    <p>You receive an IndexError.</p> Signup and view all the answers

    What happens when the L.insert(index, element) method is called with index less than 0?

    <p>The element is added at index 0</p> Signup and view all the answers

    How does L.pop(index) differ from L.pop()?

    <p>It removes and returns the element at a specified index</p> Signup and view all the answers

    What will be the state of example after executing example.insert(1, 10) on the initial list [1, 2]?

    <p>[1, 10, 2]</p> Signup and view all the answers

    If L contains [1, 2, 3] and L.pop(0) is executed, what will L be afterward?

    <p>[2, 3]</p> Signup and view all the answers

    What does the method append do in a list?

    <p>Adds a new element to the end of the list</p> Signup and view all the answers

    What is the result of using the extend method?

    <p>Adds elements from another list to the end of the current list</p> Signup and view all the answers

    What happens if you try to find the index of an element that is not in the list using the index method?

    <p>Raises an error</p> Signup and view all the answers

    What does the pop method do when called without an argument?

    <p>Removes the last element from the list</p> Signup and view all the answers

    Which of the following gives the correct behavior of the in keyword when used with lists?

    <p>Checks for an element's presence and returns True or False</p> Signup and view all the answers

    What is a defining characteristic of each item in a list?

    <p>Each item has a unique index.</p> Signup and view all the answers

    What is the result of the following operation: example + [14, 15]?

    <p>Creates a new list with elements of example followed by 14 and 15</p> Signup and view all the answers

    What will be the contents of the 'example' list after executing example.pop(0)?

    <p>[2, 10, 11, 12]</p> Signup and view all the answers

    Which of the following represents a valid list literal in Python?

    <p>all of the above</p> Signup and view all the answers

    How can you create a list of integers from 1 to 4 in Python?

    <p>both A and C</p> Signup and view all the answers

    What will the statement print when the target 45 is searched in aList = [34, 45, 67]?

    <p>1</p> Signup and view all the answers

    What does the len function return when applied to a list?

    <p>The count of items in the list.</p> Signup and view all the answers

    What will the output be for the expression first[2:4] given first = [1, 2, 3, 4]?

    <p>[3, 4]</p> Signup and view all the answers

    What is the correct output of the expression first + [5, 6]?

    <p>[1, 2, 3, 4, 5, 6]</p> Signup and view all the answers

    When comparing two lists using the equality operator (==), what does it return?

    <p>True if the lists have identical elements.</p> Signup and view all the answers

    Which of the following is NOT a method for creating a list?

    <p>Using tuples.</p> Signup and view all the answers

    Study Notes

    Chapter 5: Lists and Dictionaries

    • Lists are sequences of data values (items or elements)

    • List items can be of any type

    • Examples include shopping lists, to-do lists, rosters, guest lists, recipes, documents, and phone books

    • Each item in a list has a unique index specifying its position (starting from 0)

    • List Literals: Examples include ['apples', 'oranges', 'cherries'], [[5, 9], [541, 78]]

    • Lists can contain expressions to be evaluated, such as [x, math.sqrt(x)] when x = 2 becomes [2, 1.4142135623730951] or [x + 1] when x=2 becomes [3]

    • Lists of integers can be created using the range function, such as list(range(1, 5)) which becomes [1, 2, 3, 4]

    • The list() function can create a list from any iterable sequence. For instance, list("Hi there!") produces ['H', 'i', ' ', 't', 'h', 'e', 'r', 'e', '!']

    • Basic List Operators:

      • The len function, square brackets [] for accessing elements, the + operator for concatenation, and the == operator for equality all work as expected with lists
      • Examples - len(first) = 4, first[0] = 1, first + [5, 6] produces [1, 2, 3, 4, 5, 6], and comparing a list to another as in first == second
    • Printing Lists: To display list content, use the print() function. For instance, print([1, 2, 3, 4]) yields [1, 2, 3, 4]

    • in Operator (Membership Testing): The in operator checks if an element is present in a list. 3 in [1, 2, 3] is True, while 0 in [1, 2, 3] is False

    Replacing an Element in a List

    • Lists are mutable. Elements can be changed
    • The subscript operator [] is used to replace elements, e.g., example = [1, 2, 3, 4]; example[3] = 0; example becomes [1, 2, 3, 0]
    • The index refers to an element's position within the list, not the list itself

    List Methods (Inserting and Removing Elements)

    • append(e): Adds e to the end of a list
    • extend(aList): Adds elements in aList to the end of the list
    • insert(index, element): Inserts element at index (index) in the list. Inserts element at the end if index is out of bounds
    • pop(): Removes and returns element at the end of the list; returns None
    • pop(index): Removes and returns the element at index

    List Methods (Continued)

    • append, extend, insert, pop, and sort fall under the category of "mutator methods" and typically don't return a value
    • Python commonly returns None with these methods
    • aList.sort() or print(aList.sort()) returns None

    Searching a List

    • in operator: Checks for an element's existence in a list but does not return the element's position
    • index() method: Locates an element's position in a list. Raises an error if the element is not found.

    Sorting a List

    • Lists are ordered by position – elements maintain their order.
    • The sort() method rearranges a list's elements based on natural ordering using "<", ">", and "=="
    • example = [4, 2, 10, 8]; example.sort(); example = [2, 4, 8, 10]

    Studying That Suits You

    Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

    Quiz Team

    Related Documents

    Description

    Explore the foundational concepts of lists and dictionaries in programming. This quiz covers list creation, indexing, and basic operations, along with practical examples. Test your understanding of how lists can store various data types and evaluate expressions.

    More Like This

    Python List Basics
    3 questions

    Python List Basics

    SpectacularWaterfall avatar
    SpectacularWaterfall
    Python Basics and List Fundamentals
    9 questions
    Computer Science Lecture 11-12
    43 questions

    Computer Science Lecture 11-12

    EnviousEnlightenment2588 avatar
    EnviousEnlightenment2588
    Python Programming Module II - Lists
    21 questions
    Use Quizgecko on...
    Browser
    Browser