Python Basics - Q&A
1. List the data types in Python.
Python has the following standard data types:
- Numeric: int, float, complex
- Sequence: str, list, tuple, range
- Mapping: dict
- Set: set, frozenset
- Boolean: bool
- Binary: bytes, bytearray, memoryview
- None type: NoneType
2. Write the syntax for if...elif...else conditionals.
if condition1:
# code block
elif condition2:
# code block
else:
# code block
3. Explain range() function with its syntax.
The range() function generates a sequence of numbers.
Syntax:
range(start, stop, step)
- start: (optional) starting value (default is 0)
- stop: end value (not inclusive)
- step: (optional) difference between each number (default is 1)
Example: range(1, 10, 2) generates: 1, 3, 5, 7, 9
4. When is a dictionary used instead of a list?
A dictionary is used when you want to associate keys with values and need fast lookups based on
custom keys.
Use a dictionary when:
- You need to store data with a unique identifier (like a name, ID).
- Fast retrieval by key is required.
- Order doesn't matter (in older Python versions).
5. Describe the distinction between Errors and Exceptions.
- Errors: Serious problems that a program should not try to catch (e.g., syntax errors).
- Exceptions: Issues that can be caught and handled during runtime (e.g., ZeroDivisionError,
FileNotFoundError).
Errors are usually unrecoverable, while exceptions can be managed using try-except blocks.