Python Programming Question Bank
Python Programming Question Bank
Mutable objects in Python, such as lists and dictionaries, can be changed after creation, allowing for dynamic data structures where elements can be added, removed, or altered. They are typically used in cases where collections need to be modified, like appending data to a list. Immutable objects, such as strings and tuples, cannot be altered once defined, ideal for maintaining integrity where data should remain unchanged. This immutability is useful in scenarios demanding hash keys or environments where thread safety is a concern .
Indentation in Python is crucial because it is used to define the scope of loops, functions, classes, and other blocks of code. Unlike many other programming languages that use braces or keywords, Python uses indentation to signal a block of code. Correct indentation is essential because Python will raise an IndentationError if the code is not indented consistently. This requirement for indentation promotes readability and reduces the likelihood of errors associated with block definition .
The eval() function in Python parses the expression passed to it and evaluates it as a Python expression. It is often used to dynamically evaluate Python expressions from user input or strings at runtime. For example, eval('3 + 4') would return 7. However, using eval() can pose significant security risks if the input is not properly sanitized, as it can execute arbitrary code. Thus, it should be used cautiously .
Identifiers in Python are names used to identify variables, functions, classes, modules, or other objects. The rules for naming an identifier are: it must start with a letter (A-Z or a-z) or an underscore (_), followed by letters, digits, or underscores. Python identifiers are case-sensitive, meaning 'Var' and 'var' would be considered distinct identifiers. Keywords are not allowed as identifiers .
Membership operators 'in' and 'not in' are used to test for membership in sequences such as strings, lists, or tuples. The 'in' operator returns True if the specified element is found in the sequence. For example, 'x in [1, 2, 3]' returns True if x is one of the elements in the list. Conversely, 'not in' returns True if the specified element is not present in the sequence, e.g., 'x not in [1, 2, 3]' returns True if x is not in the list .
Relational operators in Python are used to compare values, producing boolean outcomes. They include greater than (>), less than (<), equal to (==), not equal to (!=), greater than or equal to (>=), and less than or equal to (<=). Practical applications involve decision-making within code, such as conditionals and loops, where different outcomes depend on comparisons of variables or expressions, such as filtering data in a list based on a threshold .
Lists in Python are mutable, meaning their contents can be changed after creation, such as adding, removing, or updating elements. Tuples are immutable; once created, their contents cannot be altered. This makes tuples suitable for scenarios where fixed and read-only data is required. Dictionaries, meanwhile, are mutable and are composed of key-value pairs. Keys must be immutable types, like strings or numbers. Dictionaries are ideal for fast lookups based on keys rather than positions .
Operator precedence in Python dictates the order in which operations are performed in expressions, with certain operators binding more tightly and getting executed before others. For example, multiplication (*) and division (/) have higher precedence than addition (+) and subtraction (-), meaning in the expression '3 + 4 * 5', the multiplication is performed first, resulting in '3 + 20 = 23'. Understanding operator precedence is crucial to writing correct and expected expressions, especially in complex statements without explicit parentheses .
Lambda functions in Python are anonymous functions defined using the lambda keyword. They can have any number of input parameters, but only one expression, which is evaluated and returned. Unlike regular functions defined with 'def', lambda functions are used for simple operations that are short-lived and require less formal structure. They are often used in scenarios requiring a function for a short-term purpose like filtering or sorting data using functions like sort or map. For example, using 'sorted(list, key=lambda x: x[1])' sorts a list of tuples based on the second element .
'Break' and 'continue' are loop control statements in Python that alter the flow of a loop. 'Break' immediately terminates the loop, transferring control to the first statement following the loop's end. Conversely, 'continue' skips the rest of the code inside the loop for the current iteration and resumes at the loop's next iteration. Both are useful in controlling loop execution paths, yet they differ in application as 'break' ends loop execution, while 'continue' only interrupts a single iteration .