python1

txt

School

New York University *

*We aren’t endorsed by this school

Course

9343

Subject

Computer Science

Date

Jan 9, 2024

Type

txt

Pages

4

Uploaded by CoachPower7127

Report
Sure, preparing for a Python interview with such questions is a great approach. Here are 100 Python interview questions along with concise answers: 1. **Is Python case-sensitive?** - Yes, Python is case-sensitive. 2. **Are lists mutable in Python?** - Yes, lists are mutable in Python. 3. **What is sorting in Python?** - Sorting in Python refers to arranging data in a particular format, such as ascending or descending. 4. **What is a tuple in Python?** - A tuple is an immutable sequence type in Python. 5. **Can you modify a tuple?** - No, tuples are immutable and cannot be modified after creation. 6. **What is a dictionary in Python?** - A dictionary is a collection of key-value pairs. 7. **Are Python dictionaries ordered?** - Yes, as of Python 3.7, dictionaries maintain insertion order. 8. **What is a set in Python?** - A set is an unordered collection of unique elements. 9. **Can sets have duplicates in Python?** - No, sets cannot have duplicate elements. 10. **What does `len()` do in Python?** - `len()` returns the length of an object. 11. **What is slicing in Python?** - Slicing extracts a part of a sequence by specifying a start and end point. 12. **What is the difference between `list` and `array` in Python?** - Lists can hold different data types, while arrays (from the `array` module) hold elements of the same type. 13. **What is list comprehension?** - List comprehension is a concise way to create lists based on existing lists. 14. **What are Python iterators?** - Iterators are objects that can be iterated upon. 15. **What is the difference between `iter()` and `next()`?** - `iter()` returns an iterator, `next()` retrieves the next item from an iterator. 16. **What is a lambda function?** - A lambda function is an anonymous, small function defined with the `lambda` keyword. 17. **What is `None` in Python?** - `None` represents the absence of a value or a null value. 18. **What does the `pass` keyword do?**
- `pass` is a placeholder that does nothing. 19. **What is an exception in Python?** - An exception is an error that occurs during the execution of a program. 20. **How do you handle exceptions?** - Exceptions are handled using `try`, `except` blocks. 21. **What is the use of the `finally` block?** - `finally` executes regardless of whether an exception is caught. 22. **What is a class in Python?** - A class is a blueprint for creating objects. 23. **What is an instance in object-oriented programming?** - An instance is a concrete occurrence of any object. 24. **What is inheritance?** - Inheritance allows a class to inherit attributes and methods from another class. 25. **What is multiple inheritance?** - When a class is derived from more than one base class, it's called multiple inheritance. 26. **What is polymorphism in Python?** - Polymorphism allows different classes to be treated as the same type. 27. **What is encapsulation?** - Encapsulation involves bundling data and methods that operate on it within one unit. 28. **What is a module in Python?** - A module is a file containing Python definitions and statements. 29. **What is `__init__` in Python?** - `__init__` is a method used for initializing a new object. 30. **What is `self` in Python?** - `self` refers to the instance of the class itself. 31. **What is a namespace in Python?** - A namespace is a naming system for identifying each object uniquely. 32. **What is the difference between deep and shallow copy?** - Deep copy creates a new object and recursively copies original elements, while shallow copy creates a new object but inserts references into it to the objects found in the original. 33. **How do you create a virtual environment in Python?** - Use the `venv` module: `python -m venv myenv`. 34. **What is PEP 8?** - PEP 8 is Python's style guide. 35. **What is `__name__` in Python?** - `__name__` is a special variable that represents the name of the module. 36. **What is the difference between `==` and `is`?**
- `==` compares values, `is` compares object identities. 37. **What does the `*args` do?** - `*args` allows a function to take any number of positional arguments. 38. **What does `**kwargs` do?** - `**kwargs` allows a function to take any number of keyword arguments. 39. **What is a generator in Python?** - A generator is a function that returns an iterator and generates values using the `yield` statement. 40. **What are decorators in Python?** - Decorators are a way to modify the behavior of a function or class. 41. **What is the Global Interpreter Lock (GIL)?** - GIL is a mutex that protects access to Python objects, preventing multiple threads from executing Python bytecodes at once. 42. **What is a context manager in Python?** - A context manager is a simple “protocol” (or interface) that your object needs to follow to support the `with` statement. 43. **What is the purpose of the `with` statement?** - The `with` statement is used to wrap the execution of a block with methods defined by a context manager. 44. **What is `pip`?** - `pip` is the package installer for Python. 45. **What are Python's built-in data types?** - Built-in data types include numerics (like int and float), sequences (like list, tuple, and range), mappings (like dict), sets, and boolean. 46. **What is slicing?** - Slicing is a way to get a subset of items from an iterable. 47. **How do you swap two variables in Python?** - You can swap two variables like this: `a, b = b, a`. 48. **What is the difference between `append()` and `extend()` methods?** - `append()` adds its argument as a single element to the end of a list. `extend()` extends a list by appending elements from an iterable. 49. **How is string interpolation done?** - String interpolation can be done using formatted string literals (f-strings), the `format()` method, or the `%` operator. 50. **What is the use of the `global` keyword?** - The `global` keyword is used to modify a variable outside of the current scope. 51. **What are Python's built-in types of exceptions?** - Built-in exceptions include `IndexError`, `KeyError`, `ValueError`, `TypeError`, etc. 52. **What is a Python package?**
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
- A package is a collection of Python modules in a directory. 53. **How can you catch multiple exceptions in one line?** - You can catch multiple exceptions in a tuple, like `except (RuntimeError, TypeError, NameError)`. 54. **What is the `enumerate` function in Python?** - `enumerate` adds a counter to an iterable and returns it in a form of enumerating object. 55. **How do you read and write files in Python?** - Use the `open()` function with appropriate mode ('r' for reading, 'w' for writing, etc.) 56. **What is a Python decorator?** - A decorator is a function that adds functionality to an existing function without modifying it. 57. **What is the purpose of `__pycache__`?** - `__pycache__` is a directory that stores bytecode compiled by Python. 58. **How can you concatenate strings in Python?** - Strings can be concatenated using the `+` operator or by using string literals next to each other. 59. **What are `*` and `**` used for in function definitions?** - `*` collects positional arguments in a tuple, `**` collects keyword arguments in a dictionary. 60. **What are Python's built-in sequence types?** - Built-in sequence types include `list`, `tuple`, and `range`. 61. **What is the difference between `del`, `remove`, and `pop` on a list?** - `del` removes an item by index, `remove` removes the first matching value, and `pop` removes an item by index and returns it. 62. **What is the difference between `sort()` and `sorted()`?** - `sort()` sorts a list in place, while `sorted()` returns a new sorted list from the elements of any iterable. 63. **What is the use of the `zip()` function?** - `zip()` is used to combine several iterables into one. 64. **What is Python's memory management model?** - Python uses a combination of reference counting and garbage collection for memory management. 65. **What is the purpose of the `assert` statement?** - `assert` is used for debugging purposes, to assert that a condition is true. 66. **What is a mutable object?** - A mutable object can be changed after it