Beginner Level Questions
1. What is Python?
Python is a high-level, interpreted, general-purpose programming language. It's known for its simple, readable syntax, which makes it a great language for beginners. It's used in web development, data science, automation, artificial intelligence, and more.
2. What are the key features of Python?
- Easy to Learn and Read: Its syntax is clean and similar to English.
- Interpreted Language: Code is executed line by line, which makes debugging easier.
- Dynamically Typed: You don't need to declare the type of a variable.
- Large Standard Library: It comes with a rich set of modules and functions.
- Cross-Platform: Python code can run on various operating systems like Windows, macOS, and Linux.
- Object-Oriented: It supports object-oriented programming principles.
3. What is the difference between a list and a tuple?
The main difference is that lists are mutable (they can be changed after creation), while tuples are immutable (they cannot be changed).
my_list = [1, 2, 3]
my_list[0] = 99 # This is okay
print(my_list) # Output: [99, 2, 3]
my_tuple = (1, 2, 3)
# my_tuple[0] = 99 # This will raise a TypeError
4. What are Python's built-in data types?
Python has several built-in data types:
- Text Type:
str
- Numeric Types:
int, float, complex
- Sequence Types:
list, tuple, range
- Mapping Type:
dict
- Set Types:
set, frozenset
- Boolean Type:
bool (True/False)
- Binary Types:
bytes, bytearray, memoryview
5. What is PEP 8?
PEP 8 (Python Enhancement Proposal 8) is the official style guide for Python code. It provides conventions for writing readable and consistent Python code, covering aspects like naming conventions, code layout, and comments.
6. What is the difference between `.py` and `.pyc` files?
.py files: These are the source code files that you write.
.pyc files: These are compiled bytecode files. When you run a Python script, the interpreter compiles the source code into bytecode (a low-level, platform-independent representation) and saves it as a .pyc file. This helps in speeding up subsequent executions of the script because the compilation step is skipped.
7. How is memory managed in Python?
Memory management in Python is handled automatically by the Python Memory Manager. It uses a private heap to store all Python objects and data structures. The manager has different components, including a reference counting mechanism and a garbage collector to automatically free up memory that is no longer in use.
8. What are local and global variables?
- Global variables: Variables declared outside of any function. They can be accessed from anywhere in the code.
- Local variables: Variables declared inside a function. They can only be accessed within that function.
x = "global" # Global variable
def my_function():
y = "local" # Local variable
print(x) # Can access global x
print(y)
my_function()
# print(y) # This would cause an error because y is local to the function
9. What is slicing in Python?
Slicing is a feature that allows you to access a specific range of items in a sequence (like a list, tuple, or string). You use a colon : to specify the start, end, and step of the slice.
my_list = [0, 1, 2, 3, 4, 5, 6]
# Get elements from index 2 up to (but not including) index 5
print(my_list[2:5]) # Output: [2, 3, 4]
# Get elements from the beginning up to index 3
print(my_list[:3]) # Output: [0, 1, 2]
# Get elements from index 4 to the end
print(my_list[4:]) # Output: [4, 5, 6]
10. What are list comprehensions?
List comprehensions provide a concise and readable way to create lists. They often consist of an expression followed by a for clause, inside square brackets.
# Traditional way
squares = []
for i in range(5):
squares.append(i * i)
# Using list comprehension
squares_comp = [i * i for i in range(5)]
print(squares) # Output: [0, 1, 4, 9, 16]
print(squares_comp) # Output: [0, 1, 4, 9, 16]
11. What is a lambda function?
A lambda function is a small, anonymous function defined with the lambda keyword. It can take any number of arguments but can only have one expression. They are often used when a simple function is needed for a short period.
# A lambda function that adds 10 to a number
add_ten = lambda x: x + 10
print(add_ten(5)) # Output: 15
12. What are `*args` and `**kwargs`?
They are used to pass a variable number of arguments to a function.
*args (Non-Keyword Arguments): Allows you to pass a variable number of positional arguments, which will be received as a tuple.
**kwargs (Keyword Arguments): Allows you to pass a variable number of keyword arguments (arguments with names), which will be received as a dictionary.
def my_func(*args, **kwargs):
print("Args:", args)
print("Kwargs:", kwargs)
my_func(1, 2, 'hello', name='Alice', age=30)
# Output:
# Args: (1, 2, 'hello')
# Kwargs: {'name': 'Alice', 'age': 30}
13. Explain `break`, `continue`, and `pass`.
break: Exits the current loop immediately.
continue: Skips the rest of the current iteration and moves to the next one.
pass: It's a null statement. It does nothing and is used as a placeholder where syntax requires a statement but no action is needed.
14. What is the difference between `is` and `==`?
== (Equality): Checks if the values of two operands are equal.
is (Identity): Checks if two operands refer to the exact same object in memory.
a = [1, 2, 3]
b = [1, 2, 3]
c = a
print(a == b) # True, because the values are the same
print(a is b) # False, because they are different objects in memory
print(a is c) # True, because c refers to the same object as a
15. How do you handle exceptions in Python?
You use a try-except block. The code that might raise an exception is placed inside the try block, and the code to handle the exception is placed in the except block. You can also use else (runs if no exception occurs) and finally (always runs, regardless of exceptions).
try:
result = 10 / 0
except ZeroDivisionError:
print("You can't divide by zero!")
finally:
print("This will always execute.")
16. What are Python modules and packages?
- Module: A single Python file (
.py) containing functions, classes, and variables that can be imported and used in other Python scripts.
- Package: A collection of related modules organized in a directory hierarchy. A directory must contain a file named
__init__.py to be considered a package.
17. How do you open and read a file in Python?
The recommended way is to use the with statement, which automatically handles closing the file.
# Opens 'my_file.txt' for reading ('r') and ensures it's closed
with open('my_file.txt', 'r') as file:
content = file.read()
print(content)
18. What does the `__init__.py` file do?
The __init__.py file has two main purposes:
1. It tells the Python interpreter that a directory should be treated as a package.
2. It can be used to execute package initialization code or set up the __all__ variable, which defines what modules are imported when a user does from package import *.
19. How can you reverse a list in Python?
There are several ways:
- Using the
.reverse() method (in-place): my_list.reverse()
- Using the
reversed() function (returns an iterator): new_list = list(reversed(my_list))
- Using slicing:
new_list = my_list[::-1]
20. What is `self` in a Python class?
self represents the instance of the class. By using self, we can access the attributes and methods of the class in Python. It is the first parameter of any method in a class and binds the attributes with the given arguments.
21. What is a dictionary in Python?
A dictionary is an unordered collection of data values, used to store data values like a map. Unlike other data types that hold only a single value as an element, a dictionary holds a key:value pair. Keys must be unique and immutable (like strings, numbers, or tuples).
my_dict = {'name': 'Alice', 'age': 25, 'city': 'New York'}
print(my_dict['name']) # Output: Alice
22. What are Python's logical operators?
Python has three logical operators:
and: Returns True if both statements are true.
or: Returns True if one of the statements is true.
not: Reverses the result, returns False if the result is true, and vice versa.
23. How do you add a comment in Python?
You use the hash (#) symbol. Anything after the # on the same line is considered a comment and is ignored by the interpreter. For multi-line comments, you can use triple quotes (''' or """).
# This is a single-line comment
"""
This is a multi-line comment
or a docstring.
"""
24. What is type casting in Python?
Type casting is the process of converting a variable from one data type to another. This is done using constructor functions like int(), float(), str(), etc.
x = "100"
y = int(x) # Convert string to integer
print(type(y)) # Output: <class 'int'>
25. How do you get user input in Python?
You use the input() function. This function reads a line from the input, converts it to a string, and returns it.
name = input("Enter your name: ")
print("Hello, " + name)
26. What are f-strings?
F-strings (formatted string literals), introduced in Python 3.6, provide a way to embed expressions inside string literals, using a minimal syntax. You prefix the string with an 'f' and write expressions in curly braces {}.
name = "Bob"
age = 40
print(f"His name is {name} and he is {age} years old.")
# Output: His name is Bob and he is 40 years old.
27. How do you create a function in Python?
You create a function using the def keyword, followed by the function name and parentheses (). Any input parameters or arguments should be placed within these parentheses.
def greet(name):
"""This function greets the person passed in as a parameter."""
print(f"Hello, {name}!")
greet("World") # Calling the function
# Output: Hello, World!
28. What is a docstring?
A docstring is a string literal that occurs as the first statement in a module, function, class, or method definition. It is specified in source code that is used, like a comment, to document a specific segment of code. It is enclosed in triple quotes ("""...""").
29. What is the `range()` function?
The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and stops before a specified number.
for i in range(5):
print(i) # Output: 0, 1, 2, 3, 4
30. What is a set in Python?
A set is an unordered collection of items where every element is unique (no duplicates) and must be immutable. Sets are useful for membership testing, removing duplicates from a sequence, and mathematical operations like union, intersection, and difference.
my_set = {1, 2, 3, 3, 4}
print(my_set) # Output: {1, 2, 3, 4}
Intermediate Level Questions
31. What are decorators in Python?
A decorator is a design pattern in Python that allows a user to add new functionality to an existing object without modifying its structure. Decorators are usually called before the definition of a function you want to decorate.
def my_decorator(func):
def wrapper():
print("Something is happening before the function is called.")
func()
print("Something is happening after the function is called.")
return wrapper
@my_decorator
def say_hello():
print("Hello!")
say_hello()
32. What is a generator?
A generator is a special type of function that returns an iterator, which can be iterated over. It looks like a normal function but uses the yield keyword instead of return. Generators are memory-efficient for working with large sequences because they produce items one at a time, on-demand, rather than creating the entire sequence in memory at once.
33. What is the Global Interpreter Lock (GIL)?
The GIL is a mutex (a lock) that protects access to Python objects, preventing multiple native threads from executing Python bytecode at the same time. This means that in CPython (the standard Python implementation), only one thread can be executing Python code at any given moment, even on a multi-core processor. This simplifies memory management but can be a performance bottleneck for CPU-bound multi-threaded programs.
34. What is the difference between deep copy and shallow copy?
- Shallow Copy: Creates a new object but inserts references into it to the objects found in the original. If you change a nested object in the copy, it will also change in the original.
- Deep Copy: Creates a new object and recursively copies all objects found in the original. Changes made to the copy (including nested objects) will not affect the original.
import copy
original = [[1, 2], [3, 4]]
shallow = copy.copy(original)
deep = copy.deepcopy(original)
shallow[0][0] = 99
print(original) # Output: [[99, 2], [3, 4]] (changed!)
print(deep) # Output: [[1, 2], [3, 4]] (not changed)
35. What is pickling and unpickling?
Pickling is the process of converting a Python object hierarchy into a byte stream (a sequence of bytes). Unpickling is the inverse operation, whereby a byte stream is converted back into an object hierarchy. This is useful for storing Python objects in a file or database, or for transmitting them over a network.
36. What are iterators in Python?
An iterator is an object that contains a countable number of values. It is an object that can be iterated upon, meaning that you can traverse through all the values. In Python, an iterator is an object which implements the iterator protocol, which consists of the methods __iter__() and __next__().
37. How does the `with` statement work (Context Managers)?
The with statement is used to wrap the execution of a block of code with methods defined by a context manager. A context manager is an object that defines the methods __enter__() and __exit__(). The with statement guarantees that the __exit__() method is called at the end of the block, which is useful for automatically managing resources like file handles or database connections.
38. What are Python namespaces?
A namespace is a system that has a unique name for each and every object in Python. It's a mapping from names to objects. Python has different namespaces:
- Built-in Namespace: Contains names of all of Python’s built-in objects.
- Global Namespace: Contains names from a module.
- Local Namespace: Contains names inside a function.
39. What are `map`, `filter`, and `reduce`?
map(): Applies a given function to each item of an iterable (like a list) and returns a map object (an iterator).
filter(): Constructs an iterator from elements of an iterable for which a function returns true.
reduce(): Applies a rolling computation to sequential pairs of values in a list. It's part of the `functools` module.
40. What are magic methods (dunder methods)?
Magic methods, also known as dunder (double underscore) methods, are special methods in Python that are surrounded by double underscores, like __init__ or __str__. They are not meant to be called directly by you but are invoked internally by the class on a certain action. For example, when you use the + operator, the __add__ method is called.
41. What is inheritance in Python?
Inheritance is a fundamental concept of Object-Oriented Programming (OOP). It is the mechanism by which one class (the child or subclass) can inherit the attributes and methods of another class (the parent or superclass). This promotes code reusability.
class Animal: # Parent class
def speak(self):
print("Animal speaks")
class Dog(Animal): # Child class inherits from Animal
def bark(self):
print("Dog barks")
d = Dog()
d.speak() # Inherited method
d.bark()
42. What are class methods and static methods?
- Instance Method: The most common method type. It takes
self as the first argument and can access and modify instance attributes.
- Class Method: Marked with a
@classmethod decorator. It takes cls (the class itself) as the first argument. It can modify class-level state that applies to all instances of the class.
- Static Method: Marked with a
@staticmethod decorator. It doesn't take self or cls as the first argument. It's essentially a regular function that is namespaced within a class and doesn't have access to instance or class state.
43. What is polymorphism?
Polymorphism, another OOP concept, means "many forms". It refers to the ability of different objects to respond to the same method call in different ways. For example, both a `Dog` and `Cat` object could have a `speak()` method, but each would implement it differently (one barks, one meows).
44. What is encapsulation?
Encapsulation is the bundling of data (attributes) and the methods that operate on that data into a single unit (a class). It also involves restricting direct access to some of an object's components, which is a key principle of data hiding. In Python, this is often done by convention using a single underscore prefix (e.g., _my_variable) to indicate an attribute is "protected".
45. What is the `super()` function used for?
The super() function is used to give access to methods and properties of a parent or sibling class. It is most commonly used in a subclass's __init__ method to call the parent class's __init__ method, ensuring that the parent's initialization is done correctly.
46. What are virtual environments?
A virtual environment is a self-contained directory tree that contains a Python installation for a particular version of Python, plus a number of additional packages. It allows you to work on a specific project without affecting other projects. This avoids conflicts with package versions and keeps your global site-packages directory clean.
47. What is the difference between `append()` and `extend()` for lists?
append(): Adds its single argument as a single element to the end of a list.
extend(): Iterates over its argument (which must be an iterable) and adds each element to the list, extending the list.
my_list = [1, 2]
my_list.append([3, 4])
print(my_list) # Output: [1, 2, [3, 4]]
my_list = [1, 2]
my_list.extend([3, 4])
print(my_list) # Output: [1, 2, 3, 4]
48. How does Python's garbage collection work?
Python's primary garbage collection mechanism is reference counting. Every object has a count of the number of references pointing to it. When this count drops to zero, the object's memory is deallocated. To handle reference cycles (where objects refer to each other), Python also has a cyclic garbage collector that periodically finds and breaks these cycles.
49. What is monkey patching?
Monkey patching refers to the dynamic modification of a class or module at runtime. This means you can change the behavior of a function or method after it has already been defined. It's a powerful but potentially dangerous technique that should be used with caution.
50. What is a regular expression?
A regular expression (or regex) is a special sequence of characters that helps you match or find other strings or sets of strings, using a specialized syntax held in a pattern. Python's re module provides support for regular expressions.
51. What is the difference between a module and a script?
In general, a "script" is a file designed to be executed directly from the command line. A "module" is a file that is designed to be imported into other scripts or modules. However, any Python file can be both a module and a script.
52. What does `if __name__ == "__main__":` mean?
This is a common idiom in Python scripts. The special variable `__name__` is set to "__main__" when the script is executed directly. If the script is imported as a module into another script, `__name__` is set to the module's name. Therefore, the code inside this block will only run when the script is executed directly, not when it's imported.
53. What are unit tests in Python?
Unit tests are a software testing method where individual units or components of a software are tested. The purpose is to validate that each unit of the software code performs as expected. Python provides a built-in module called unittest for this, and popular third-party libraries like pytest are also widely used.
54. What is duck typing?
Duck typing is a concept related to dynamic typing, where the type or class of an object is less important than the methods it defines. The name comes from the saying: "If it walks like a duck and it quacks like a duck, then it must be a duck." In Python, you don't care what an object *is*, only what it *does* (i.e., what methods it has).
55. What is Flask/Django?
Both Flask and Django are popular web frameworks for Python.
- Django: A high-level, "batteries-included" web framework that encourages rapid development and clean, pragmatic design. It includes an ORM, admin panel, authentication system, and more out of the box.
- Flask: A micro-framework that is much more lightweight and flexible. It provides the basics (like routing and templating) and lets the developer choose which libraries and tools they want to use for other tasks.
56. What are mutable and immutable data types?
- Mutable objects: Their state or content can be changed after they are created. Examples:
list, dict, set.
- Immutable objects: Their state cannot be changed after they are created. If you modify them, a new object is created. Examples:
int, float, str, tuple, frozenset.
57. What are Python's access specifiers?
Unlike languages like Java, Python does not have strict access specifiers like public, private, or protected. However, it has conventions:
- Public: All members are public by default.
- Protected: A single underscore prefix (e.g.,
_variable) is a convention to indicate that a member is for internal use and should not be accessed from outside the class.
- Private: A double underscore prefix (e.g.,
__variable) triggers name mangling, making it harder (but not impossible) to access from outside the class.
58. How do you format a string in Python (pre-f-strings)?
Before f-strings (Python 3.6+), the main ways were:
- %-formatting (old style):
"Hello, %s" % name
str.format() method (newer style): "Hello, {}".format(name) or "Hello, {n}".format(n=name)
59. What are named tuples?
Named tuples are a part of the collections module. They are like regular tuples but the elements can also be accessed using names instead of only indices. This makes the code more readable and self-documenting.
from collections import namedtuple
Point = namedtuple('Point', ['x', 'y'])
p = Point(10, 20)
print(p.x, p.y) # Output: 10 20
60. What is the `collections.Counter` class?
Counter is a dictionary subclass for counting hashable objects. It's a collection where elements are stored as dictionary keys and their counts are stored as dictionary values.
from collections import Counter
my_list = ['a', 'b', 'c', 'a', 'b', 'a']
counts = Counter(my_list)
print(counts) # Output: Counter({'a': 3, 'b': 2, 'c': 1})
Advanced Level Questions
61. What are metaclasses in Python?
A metaclass is a "class of a class". While a class defines how an instance of the class behaves, a metaclass defines how a class behaves. A class is an instance of a metaclass. The default metaclass is type. You can create custom metaclasses to modify classes automatically at creation time, for example, to register classes or enforce coding standards.
62. What are descriptors?
A descriptor is an object attribute with "binding behavior", one whose attribute access has been overridden by methods in the descriptor protocol. These methods are __get__(), __set__(), and __delete__(). Descriptors are the mechanism behind properties, methods, static methods, class methods, and super().
63. Explain `async` and `await` and how they work.
async and await are keywords used for asynchronous programming in Python. An async def function defines a coroutine. The await keyword is used inside a coroutine to pause its execution and wait for another coroutine to complete. This allows the event loop to run other tasks while waiting for I/O operations (like network requests or database queries) to finish, improving performance and concurrency without using threads.
64. What is the purpose of `__slots__`?
By default, Python classes store instance attributes in a dictionary called __dict__. This can use a significant amount of memory if you have millions of instances. The __slots__ class variable allows you to explicitly declare which instance attributes you expect your objects to have. This has two main benefits:
1. It's much more memory-efficient as Python doesn't need to create a `__dict__` for each instance.
2. It results in faster attribute access.
65. What is Cython?
Cython is a programming language that makes writing C extensions for Python as easy as Python itself. It is a superset of Python that also supports C-like features, such as static typing. You can write Python code and add type annotations, which Cython translates into optimized C/C++ code. This is often used to speed up CPU-bound Python code significantly.
66. How does a Python `for` loop work under the hood?
A `for` loop works with any object that is an iterable. When the loop starts, it calls the `iter()` function on the object, which returns an iterator. The loop then repeatedly calls the `next()` function on this iterator to get the next item, until a `StopIteration` exception is raised, which signals the end of the loop.
67. What is WSGI and ASGI?
- WSGI (Web Server Gateway Interface): It is the standard interface between web servers and Python web applications or frameworks for synchronous programming. It defines how a web server communicates with a Python web application.
- ASGI (Asynchronous Server Gateway Interface): It is the successor to WSGI, designed to handle asynchronous web frameworks and applications. It supports all that WSGI does but also adds support for asynchronous features like WebSockets and long-polling.
68. What are function annotations?
Function annotations are a Python 3 feature that allows you to add arbitrary metadata to function parameters and return values. They are stored in a special __annotations__ attribute of the function and are not enforced by Python. They are primarily used by third-party libraries, for example, to provide type hints for static analysis.
def my_func(name: str, age: int) -> str:
return f"{name} is {age}."
69. Explain how to profile a Python script.
Profiling is the process of analyzing a program's performance to identify bottlenecks. Python provides a built-in profiler module called cProfile. You can run it from the command line on your script to get a detailed breakdown of how much time was spent in each function.
python -m cProfile my_script.py
This will output statistics about function calls, execution time, and more, helping you find which parts of your code are slow and need optimization.
70. What is the difference between `__str__` and `__repr__`?
__str__: Is used to provide a user-friendly, "informal" string representation of an object. It is called by the str() built-in function and by the print() function. The goal is readability.
__repr__: Is used to provide an "official" or developer-friendly string representation of an object. It should ideally be an unambiguous representation that could be used to recreate the object. It is called by the repr() built-in function. If __str__ is not defined, Python will use __repr__ as a fallback.
71. What is an Abstract Base Class (ABC)?
An Abstract Base Class is a class that cannot be instantiated. Its primary purpose is to define a common interface for a set of subclasses. By using ABCs, you can enforce that derived classes implement specific methods from the base class, which helps in designing robust and predictable class hierarchies. Python's `abc` module is used to create ABCs.
72. What are data classes?
Introduced in Python 3.7, data classes are classes that are primarily used to store data. The @dataclass decorator automatically generates special methods like __init__(), __repr__(), __eq__(), etc., based on the class's type-annotated attributes. This saves a lot of boilerplate code.
from dataclasses import dataclass
@dataclass
class Point:
x: int
y: int
p = Point(10, 20)
print(p) # Output: Point(x=10, y=20)
73. What is method overloading and does Python support it?
Method overloading is the ability to define multiple methods with the same name but different parameters within the same class. Python does not support method overloading in the traditional sense. If you define two methods with the same name, the last one defined will overwrite the previous one. However, you can achieve similar functionality by using default arguments or `*args` and `**kwargs` to handle a variable number of arguments in a single method.
74. What is method overriding?
Method overriding is an OOP concept that allows a subclass to provide a specific implementation of a method that is already provided by one of its parent classes. When a method in a subclass has the same name, parameters, and return type as a method in its superclass, the method in the subclass is said to override the method in the superclass.
75. What is the purpose of the `enumerate()` function?
The enumerate() function adds a counter to an iterable and returns it as an enumerate object. This is useful when you need both the index and the value of each item in a sequence during iteration.
my_list = ['apple', 'banana', 'cherry']
for index, value in enumerate(my_list):
print(index, value)
# Output:
# 0 apple
# 1 banana
# 2 cherry
76. What is the `zip()` function?
The zip() function takes two or more iterables and aggregates them, creating an iterator that produces tuples. Each tuple contains elements from all the input iterables at the same position. The iteration stops as soon as the shortest input iterable is exhausted.
list1 = [1, 2, 3]
list2 = ['a', 'b', 'c']
for num, letter in zip(list1, list2):
print(num, letter)
# Output:
# 1 a
# 2 b
# 3 c
77. What is type hinting in Python?
Type hinting is a formal way to indicate the type of a variable, function parameter, or return value. These hints are not enforced by the Python interpreter at runtime but can be used by static analysis tools (like Mypy) to catch type-related errors before the code is run. It improves code readability and maintainability.
78. What is `pip`?
pip is the standard package manager for Python. It allows you to install and manage third-party software packages that are not part of the Python standard library. You use it from the command line to install packages from the Python Package Index (PyPI).
79. Explain the difference between `range` and `xrange` in Python 2.
This is a common question to check knowledge of Python 2 vs 3. In Python 2:
- `range()`: Creates a complete list of numbers in memory. For a large range, this can be memory-intensive.
- `xrange()`: Creates a generator-like object that yields numbers on demand. It is much more memory-efficient for large ranges.
In Python 3, the old `range()` was removed, and `xrange()` was renamed to `range()`. So, Python 3's `range()` behaves like Python 2's `xrange()`.
80. How can you combine two dictionaries?
In Python 3.9+, you can use the union operator `|`:
dict1 = {'a': 1}
dict2 = {'b': 2}
merged = dict1 | dict2 # {'a': 1, 'b': 2}
In older versions, you can use dictionary unpacking ** or the .update() method:
merged = {**dict1, **dict2}
dict1.update(dict2) # Modifies dict1 in-place
81. What is a Ternary Operator?
A ternary operator is a concise way to write a simple if-else statement in a single line. It evaluates a condition and returns one value if the condition is true, and another if it is false.
# A is assigned 'True Value' if condition is true, else 'False Value'
A = "True Value" if condition else "False Value"
82. What is negative indexing?
Negative indexing allows you to access elements of a sequence from the end. The last element has an index of -1, the second-to-last has an index of -2, and so on.
my_list = ['a', 'b', 'c', 'd']
print(my_list[-1]) # Output: d
print(my_list[-2]) # Output: c
83. What is an ORM?
ORM stands for Object-Relational Mapping. It is a programming technique that converts data between incompatible type systems in object-oriented programming languages. An ORM library provides a high-level abstraction upon a relational database that allows a developer to write Python code instead of SQL to create, read, update, and delete data and schemas in their database. Examples include SQLAlchemy and the Django ORM.
84. What is a closure in Python?
A closure is a function object that remembers values in enclosing scopes even if they are not present in memory. It's a function that is defined inside another function (the outer function) and has access to variables from the outer function's scope, even after the outer function has finished execution.
85. How can you check if all items in a list are unique?
A simple way is to convert the list to a set and compare the lengths. Since sets only contain unique elements, if the lengths are the same, all items in the list were unique.
def all_unique(items):
return len(items) == len(set(items))
list1 = [1, 2, 3, 4]
list2 = [1, 2, 3, 1]
print(all_unique(list1)) # True
print(all_unique(list2)) # False
86. What is the difference between `__init__` and `__new__`?
__new__ is the first step of instance creation. It's a static method that takes the class it's called on as its first argument. Its job is to create and return a new, empty instance of that class.
__init__ is the initializer. It's called after the instance has been created by __new__. Its job is to initialize the newly created object.
You rarely need to override __new__, unless you are subclassing an immutable type like `str` or `int`.
87. What are walrus operators (`:=`)?
The walrus operator, introduced in Python 3.8, is a new syntax for assignment expressions. It allows you to assign a value to a variable as part of a larger expression. It is useful for simplifying code in loops and comprehensions where you need to use a value after checking it.
# Instead of:
line = file.readline()
while line:
print(line)
line = file.readline()
# You can write:
while (line := file.readline()):
print(line)
88. What is the `itertools` module?
The `itertools` module is a collection of tools for handling iterators. It provides a set of fast, memory-efficient functions that can be used to create complex iterators for tasks like permutations, combinations, and creating Cartesian products.
89. What is the difference between a process and a thread?
- Process: An instance of a program running. Each process has its own separate memory space. Processes are more isolated and do not share memory.
- Thread: A segment of a process. Threads within the same process share the same memory space. This makes communication between threads easier but also raises challenges like race conditions. Due to the GIL, multithreading in Python is best for I/O-bound tasks, while multiprocessing is better for CPU-bound tasks.
90. How do you sort a Python dictionary?
Dictionaries themselves are inherently unordered (though in Python 3.7+ they preserve insertion order). You cannot sort a dictionary directly. However, you can create a sorted representation of its items (key-value pairs) based on keys or values.
my_dict = {'c': 3, 'a': 1, 'b': 2}
# Sort by key
sorted_by_key = sorted(my_dict.items()) # [('a', 1), ('b', 2), ('c', 3)]
# Sort by value
sorted_by_value = sorted(my_dict.items(), key=lambda item: item[1]) # [('a', 1), ('b', 2), ('c', 3)]
91. How can you make a Python script executable on Unix?
You need to do two things:
1. Add a shebang line at the very top of the script. This tells the system which interpreter to use. For example: #!/usr/bin/env python3
2. Make the script executable by changing its permissions using the command line: chmod +x your_script.py
92. What are Python Enhancement Proposals (PEPs)?
PEPs are design documents for the Python community. They provide information or describe a new feature for Python, its processes, or environment. PEP 8 is the style guide, while others might introduce new syntax (like the walrus operator in PEP 572) or guidelines for the community.
93. What is the purpose of the `any()` and `all()` functions?
any(): Returns True if at least one element of an iterable is true.
all(): Returns True if all elements of an iterable are true.
94. What is a race condition?
A race condition occurs when multiple threads or processes access shared data and try to change it at the same time. Because the scheduler can swap between threads at any time, the final result depends on the order of execution, which can lead to unpredictable and incorrect behavior. Locks and other synchronization primitives are used to prevent race conditions.
95. What is the `deque` object?
A `deque` (double-ended queue) is part of the `collections` module. It is a list-like container that supports fast appends and pops from both ends. While appending to the end of a regular list is fast, inserting or appending at the beginning is slow because all other elements have to be shifted. Deques are optimized for these operations.
96. What is memoization?
Memoization is an optimization technique used primarily to speed up computer programs by storing the results of expensive function calls and returning the cached result when the same inputs occur again. In Python, you can implement memoization using a dictionary to store results or by using a decorator like `@lru_cache` from the `functools` module.
97. What is API?
API stands for Application Programming Interface. It is a set of rules and protocols that allows different software applications to communicate with each other. For example, when you use a weather app on your phone, the app communicates with a weather service's API to fetch the latest weather data.
98. What are some popular Python libraries for data science?
Some of the most popular libraries are:
- NumPy: For numerical operations and working with arrays.
- Pandas: For data manipulation and analysis, providing data structures like DataFrames.
- Matplotlib / Seaborn: For data visualization and plotting.
- Scikit-learn: For machine learning algorithms.
- TensorFlow / PyTorch: For deep learning.
99. What are first-class functions?
In Python, functions are "first-class citizens". This means that functions can be treated like any other object: they can be assigned to variables, passed as arguments to other functions, stored in data structures like lists, and returned from other functions.
100. How can you find the ID of an object in Python?
You can use the built-in id() function. It returns a unique integer that serves as the "identity" of an object for its lifetime. This ID is typically the memory address of the object.
x = "hello"
print(id(x)) # Output: A unique integer ID for the "hello" string object