Python List Operations Quiz
42 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 does the expression a[2:5:2] return given the sequence a = [1,2,3,4,5]?

  • [3, 5] (correct)
  • [1, 3, 5]
  • [2, 4]
  • [3, 4]
  • What will be the result of len(b) if b = ['a', 'b', 'c', 'd']?

  • 2
  • 4 (correct)
  • 5
  • 3
  • Which of the following returns True if the element 'c' is present in sequence b?

  • 'c' in b (correct)
  • 'c' not in b
  • b.count('c') > 0
  • b.contains('c')
  • What is the output of a + b when a = [1, 2, 3, 4, 5] and b = ['a', 'b', 'c', 'd']?

    <p>[1, 2, 3, 4, 5, 'a', 'b', 'c', 'd']</p> Signup and view all the answers

    What will 3 * a yield if a = [1, 2, 3]?

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

    What will be the output of printing a[-2] given a = ['a', 'b', 'c', 'd', 'e']?

    <p>'d'</p> Signup and view all the answers

    Which method can be used to remove an element from a list if you only know its value?

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

    What will be the output of print(a[::-2]) given a = ['a', 'b', 'c', 'd']?

    <p>['d', 'b']</p> Signup and view all the answers

    What is the result of using a.copy() on a list in Python?

    <p>It creates a shallow copy of the list.</p> Signup and view all the answers

    Given the list 'l = [1, 2, 3, 4]', what is the output of l.pop()?

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

    What will be the result of l.remove(3) if l = [1, 5, 2, 3, 4, 0]?

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

    Which Python method reorders the elements of a list in reversed order?

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

    What will be the output of the expression [i for i in range(1,100,2)]?

    <p>All odd numbers within the specified range</p> Signup and view all the answers

    Which of the following statements about list comprehension is correct?

    <p>It simplifies the process of creating lists using for loops.</p> Signup and view all the answers

    What error might occur when using mutable objects as default function arguments?

    <p>Unexpected behavior due to shared references</p> Signup and view all the answers

    What will be the output of the following code? print("First Name" "Last Name")

    <p>&quot;First Name&quot; &quot;Last Name&quot;</p> Signup and view all the answers

    Which escape character would you use to insert a backslash in a string?

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

    What is the result of the following set operation: a.remove(3.14) when a = {1, 3.14, 'Some String'}?

    <p>{1, 'Some String'}</p> Signup and view all the answers

    Which of the following statements about sets in Python is true?

    <p>Sets only accept hashable objects as elements.</p> Signup and view all the answers

    How can you create an empty set in Python?

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

    What will be printed after executing print("Here is a backslash character: \")?

    <p>Here is a backslash character: \</p> Signup and view all the answers

    What is the output of the code print(s1+s2) provided s1 and s2 have multiple line strings?

    <p>Line 1 Line 2 Muhammad Ali John Smith</p> Signup and view all the answers

    What does the escape character do in a string?

    <p>Inserts a tab space.</p> Signup and view all the answers

    What does the in operator return when checking if 'wed' is in 'Sweden'?

    <p>'wed' is found within 'Sweden'</p> Signup and view all the answers

    What is the output of the following code: for day in l: print(day) where l = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']?

    <p>All days of the week will be printed, one at a time</p> Signup and view all the answers

    What is the purpose of the function apply_operation in the provided example?

    <p>To apply the same operation on all elements of a sequence</p> Signup and view all the answers

    What does the method s1.lower() do to the string s1?

    <p>It converts all characters in s1 to lowercase</p> Signup and view all the answers

    Which of the following statements about the s1.replace(s2, s3) method is correct?

    <p>It replaces all occurrences of s2 in s1 with s3</p> Signup and view all the answers

    What is necessary to create a tuple with a single element?

    <p>A comma must be placed after the element.</p> Signup and view all the answers

    Which of the following statements about tuples is TRUE?

    <p>Tuples can be used in for loops.</p> Signup and view all the answers

    What value does the expression (2,3,4) in (1,2,3,4,5) evaluate to?

    <p>False, since tuples cannot be checked within a tuple</p> Signup and view all the answers

    What does the method s1.rfind(s2) return?

    <p>The index of the last occurrence of s2 in s1</p> Signup and view all the answers

    What does the expression a[2:4] return when a = ('a', 'b', 'c', 'd')?

    <p>('b', 'c')</p> Signup and view all the answers

    When using the map function with a lambda expression, what is the expected output when applied as list(map(lambda x: x*x, [1,2,3]))?

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

    What occurs when attempting to change an element of a tuple?

    <p>A TypeError is raised.</p> Signup and view all the answers

    What happens if an attempt is made to create a string using the same type of quotes within its string content?

    <p>It will raise a syntax error</p> Signup and view all the answers

    Which of the following statements about lists is correct?

    <p>Lists can use negative indexing.</p> Signup and view all the answers

    What will the output be for b = [1,2,3,4] followed by print(b)?

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

    If a = (1, 2, 3, 4) is compared to (1, 2, 4, 3), what will be the result?

    <p>They are not equal.</p> Signup and view all the answers

    What will a * 2 output if a = ('a', 'b', 'c', 'd')?

    <p>('a', 'b', 'c', 'd', 'a', 'b', 'c', 'd')</p> Signup and view all the answers

    Which data structure is mutable?

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

    Which method can be used to access the second element in a tuple?

    <p>a[1]</p> Signup and view all the answers

    Study Notes

    Python Data Structures

    • Python offers various data structures, including strings, ranges, tuples, lists, sets, and dictionaries.
    • Tuples: Ordered, immutable sequences of elements.
    • Elements within a tuple are enclosed in parentheses (), and multiple elements are separated by commas.
    • Accessing elements is similar to strings: using index.
      • Example: my_tuple = (1, 2, 3)
      • Accessing the second element: my_tuple[1]
    • Tuples are immutable; their elements cannot be changed after creation.
    • To create a 1-element tuple, include a trailing comma. For example, (5,) is a tuple, while just (5) is an integer.
    • Lists: Ordered, mutable sequences.
    • Elements are enclosed in square brackets [] and elements are separated by commas.
    • Lists can be modified (added, removed, changed) after creation—mutable nature.
    • Elements can be accessed by index.
    • Example: my_list = [1, 2, 3]
    • Accessing the second element: my_list[1]
    • Sets: Unordered collections of unique elements.
    • Elements are enclosed in curly braces {} separated by commas.
    • Sets do not allow duplicate elements.
    • Sets offer operations like union, intersection, and difference.
    • Example: my_set = {1, 2, 3}
    • Dictionaries: Unordered collections of key-value pairs.
    • Elements are enclosed in curly braces {}.
    • Each key-value pair is separated by a colon :.
    • Keys must be unique and immutable (strings, numbers, tuples).
    • Values can be any data type.
    • Example: my_dict = {"name": "Alice", "age": 30}
      • Accessing the age: my_dict["age"]

    Sequence Operations

    • Sequences (strings, lists, tuples, ranges) share common operations, like indexing, slicing, and concatenation.
    • Indexing: Accessing elements using their position. Negative indexing is possible, where -1 is the last element.
    • Slicing: Extracting a portion of the sequence.
    • Concatenation: Joining two or more sequences.
    • Repetition: Creating a new sequence by repeating the original sequence.
    • Length: Determining the number of elements in a sequence (using len()).
    • Membership testing: Checking if an element exists in a sequence (in operator).

    List Methods

    • append(): adds an element to the end of a list.
    • insert(): inserts an element at a specific index.
    • remove(): removes the first occurrence of an element.
    • index(): returns the index of the first occurrence of a value.
    • pop(): removes and returns an element (by default, the last one).
    • pop(index): removes and returns element at the specified index.
    • reverse(): reverses the order of elements in a list.
    • sort(): sorts the elements in a list in ascending order (modify the list in place).

    String Methods

    • count(): obtains the number of occurrences of a substring.
    • find(): return the index of the first occurrence.
    • rfind(): return the index of the last occurrence.
    • lower(): converts the string to lowercase.
    • replace(): replaces part of the string.
    • rstrip(): removes trailing whitespace characters.
    • split(): splits the string into a list of substrings using a delimiter (by default, whitespace)

    Escape Characters

    • Escape characters (\), used within strings, control special characters like new lines (\n), tabs (\t), and quotes (\', \").

    Dictionary Methods

    • len(): returns the number of key-value pairs.
    • keys(): returns a view object containing the dictionary's keys.
    • values(): returns a view object containing the dictionary's values.
    • items(): returns a view object containing the dictionary's key-value pairs.
    • update(): merges another dictionary's key-value pairs with the current dictionary.
    • del: removes a key-value pair.
    • Check existence of Key- key in dictionary.

    List Comprehension

    • Concise way to create new lists.

    Dictionary Comprehension

    • Concise way to create new dictionaries.

    Studying That Suits You

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

    Quiz Team

    Related Documents

    Description

    Test your understanding of Python list operations with this engaging quiz. Questions cover basic list slicing, length determination, membership checks, concatenation, and repetition. Perfect for beginners looking to solidify their Python skills!

    More Like This

    Python List Operations Quiz
    20 questions
    Python List Operations Quiz
    10 questions
    Python List, Tuple, and Set Operations
    24 questions
    Python List Operations Quiz
    47 questions

    Python List Operations Quiz

    PleasurableNewton3147 avatar
    PleasurableNewton3147
    Use Quizgecko on...
    Browser
    Browser