How can you create a custom exception class in Python?
- class MyException(Exception):
- define MyException as a function
- import customexception
- MyException extends Throwable
To create a custom exception class in Python, you should define a new class that inherits from the built-in Exception class (or one of its subclasses). This allows you to raise and catch instances of your custom exception when necessary.
How would you create a try block that catches both ValueError and TypeError exceptions?
- try: catch ValueError, TypeError:
- try: except (ValueError, TypeError):
- try: except Exception as e:
- try: except ValueError, TypeError:
To catch multiple exceptions like ValueError and TypeError in Python, you should use the except (ValueError, TypeError):
syntax. This allows you to handle multiple exception types in a single except block.
Which Python built-in function can be used to iterate over a sequence while also obtaining the index of the current item?
- enumerate()
- for_each()
- index()
- iterate()
The enumerate() function in Python is used to iterate over a sequence (like a list or tuple) while also obtaining the index of the current item. It returns pairs of index and value, making it helpful for certain looping scenarios.
How can you exit a loop prematurely in Python?
- break
- exit
- stop
- terminate
In Python, you can exit a loop prematurely using the break statement. When break is encountered within a loop, it immediately terminates the loop and continues with the next code after the loop.
What keyword is used to catch exceptions in Python?
- catch
- except
- throw
- try
In Python, you use the "except" keyword to catch exceptions. Exceptions are used to handle errors and unexpected situations in your code. When an exception occurs, the code within the "except" block is executed.
How do you create a loop that iterates as long as a specific condition is true?
- for
- repeat
- until
- while
In Python, you use the "while" keyword to create a loop that continues to iterate as long as a specific condition is true. The condition is evaluated before each iteration.
Which Python keyword is used to start an if statement?
- elif
- if
- then
- when
In Python, the keyword "if" is used to start an if statement. It is followed by a condition, and if that condition evaluates to True, the indented block of code beneath it is executed.
You are implementing a function to calculate the factorial of a number. Which Python built-in data type would be most suitable to store the result for very large numbers?
- Decimal
- float
- int
- long
For very large numbers, the Decimal data type from the decimal module is the most suitable choice. It provides arbitrary-precision arithmetic and can handle extremely large integers without loss of precision. The int type has a limited size, and the float type is not suitable for precise integer calculations. The long type is not a standard Python type; Decimal is the recommended choice for arbitrary precision.
You are developing a Python application where you need to store configuration settings. Which data type would you use to store key-value pairs of configuration settings?
- Dictionaries
- Lists
- Sets
- Tuples
In Python, dictionaries are used to store key-value pairs, making them an excellent choice for storing configuration settings where you can easily access values by their keys. Lists and tuples are used for ordered collections of items, and sets are used for storing unique elements.
You are given a task to parse string dates in various formats and convert them to datetime objects in Python. Which module would you use to achieve this?
- calendar
- datetime
- dateutil
- time
In Python, the datetime module provides classes for working with dates and times, making it the appropriate choice for parsing string dates into datetime objects. The time module deals with lower-level time-related operations, and the calendar module is used for calendar-related calculations. The dateutil module is a third-party library that enhances the capabilities of datetime.