Python MCQ Revision Practice Sheet
Python MCQ Revision Practice Sheet
Key-value pairs in Python are stored in dictionaries. A dictionary is defined using curly braces {} with pairs separated by commas, e.g., Day = {1: 'Monday', 2: 'Tuesday'}. Retrieval is done using keys, allowing quick access to values, like 'Day[1]' which gives 'Monday'. This method is efficient for data lookup and is more intuitive when the relationship between values and keys is meaningful, as it provides constant time complexity for access, insertions, and deletions .
Python treats identifiers based on its lexical rules where an identifier must start with a letter (A-Z or a-z) or an underscore (_), followed by letters, numbers, or underscores. For example, '2hundred' is invalid as it starts with a digit, while '_main' or 'hello_rsp1' are valid identifiers. This impacts how developers assign meaningful variable names while adhering to these rules to maintain code readability and prevent syntax errors .
List slicing in Python allows you to extract parts of a list by specifying a range of indices. The syntax for slicing is list[start:stop:step]. For example, with the list [5, 10, 15, 20, 25], the slice list[1:4] returns [10, 15, 20], which includes elements from index 1 to 3. Slicing provides flexibility in accessing subsets of list elements and supports advanced operations like reversing a list with list[::-1].
In Python, mutable types are those whose values can be changed after they are created, such as lists and dictionaries. Immutable types, like tuples and strings, cannot be changed once they are created. This distinction is crucial because it affects how objects are handled in memory. Immutable objects can be shared between different parts of a program without worrying that their values will change, whereas mutable objects require careful handling to prevent unintended modifications. This distinction impacts how you design your program and manage memory .
Storing data using mutable data types like lists in Python allows for dynamic data manipulation, such as appending, removing, or altering elements. This flexibility supports algorithms that require frequent data updates. However, it also necessitates careful handling to prevent unintended modifications, which can lead to bugs due to shared references. Proper program design involves using immutable data when possible to enhance predictability and safety, especially when data sharing between different functions or modules is involved .
The result of executing the code segment 'print("100+200")' will be the string '100+200'. The '+' symbol here is not treated as an arithmetic operator because it is within a string literal, which means the expression is not evaluated mathematically. This highlights the concept of string literals where all contained characters, including symbols that are typically operators, are treated as plain text .
Operator precedence in Python determines the order in which operations are performed in expressions. For example, the expression '3 + 5 * 2' will be evaluated as '3 + (5 * 2)' because the multiplication '*' operator has a higher precedence than the addition '+' operator. Understanding operator precedence is important to correctly predict and control the flow of complex expressions. Without proper understanding, expressions might not produce the desired results .
The expression '3 + 3.00' results in '6.0' because the integer '3' is implicitly converted to a float to match the type of '3.00'. The '3**3.0' expression results in '27.0', with the integer '3' being coerced into a float '3.0' for exponentiation. Type coercion ensures that operations between mixed types are feasible by converting them to a common type. This is essential for maintaining consistency and avoiding type errors during calculations involving mixed data types .
Tuple unpacking in Python allows multiple variables to be assigned values from a tuple simultaneously, enhancing code efficiency and readability by reducing boilerplate code. For instance, with 'x, y = (3, 4)', both 'x' and 'y' are assigned in a single line. This technique is particularly useful when returning multiple values from a function or when iterating with 'enumerate()' in loops, improving clarity and alignment with Pythonic practices .
The 'range()' function in Python generates a sequence of numbers and is commonly used in for-loops to iterate over a sequence. For example, 'for i in range(5)' generates numbers from 0 to 4, iterating five times. 'range()' can take up to three arguments: start, stop, and step, allowing for customized ranges. Using 'range()' is efficient for loops because it does not create a list in memory, but a range object that generates elements on demand .