Question
stringlengths
14
62
Answer
stringlengths
37
98
What is Python?
Python is a high-level, interpreted programming language known for its simplicity and readability.
How do you declare a variable in Python?
You declare a variable by assigning a value using '=' (e.g., x = 10).
What are Python鈥檚 key features?
Python is dynamically typed, interpreted, object-oriented, and supports multiple paradigms.
What is PEP 8?
PEP 8 is the official style guide for Python code to improve code readability.
How do you comment in Python?
You can comment using '#' for single-line and triple quotes for multi-line comments.
What are Python's data types?
Common data types include int, float, str, list, tuple, dict, set, and bool.
What is a list in Python?
A list is an ordered, mutable collection of elements enclosed in square brackets.
What is a tuple?
A tuple is an ordered, immutable collection of elements enclosed in parentheses.
What is the difference between a list and a tuple?
Lists are mutable, while tuples are immutable.
What is a dictionary in Python?
A dictionary is an unordered collection of key-value pairs enclosed in curly braces.
How do you create a dictionary?
You create a dictionary using: my_dict = {'name': 'Alice', 'age': 25}.
What is the difference between del and remove in lists?
'del' removes an element by index, while 'remove' removes by value.
What is slicing in Python?
Slicing extracts parts of sequences using [start:end:step] notation.
How do you reverse a list?
You can reverse a list using list[::-1] or list.reverse().
What is the difference between 'is' and '=='?
'is' checks object identity, while '==' checks value equality.
How do you iterate over a dictionary?
You iterate using a loop: for key, value in dict.items():
What is a set in Python?
A set is an unordered collection of unique elements.
What is a lambda function?
A lambda function is an anonymous, single-expression function: lambda x: x * 2.
How do you define a function in Python?
Use the 'def' keyword: def my_function():
What is the purpose of the return statement?
The return statement exits a function and returns a value.
What is recursion in Python?
Recursion is when a function calls itself.
What are *args and **kwargs?
*args allows variable positional arguments, **kwargs allows variable keyword arguments.
What is list comprehension?
List comprehension provides a concise way to create lists.
What is a generator in Python?
Generators yield values one at a time using the 'yield' keyword.
What is the difference between deep copy and shallow copy?
A shallow copy copies references, while a deep copy creates a new independent object.
What is an exception in Python?
An exception is an error that occurs during program execution.
How do you handle exceptions?
Use try-except blocks to handle exceptions.
What is the difference between 'try-except' and 'try-finally'?
'try-except' handles errors; 'try-finally' ensures cleanup code runs.
What is Python鈥檚 with statement?
The 'with' statement manages resources automatically, like file handling.
What is a module in Python?
A module is a Python file containing reusable code.
What are Python packages?
A package is a collection of modules.
How do you install external packages?
Use 'pip install package_name' to install external packages.
What is the difference between Python 2 and Python 3?
Python 3 improved syntax and Unicode handling over Python 2.
What is an iterator?
An iterator is an object with a __next__() method.
What is a Python decorator?
A decorator modifies functions or methods dynamically.
What is monkey patching?
Monkey patching dynamically changes a module or class at runtime.
What is the difference between staticmethod and classmethod?
staticmethod doesn鈥檛 access the instance; classmethod operates on the class.
What is the purpose of the __init__ method?
__init__ initializes an instance of a class.
What is self in Python classes?
'self' refers to the instance of the class.
What is inheritance in Python?
Inheritance allows a class to inherit methods from a parent class.
What is multiple inheritance?
Multiple inheritance allows a class to inherit from multiple parents.
What is polymorphism?
Polymorphism allows different classes to use the same interface.
What is encapsulation?
Encapsulation restricts direct access to object attributes.
What is an abstract class?
An abstract class has at least one abstract method and cannot be instantiated.
What is the purpose of super()?
super() calls a method from the parent class.
What is a metaclass in Python?
A metaclass defines how classes behave.
What are magic methods in Python?
Magic methods (dunder methods) provide special functionalities, like __len__ or __add__.
What is __str__ vs __repr__?
__str__ returns a user-friendly string, while __repr__ is for debugging.
How do you work with files in Python?
Use open('file.txt', 'r') to open files in Python.
How do you read a file line by line in Python?
Use a loop: 'for line in open('file.txt'):' to read line by line.
README.md exists but content is empty.
Downloads last month
0