1/31
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
What is a tuple in Python?
A fixed-length, immutable sequence of Python objects.
How do you create a tuple?
Using a comma-separated sequence of values, e.g., tup = 4, 5, 6 or tup = (4, 5, 6).
How do you access elements in a tuple?
Using square brackets [] and the index, e.g., tup[0].
Can you modify a tuple after creation?
No, tuples are immutable, but you can modify mutable objects inside a tuple (e.g., lists).
How do you concatenate tuples?
Using the + operator, e.g., (1, 2) + (3, 4) results in (1, 2, 3, 4).
What is tuple unpacking?
Assigning the elements of a tuple to variables, e.g., a, b, c = (1, 2, 3).
What method counts occurrences of a value in a tuple?
The count method, e.g., tup.count(2).
What is a list in Python?
A variable-length, mutable sequence of Python objects.
How do you create a list?
Using square brackets [] or the list() function, e.g., a_list = [2, 3, 7] or b_list = list((1, 2, 3)).
How do you add an element to the end of a list?
Using the append method, e.g., a_list.append(4).
How do you insert an element at a specific position in a list?
Using the insert method, e.g., a_list.insert(1, 'red').
How do you remove an element from a list by value?
Using the remove method, e.g., a_list.remove('red').
How do you remove and return an element from a list by index?
Using the pop method, e.g., a_list.pop(2).
How do you sort a list in-place?
Using the sort method, e.g., a_list.sort().
How do you create a sorted copy of a list?
Using the sorted function, e.g., sorted(a_list).
What is a dictionary in Python?
A flexibly sized collection of key-value pairs, also known as a hash map or associative array.
How do you create a dictionary?
Using curly braces {} and colons, e.g., d1 = {'a': 'some value', 'b': [1, 2, 3]}.
How do you access a value in a dictionary?
Using square brackets [] and the key, e.g., d1['a'].
How do you add or update a key-value pair in a dictionary?
By assignment, e.g., d1['new_key'] = 'new_value'.
How do you remove a key-value pair from a dictionary?
Using the del keyword or the pop method, e.g., del d1['a'] or d1.pop('a').
What method returns all keys in a dictionary?
The keys method, e.g., d1.keys().
What method returns all values in a dictionary?
The values method, e.g., d1.values().
What is a set in Python?
An unordered collection of unique elements.
How do you create a set?
Using the set() function or curly braces {}, e.g., set([2, 2, 2, 1, 3]) or {2, 2, 2, 1, 3}.
How do you add an element to a set?
Using the add method, e.g., a_set.add(4).
How do you remove an element from a set?
Using the remove method, e.g., a_set.remove(4).
What is a list comprehension?
A concise way to create a new list by filtering and transforming elements of a collection, e.g., [x.upper() for x in strings if len(x) > 2].
What is a generator in Python?
A function that yields a sequence of values lazily, pausing after each one until the next is requested, using the yield keyword.
How do you open a file for reading in Python?
Using the open function with mode 'r', e.g., f = open('file.txt', 'r').
How do you read all lines from a file into a list?
Using f.readlines() or a list comprehension, e.g., [x.rstrip() for x in open('file.txt')].
How do you write to a file in Python?
Using the write or writelines methods, e.g., f.write('text') or f.writelines(list_of_strings).
What is the with statement used for with files?
To ensure a file is automatically closed after its block of code executes, e.g., with open('file.txt') as f: ....