Python Test Questions and Exercises
Python Test Questions and Exercises
Applying the method `str.strip(' P')` to the string ' Python tutorial' trims the leading character ' ' and the leading 'P' from the string, resulting in 'ython tutorial'. This illustrates the use of the `strip` method to remove specific characters from the beginning and end of a string in Python .
The `filter` function in Python is used to construct a list of elements from an iterable for which a specified function returns true. Given `numbers = [1, 2, 3, 4, 5, 6]`, `filter(lambda x: x % 2, numbers)` filters out even numbers since the lambda function checks for odd numbers (`x % 2` is true for odds), returning `[1, 3, 5]`. This approach effectively applies functional programming principles by utilizing anonymous functions as predicates .
To slice a list or a string in Python, you can use the `slice()` function. The `slice(0, 4)` indicates that the elements from the 0th index up to, but not including, the 4th index should be extracted. For a list `b = [1, 3, 4, 6, 7, 10]`, `b[slice(0, 4)]` results in `[1, 3, 4, 6]`. For a string `st = 'Python tutorial'`, `st[slice(0, 4)]` results in 'Pyth' .
`*args` and `**kwargs` in Python are used to pass a variable number of arguments to functions. `*args` allows passing a non-keyworded, variable number of arguments as a tuple, while `**kwargs` allows for a keyworded, variable number of arguments as a dictionary. This functionality is beneficial for creating flexible functions that can handle different sets and numbers of arguments, thereby enhancing reusability and abstraction levels in code design .
Python's `zip` function pairs elements from multiple lists together, providing a convenient way to iterate over tuples of paired elements. For example, given `products = ['table', 'chair', 'sofa', 'bed', 'lamp']` and `prices = [50, 20, 200, 150, 10]`, iterating with `zip(products, prices)` results in tuple pairs that can be used to generate outputs like 'Product: table, Price: 50', illustrating the creation of associative arrays or dictionaries-like structures in Python .
`.py` files contain the source code of Python programs, whereas `.pyc` files contain the bytecode compiled from the source code for execution by the Python interpreter. `.pyc` files are automatically generated to improve startup speed by skipping the source compilation process. This distinction implies that while debugging or inspecting code, `.py` files are necessary, but `.pyc` files enhance performance by reducing runtime compilation overhead .
To replace underscores with hyphens in a list of strings, you can use a list comprehension. For a list like `columns = ['First_name', 'Middle_name', 'Last_name']`, a comprehension `[col.replace('_', '-') for col in columns]` will result in `['First-name', 'Middle-name', 'Last-name']`. This process involves iterating over each string in the list and applying the `replace` method to each one, demonstrating both list comprehensions and string manipulation .
In Python, one can check for the existence of an item within a list using the `in` keyword. For instance, to check if 'trek' is in the list of bikes `bikes = ['trek', 'redline', 'giant']`, the expression `'trek' in bikes` returns `True`. This capability is crucial for decision-making processes where the presence or absence of an item affects the flow of program execution .
The `join` method in Python is used to concatenate elements of a list into a single string with a specified separator. For example, given a list `a = ['Python', 'tutorial', 'and', 'test']`, using `' '.join(a)` produces the string 'Python tutorial and test'. This method is useful for combining a list of words into a sentence or a CSV line .
Python's `lambda` functions are small anonymous functions defined with the `lambda` keyword instead of `def`. They can take any number of arguments, but have a single expression. When used with the `filter` function, such as in `list(filter(lambda x: x % 2, numbers))` for `numbers = [1, 2, 3, 4, 5, 6]`, the `lambda` function checks each number for oddness (`x % 2`), resulting in a list `[1, 3, 5]`. This demonstrates the use of `lambda` for concise, inline functions particularly in functional operations .