Python Data Structures - Tuples, Lists, Sets

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

What is the result of the operation a[1:4] if a = [1, 2, 3, 4, 5]?

  • [3, 4, 5]
  • [2, 3, 4, 5]
  • [2, 3, 4] (correct)
  • [1, 2, 3]

Which of the following operations would create a new sequence that consists of the elements of b repeated three times if b = ['a', 'b', 'c', 'd']?

  • 3 * b (correct)
  • b.repeat(3)
  • b * 3 (correct)
  • b + b + b

What will the expression len(a + b) return if a = [1, 2, 3, 4, 5] and b = ['a', 'b', 'c', 'd']?

  • 8 (correct)
  • 10
  • 5
  • 9

If 6 in a evaluates to False, which of the following statements is accurate?

<p>The element 6 does not exist in sequence a. (A)</p> Signup and view all the answers

What does the expression a[2:5:2] yield when a = [1, 2, 3, 4, 5]?

<p>[3, 5] (D)</p> Signup and view all the answers

What character is used in Python to create a new line within a string?

<p>\n (A)</p> Signup and view all the answers

Which of the following statements about Python sets is true?

<p>Sets require all elements to be hashable. (B)</p> Signup and view all the answers

How do you insert a double quote within a string in Python?

<p>By using &quot; (D)</p> Signup and view all the answers

Which of the following is the correct way to initialize an empty set in Python?

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

What will be the output of the following code: print("Hello\nWorld")?

<p>Hello World (B)</p> Signup and view all the answers

What is the purpose of the escape character \t in a string?

<p>It adds a tab space. (A)</p> Signup and view all the answers

After executing a = {1, 3.14, 'Some String'}; a.remove(3.14), what will the set 'a' contain?

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

What is the output of the code snippet: print("Here is a backslash character: ")?

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

What indicates that a tuple is being created with only one element?

<p>Adding a comma after the element (C)</p> Signup and view all the answers

What will be the output of the operation 'a + b' when a = ('a', 'b', 'c', 'd') and b = (1, 2, 3)?

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

Which of the following statements is true about tuples?

<p>Tuples are immutable once created. (C)</p> Signup and view all the answers

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

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

How do negative indices function in Python?

<p>They access elements from the end of a sequence. (A)</p> Signup and view all the answers

What will happen if you attempt to assign a new value to an element of a tuple?

<p>An error will be thrown. (C)</p> Signup and view all the answers

In Python, which of the following best describes a list?

<p>A mutable sequence type. (A)</p> Signup and view all the answers

Which of these is a valid way to define an empty tuple?

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

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

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

What will 'print(a == (1, 2))' return if a = (1, 2, 3, 4)?

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

What will be the result of the following expression: a[-1] where a = [1, 2, 3, 4, 5]?

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

Which of the following correctly demonstrates a mutable object assignment?

<p>a = [1, 2, 3]; b = a; a[0] = 10 (D)</p> Signup and view all the answers

What will the output of the following code segment be? l = [1, 2, 3]; l.append(0); l.insert(1, 5); print(l)

<p>[1, 5, 2, 3, 0] (A)</p> Signup and view all the answers

How can you create a list of odd numbers between 1 and 100 using list comprehension?

<p>[i for i in range(1, 100) if i % 2 != 0] (A)</p> Signup and view all the answers

Which of the following methods would remove the last element from the list?

<p>l.pop() (C)</p> Signup and view all the answers

What will the output be after executing the code: a = [1, 2, 3]; a.reverse(); print(a)?

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

Which of the following is NOT a method provided for list manipulation?

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

Given the list a = ['a', 'b', 'c'], what will print(a[:-1]) yield?

<p>['a', 'b'] (C)</p> Signup and view all the answers

What will be the value of b after executing the following code? a = [1, 2, 3]; b = a.copy(); a[0] = 10; print(b)

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

Which syntax correctly removes an element at a specified index from a list?

<p>list.pop(index) (D)</p> Signup and view all the answers

What will the output be for the condition 'if (2,3,4) in (1,2,3,4,5)'?

