Module 2 Summary: Python Data Structures Flashcards

(112 cards)

1
Q

What is a tuple in Python?

A

An ordered and immutable collection of elements used to group related data together.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How are tuples written in Python?

A

As comma-separated elements enclosed in parentheses ().

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What types of data can tuples contain?

A

Tuples can include strings, integers, floats, or even other tuples.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you access elements in a tuple?

A

By using positive or negative indexing.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What operations can you perform on tuples?

A

Combining, concatenating, and slicing.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Why are tuples considered immutable?

A

They cannot be changed after creation; to modify them you must create a new tuple.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is tuple nesting?

A

When a tuple contains other tuples or complex data types.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you access elements in a nested tuple?

A

By using multiple levels of indexing (e.g., t[1][0]).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is a list in Python?

A

An ordered and mutable collection of items that can hold different data types.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How are lists written in Python?

A

As comma-separated elements enclosed in square brackets [].

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What property makes lists similar to tuples?

A

Both are ordered sequences of elements.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What makes lists different from tuples?

A

Lists are mutable, while tuples are immutable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What types of data can a list contain?

A

Strings, integers, floats, and even other lists (nested lists).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do you access elements in a list?

A

By using positive or negative indexing.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What happens when you concatenate or append a list?

A

The same list is modified, not a new one created.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What operations can you perform on lists?

A

Adding, deleting, splitting, concatenating, and slicing.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What separates elements in a list?

A

Commas (delimiters).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is aliasing in lists?

A

When multiple names refer to the same list object in memory.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

How do you clone a list?

A

By creating a copy (e.g., list_copy = original_list[:]) to avoid aliasing.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What is a dictionary in Python?

A

A collection of key-value pairs for flexible data storage and retrieval based on unique keys.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

How are dictionaries written in Python?

A

Using curly brackets {} with key-value pairs separated by colons.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What are the rules for dictionary keys?

A

Keys must be immutable and unique.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Can dictionary values be duplicated?

A

Yes, values may be duplicated and can be mutable or immutable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

How do you separate key-value pairs in a dictionary?

A

