Python General Flashcards

(95 cards)

1
Q

What is Python’s memory model primarily based on?

A

Reference counting and garbage collection.

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

True or False: Tuples are mutable in Python.

A

False.

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

What is the syntax to create a list in Python?

A

Use square brackets, e.g., my_list = [].

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

Fill in the blank: In Python, a ______ is an ordered collection of items.

A

list

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

What is the main difference between lists and tuples in Python?

A

Lists are mutable; tuples are immutable.

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

How do you define a string in Python?

A

Enclose characters in single or double quotes, e.g., ‘hello’ or “hello”.

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

What is a class in Python?

A

A blueprint for creating objects that encapsulate data and functionality.

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

True or False: Meta classes in Python are classes of classes.

A

True.

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

What keyword is used to define a function in Python?

A

def

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

What does the ‘len()’ function do?

A

Returns the number of items in an object.

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

What will be the output of ‘str(123)’?

A

‘123’

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

Which method is used to add an item to the end of a list?

A

append()

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

Fill in the blank: A ______ is a collection of key-value pairs in Python.

A

dictionary

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

What is the purpose of the ‘self’ parameter in class methods?

A

To refer to the instance of the class.

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

True or False: Strings in Python are mutable.

A

False.

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

What is the output of ‘5 in [1, 2, 3, 4, 5]’?

A

True.

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

How can you create a tuple with one item?

A

Use a comma, e.g., my_tuple = (1,).

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

What built-in function can convert a list to a tuple?

A

tuple()

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

What is the difference between ‘==’ and ‘is’ in Python?

A

’==’ checks for value equality; ‘is’ checks for object identity.

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

Fill in the blank: A ______ allows you to iterate over a sequence in Python.

A

for loop

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

What does the ‘pop()’ method do in list?

A

Removes and returns the last item from the list.

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

How do you define a meta class in Python?

A

By inheriting from type.

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

True or False: You can concatenate two strings using the ‘+’ operator.

A

True.

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

What is the primary use of the ‘with’ statement?

A

To wrap the execution of a block with methods defined by a context manager.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
What does the 'join()' method do for strings?
Concatenates a list of strings into a single string.
26
Fill in the blank: The ______ method returns a new string with all occurrences of a substring replaced.
replace()
27
How can you create a shallow copy of a list?
Using the list() constructor or slicing.
28
What method can be used to sort a list in place?
sort()
29
What is the output of 'type([])'?
30
True or False: A list can contain items of different data types.
True.
31
What is the method to convert a string to lowercase?
lower()
32
Fill in the blank: The ______ method splits a string into a list.
split()
33
What is the role of a constructor in a Python class?
To initialize an object's attributes.
34
Which operator is used for list slicing?
[start:end]
35
What does the 'extend()' method do for lists?
Adds elements from an iterable to the end of the list.
36
What are the two main types of classes in Python?
Standard classes and meta classes.
37
True or False: The 'id()' function returns the memory address of an object.
True.
38
What is a class variable in Python?
A variable that is shared among all instances of a class.
39
What does the 'map()' function do?
Applies a function to all items in an iterable.
40
Fill in the blank: The ______ method removes the first occurrence of a value from a list.
remove()
41
What is a lambda function in Python?
A small anonymous function defined with the 'lambda' keyword.
42
How do you create a set in Python?
Use curly braces or the set() constructor, e.g., my_set = {1, 2, 3}.
43
What does 'list comprehension' allow you to do?
Create a new list by applying an expression to each item in an iterable.
44
True or False: Python uses zero-based indexing.
True.
45
What is the output of 'len("Hello World")'?
11
46
What is the purpose of the __init__ method in a class?
To initialize a new instance of the class.
47
Fill in the blank: The ______ method is used to create a new instance of a class.
__new__
48
What built-in function can be used to check if a key exists in a dictionary?
in
49
What is the output of '"hello".capitalize()'?
'Hello'
50
How can you delete an item from a dictionary?
Using the 'del' statement or the 'pop()' method.
51
True or False: An empty list is considered False in a boolean context.
True.
52
What does the 'any()' function do?
Returns True if any element of the iterable is True.
53
Fill in the blank: The ______ method returns a list of the dictionary's keys.
keys()
54
What is a generator in Python?
A function that returns an iterator using 'yield' instead of 'return'.
55
What does the 'filter()' function do?
Creates an iterator from elements of an iterable for which a function returns True.
56
True or False: Python's string methods modify the original string.
False.
57
What is the output of '"Hello".find("e")'?
1
58
How do you check the data type of a variable in Python?
Using the 'type()' function.
59
What does the 'sorted()' function do?
Returns a new sorted list from the elements of any iterable.
60
Fill in the blank: The ______ method returns the last item of a list without removing it.
[-1]
61
What is the purpose of the 'pass' statement in Python?
To create a placeholder for future code.
62
What is a docstring?
A string literal that documents a module, class, method, or function.
63
True or False: The 'break' statement is used to exit a loop.
True.
64
What does the 'enumerate()' function do?
Returns an iterator that produces pairs of index and value from an iterable.
65
Fill in the blank: The ______ method returns a list of the dictionary's values.
values()
66
What is the purpose of the 'return' statement in a function?
To exit the function and optionally pass back an expression to the caller.
67
How do you create a multi-line string in Python?
By using triple quotes, either ''' or """.
68
What does the 'slice' operator do?
Extracts a portion of a sequence.
69
What is the output of '"abc".upper()'?
'ABC'
70
What is the difference between a list and a set in Python?
Lists are ordered and allow duplicates; sets are unordered and do not allow duplicates.
71
What is the output of '"Hello, World!".split(" ")'?
['Hello,', 'World!']
72
True or False: A class can inherit from multiple classes in Python.
True.
73
What does the 'zip()' function do?
Combines iterables into tuples based on their index.
74
Fill in the blank: A ______ is a special method in Python that starts and ends with double underscores.
dunder method
75
What is the purpose of the 'super()' function?
To call a method from a parent class.
76
What is the output of '"abc".replace("a", "A")'?
'Abc'
77
True or False: Python supports method overloading.
False.
78
What is the output of 'list(range(5))'?
[0, 1, 2, 3, 4]
79
What is the purpose of the 'isinstance()' function?
To check if an object is an instance of a class or a subclass thereof.
80
Fill in the blank: The ______ method is used to convert a dictionary into a list of tuples.
items()
81
What does the 'sort()' method do in place?
Sorts the elements of a list in ascending order.
82
What is the output of '"Hello".endswith("o")'?
True.
83
How do you handle exceptions in Python?
Using try-except blocks.
84
What is the output of '"Hello".count("l")'?
2
85
True or False: The 'continue' statement is used to skip the current iteration of a loop.
True.
86
What does the 'list()' function do?
Converts an iterable into a list.
87
What is the syntax to create a dictionary in Python?
Use curly braces, e.g., my_dict = {}.
88
Fill in the blank: The ______ method is used to remove all items from a list.
clear()
89
What is the output of '"abc".find("z")'?
-1
90
What does the 'reversed()' function do?
Returns a reverse iterator of a sequence.
91
What is the purpose of the 'assert' statement?
To test if a condition is true; raises an AssertionError if false.
92
True or False: You can use the 'in' operator to check for membership in a string.
True.
93
What is the output of '"Hello World".split()'?
['Hello', 'World']
94
What is the primary purpose of a meta class?
To control the creation and behavior of classes.
95
What does the 'callable()' function check?
If an object appears to be callable (like a function).