<p>(2,3,4) is not in (1,2,3,4,5) (B)</p> Signup and view all the answers

Which string method returns the index of the first occurrence of a substring?

<p>s1.find(s2) (C)</p> Signup and view all the answers

What does the slice operation s1.rstrip() accomplish?

<p>Removes whitespace from the end of the string (B)</p> Signup and view all the answers

What is the result of applying the function apply_operation(square, [1,2,3])?

<p>1, 4, 9 (C)</p> Signup and view all the answers

When using the in operator with strings, which of the following is true?

<p>It checks if a substring is contained in a longer string. (A)</p> Signup and view all the answers

What will be the output of the following code: print('These are "double quotes" within a single quote string.')?

<p>These are double quotes within a single quote string. (A)</p> Signup and view all the answers

Which of the following correctly uses the map function to square a list of numbers?

<p>list(map(square, [1,2,3])) (C)</p> Signup and view all the answers

What is returned by s1.count(s2)?

<p>The total number of occurrences of s2 in s1 (A)</p> Signup and view all the answers

Which operation is not valid for manipulating strings?

<p>s1.append(s2) (D)</p> Signup and view all the answers

If the string s is 'John Smith ', what will print(s.split(' ')) return?

<p>['John', 'Smith', ''] (D)</p> Signup and view all the answers

Flashcards

String

A sequence of characters enclosed in single or double quotes. Example: 'Hello' or "World".

List

A sequence of objects. It's like a tuple, but you can change the elements within it.

Tuple

A sequence of objects that cannot be changed after creation. Example: (1, 2, 3, 4).

Range

A sequence of numbers. Example: 'range(5)' generates 0, 1, 2, 3, 4.

Signup and view all the flashcards

Set

A data structure that holds unique elements. Example: {1, 2, 3}.

Signup and view all the flashcards

Dictionary

A data structure that maps keys to values. Example: {'name': 'Alice', 'age': 30}.

Signup and view all the flashcards

Mutability

Changing the elements of a data structure after it's created. Lists are mutable, tuples are not.

Signup and view all the flashcards

Indexing

Accessing elements in a sequence using their position (index). For example, a[0] retrieves the first element.

Signup and view all the flashcards

Slicing

Extracting a portion of a sequence using start and end indices. For example, a[1:3] retrieves elements from index 1 to 2.

Signup and view all the flashcards

Iteration

Iterating over each element of a sequence one by one. For example, 'for i in a:' will process each element 'i' in the sequence 'a'.

Signup and view all the flashcards

Negative Indexing in Python Lists

Negative indices in Python lists allow access to elements from the end of the list. -1 refers to the last element, -2 to the second last, and so on.

Signup and view all the flashcards

Slicing with Negative Steps

Slicing with negative steps in Python lists allows you to traverse the list in reverse order. The step value determines the interval of elements to be included in the slice.

Signup and view all the flashcards

Assignment by Reference

In Python, assignment using the '=' operator creates references to objects. Changes made to a referenced object are reflected in both the original and the assigned variables.

Signup and view all the flashcards

Avoiding Mutable Object Assignment Issues

Mutable objects (like lists) assigned by reference can lead to unexpected behavior when modifications are made through either reference. Using the copy() method creates a new, independent copy, preventing unintended changes.

Signup and view all the flashcards

List append() Method

The append() method adds an element to the end of a list.

Signup and view all the flashcards

List insert() Method

The insert() method inserts an element at a specific index in a list.

Signup and view all the flashcards

List remove() Method

The remove() method removes the first occurrence of a given element from a list.

Signup and view all the flashcards

List index() Method

The index() method returns the index of the first occurrence of a specified element within a list.

Signup and view all the flashcards

List pop() Method

The pop() method removes and returns an element from a list, by default the last element. Optionally, an index can be provided to remove a specific element.

Signup and view all the flashcards

List reverse() Method

The reverse() method reverses the order of elements in a list.

Signup and view all the flashcards

seq[i]

Retrieves the element at the specified index position within a sequence. Example: my_list[2] retrieves the element at index 2, which is the third element.