By commas between each pair.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
How do you assign a dictionary to a variable?
By storing it in a variable name (e.g., my_dict = {'key':'value'}).
26
How do you retrieve a value from a dictionary?
Use the key as an argument (e.g., my_dict['key']).
27
Can you add or delete elements in a dictionary?
Yes, dictionaries support addition and deletion of key-value pairs.
28
How can you check if a key exists in a dictionary?
Use the 'in' operator, which returns True or False.
29
Which methods retrieve all keys or values from a dictionary?
The .keys() and .values() methods.
30
What is a set in Python?
An unordered collection of unique elements used for operations like removing duplicates and unions/intersections.
31
How are sets written in Python?
Using curly brackets {}.
32
Can sets contain duplicate elements?
No, sets automatically remove duplicates.
33
How can you create a set from a list?
Pass the list through the set() function.
34
What are common set operations?
Adding, removing, and verifying elements.
35
How do you find common elements between sets?
Use the ampersand (&) operator for intersection.
36
How do you combine two sets including all unique elements?
Use the union() method.
37
What does the subset method do in sets?
Checks whether one set is a subset of another, returning True or False.
38
What is a dictionary in Python?
A built-in data type representing a collection of key-value pairs enclosed in curly braces {}.
39
How do you create an empty dictionary?
dict_name = {}
40
Give an example of a dictionary with data.
person = { 'name': 'John', 'age': 30, 'city': 'New York' }
41
How do you access a value in a dictionary?
Use its key: value = dict_name['key_name']
42
Give an example of accessing a dictionary value.
name = person['name']
43
How do you add or modify an item in a dictionary?
dict_name[key] = value
44
What happens if the key already exists when assigning a new value?
The existing value is updated.
45
Give an example of adding a new key-value pair.
person['Country'] = 'USA'
46
Give an example of updating a value for an existing key.
person['city'] = 'Chicago'
47
How do you delete a key-value pair?
Use the del statement: del dict_name[key]
48
What happens if you use del on a non-existent key?
It raises a KeyError.
49
How do you merge one dictionary into another?
Use the update() method.
50
Example of the update() method.
person.update({'Profession': 'Doctor'})
51
How do you remove all items from a dictionary but keep it accessible?
Use clear(): dict_name.clear()
52
How can you check if a key exists in a dictionary?
Use the 'in' keyword, e.g., if 'name' in person:
53
What does the copy() method do in dictionaries?
Creates a shallow copy; the new dictionary is a distinct object.
54
Two ways to copy a dictionary.
new_person = person.copy() or new_person = dict(person)
55
How do you retrieve all dictionary keys as a list?
keys_list = list(dict_name.keys())
56
How do you retrieve all dictionary values as a list?
values_list = list(dict_name.values())
57
How do you get both keys and values from a dictionary?
items_list = list(dict_name.items())
58
What is a set in Python?
An unordered collection of unique elements enclosed in curly braces {}.
59
How do you create an empty set?
empty_set = set()
60
Give an example of defining a set.
fruits = {'apple', 'banana', 'orange'}
61
What happens when you add a duplicate element to a set?
Duplicates are ignored; sets only store unique values.
62
How do you add an element to a set?
set_name.add(element)
63
Example of using add().
fruits.add('mango')
64
How do you remove all elements from a set?
set_name.clear()
65
How do you copy a set?
new_set = set_name.copy()
66
What is the discard() method used for?
Removes a specific element but does nothing if the element is not found.
67
What is the remove() method used for?
Removes a specific element; raises KeyError if not found.
68
What is the pop() method in sets?
Removes and returns an arbitrary element; raises KeyError if the set is empty.
69
How do you check if one set is a subset of another?
set1.issubset(set2)
70
How do you check if one set is a superset of another?
set1.issuperset(set2)
71
What does union() do for sets?
Returns a new set containing all unique elements from both sets.
72
What does intersection() do for sets?
Returns a new set containing only common elements from both sets.
73
What does difference() do for sets?
Returns elements present in one set but not the other.
74
What does symmetric_difference() do?
Returns elements that are in either set but not in both.
75
How do you combine two sets while keeping unique elements?
Use the union() method or the update() method.
76
What does update() do in sets?
Adds elements from another iterable while keeping only unique values.
77
What does the append() method do when adding a list to another list?
It adds the entire list as a single nested element to the end of the list, not individual elements.
78
Example: What is the result of ['shirt','pants'].append(['hat','belt'])?
['shirt','pants',['hat','belt']] — The new list is nested as one element.
79
How can you add multiple elements from one list to another without nesting?
Use extend(): list1.extend(list2) adds each element individually.
80
What happens when you assign one list variable to another using '='?
Both variables reference the same list object in memory (aliasing). Changes to one affect the other.
81
How do you make an independent copy of a list?
Use slicing or copy(): list_copy = original[:] or list_copy = original.copy().
82
Why is slicing ([:]) preferred when cloning lists?
It creates a shallow copy without linking to the original list’s memory reference.
83
What will ('Python','Data','Science')[1:3] return?
('Data','Science') — slicing starts at index 1 and stops before index 3.
84
Why does ('Python','Data','Science')[1:3] return ('Data','Science')?
Because slicing includes the start index but excludes the stop index.
85
How do you add or update entries in a Python dictionary?
Use square bracket notation: dict['key'] = value.
86
Why can’t you use add() to insert into a dictionary?
Dictionaries don’t have an add() method. You assign key-value pairs directly.
87
Example: Add a key-value pair to a dictionary named movie.
movie['Year'] = 2010 — Adds a new key and value.
88
How do you check if an element exists in a Python set?
Use the 'in' keyword: 'B456' in auth_codes.
89
Why is 'B456' in auth_codes preferred over .find() or .contains()?
Because sets don’t support .find() or .contains(); 'in' is the correct membership operator.
90
What does the add() method do in sets?
It inserts a new unique element into the set if it doesn’t already exist.
91
Aliasing
Aliasing refers to giving another name to a function or a variable.
92
Ampersand
A character typically "&" standing for the word "and."
93
Compound elements
Compound statements contain (groups of) other statements; they affect or control the execution of those other statements in some way.
94
Delimiter
A delimiter in Python is a character or sequence of characters used to separate or mark the boundaries between elements or fields within a larger data structure, such as a string or a file.
95
Dictionaries
A dictionary in Python is a data structure that stores a collection of key-value pairs, where each key is unique and associated with a specific value.
96
Function
A function is a block of code, defining a set procedure, which is executed only when it is called.
97
Immutable
Immutable Objects are of in-built datatypes like int, float, bool, string, Unicode, and tuple. In simple words, an immutable object can't be changed after it is created.
98
Intersection
The intersection of two sets is a new set containing only the elements that are present in both sets.
99
Keys
The keys () method in Python Dictionary, returns a view object that displays a list of all the keys in the dictionary in order of insertion using Python.
100
Lists
A list is any list of data items, separated by commas, inside square brackets.
101
Logic operations
In Python, logic operations refer to the use of logical operators such as "and," "or," and "not" to perform logical operations on Boolean values (True or False).
102
Mutable
Mutable objects in Python are objects whose values can be changed after they are created. These objects allow modifications such as adding, removing, or altering elements without creating a new object.
103
Nesting
A nested function is simply a function within another function and is sometimes called an "inner function".
104
Ratings in python
Ratings in Python typically refer to a numerical or qualitative measure assigned to something to indicate its quality, performance, or value.
105
Set operations
Set operations in Python refer to mathematical operations performed on sets, which are unordered collections of unique elements.
106
Sets in python
A set is an unordered collection of unique elements.
107
Syntax
The rules that define the structure of the language for python is called its syntax.
108
Tuples
These are used store multiple items in a single variable.
109
Type casting
In python, this is converting one data type to another.
110
Variables
In python, a variable is a symbolic name or identifier used to store and manipulate data. Variables serve as containers for values, and these values can be of various data types, including numbers, strings, lists, and more.
111
Venn diagram
A Venn diagram is a graphical representation that uses overlapping circles to illustrate the relationships and commonalities between sets or groups of items.
112
Versatile data
Versatile data, in a general context, refers to data that can be used in multiple ways, is adaptable to different applications or purposes, and is not restricted to a specific use case.