Signup and view all the flashcards

seq1 + seq2

Creates a new sequence by combining two existing sequences in order. Example: [1, 2, 3] + ['a', 'b'] becomes [1, 2, 3, 'a', 'b'].

Signup and view all the flashcards

seq[start:end:step]

Returns a slice of a sequence, containing elements from the start index (inclusive) up to but not including the end index. Optionally, a step can be used to select every nth element.

Signup and view all the flashcards

a in seq

Checks if a specific element exists within a sequence. Returns True if found, False otherwise. Example: 3 in [1, 2, 3] returns True.

Signup and view all the flashcards

n * seq

Creates a new sequence by repeating the original sequence a specific number of times. Example: 3 * [1, 2] becomes [1, 2, 1, 2, 1, 2].

Signup and view all the flashcards

What is a String?

A sequence of characters enclosed in single or double quotes.

Signup and view all the flashcards

How to use In Operator?

Check if a substring is present within a longer string.

Signup and view all the flashcards

What is a Range

A sequence of numbers starting from 0 and ending before the specified number.

Signup and view all the flashcards

What is Iteration?

Iterating through each element in a sequence one by one.

Signup and view all the flashcards

What is Mapping?

Applies the same operation to each element in a sequence.

Signup and view all the flashcards

String Method: find ()

Returns the index of the first occurrence of a substring.

Signup and view all the flashcards

String Method: count ()

Returns the number of times a substring appears within a string.

Signup and view all the flashcards

String Method: replace ()

Replaces all instances of a given substring with a new substring.

Signup and view all the flashcards

String Method: rstrip ()

Removes trailing whitespace characters from a string.

Signup and view all the flashcards

String Method: split ()

Splits a string into a list of substrings based on a separator.

Signup and view all the flashcards

Escape Character

A special character used within strings to represent specific actions. These characters are preceded by a backslash (), for example, \n for a new line.

Signup and view all the flashcards

add()

Adds an element to the set. Example: my_set.add(5)

Signup and view all the flashcards

remove()

Removes an element from the set. Example: my_set.remove(3)

Signup and view all the flashcards

Study Notes

Python Data Structures - Tuples

  • Tuples are ordered, immutable sequences of items
  • Items can be any data type (e.g., numbers, strings, other tuples)
  • Defined using parentheses () and commas to separate items
  • Creating a tuple with one element requires a comma after the element. e.g. (1,)
  • Tuples are immutable; you cannot change elements after creation. Attempting to modify a tuple will result in a TypeError
  • Accessed using index positions (starting at 0)
  • Similar to strings, you can iterate over, slice or index them

Python Data Structures - Lists

  • Lists are ordered, mutable sequences of items
  • Items can be any data type (e.g., numbers, strings, other lists)
  • Defined using square brackets [] and commas to separate items
  • Lists are mutable, meaning you can change elements after they are created
  • Accessed using index positions (starting at 0)
  • Can be iterated over, sliced, and indexed in a similar way to tuples

Python Data Structures - Sets

  • Sets are unordered collections of unique elements
  • Items can be any hashable data type (numbers, strings, tuples, but not lists)
  • Defined using curly braces {} and commas to separate items
  • Sets do not allow duplicate elements.
  • Elements aren't accessible by index; you must iterate over them
  • Sets support various operations like union, intersection, difference, etc.

Python Data Structures - Dictionaries

  • Dictionaries are unordered collections of key-value pairs
  • Each key is unique and immutable (like keys in a real-world dictionary)
  • Values can be any type
  • Defined using curly braces {} with key-value pairs separated by colons and items separated by commas
  • Accessed using keys, rather than indexes
  • Mutability: Keys and values in a dictionary are mutable, allowing you to change the values associated with keys

Studying That Suits You

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

Quiz Team

Related Documents

More Like This

Python Data Structures Quiz
8 questions

Python Data Structures Quiz

LuxuriousAllusion8938 avatar
LuxuriousAllusion8938
Python-kurs, kapittel 7: Lister og tupler
43 questions
Use Quizgecko on...
Browser
Browser