1.
Basics of Python
1. What is Python?
Python is a high-level, interpreted, and dynamically typed programming language
designed for simplicity and readability. It supports multiple paradigms, including
procedural, object-oriented, and functional programming.
2. What are the key features of Python?
Easy to learn and use
Interpreted and dynamically typed
Extensive standard library
Cross-platform compatibility
Supports multiple programming paradigms
3. What is Python used for?
Python is used in web development, data analysis, artificial intelligence, machine
learning, scientific computing, game development, and more.
2. Variables and Data Types
4. What are the data types in Python?
Numeric types: int, float, complex
Sequence types: list, tuple, range
Text type: str
Set types: set, frozenset
Mapping type: dict
Boolean type: bool
None type: NoneType
5. What is the difference between mutable and immutable objects?
Mutable: Can be changed after creation (e.g., list, dict, set).
Immutable: Cannot be changed after creation (e.g., int, float, tuple, str).
6. How is memory managed in Python?
Memory management in Python involves private heap space and automatic garbage
collection, which reclaims unused memory.
3. Control Statements
7. What are control statements in Python?
Control statements direct the flow of execution in a program:
Conditional statements: if, elif, else
Loops: for, while
Control keywords: break, continue, pass
8. What is the difference between break and continue?
break: Exits the nearest enclosing loop.
continue: Skips the current iteration and moves to the next iteration of the loop.
4. Functions
9. What is a function in Python?
A function is a reusable block of code that performs a specific task. It is defined using
the def keyword.
10. What are the types of functions in Python?
Built-in functions: Provided by Python (e.g., print(), len()).
User-defined functions: Created by users using def.
Anonymous functions: Created using lambda.
11. What is a lambda function?
A lambda function is an anonymous function defined using the lambda keyword,
useful for short, simple operations.
Example:
add = lambda x, y: x + y
print(add(2, 3)) # Output: 5
5. Object-Oriented Programming
12. What is object-oriented programming in Python?
Object-oriented programming (OOP) organizes code into objects, which are instances
of classes. Key concepts include encapsulation, inheritance, and polymorphism.
13. What is the difference between a class and an object?
Class: A blueprint for creating objects.
Object: An instance of a class.
14. What are Python's access specifiers?
Public: Accessible from anywhere (self.variable).
Protected: Accessible within the class and its subclasses (self._variable).
Private: Accessible only within the class (self.__variable).
6. Modules and Packages
15. What is the difference between a module and a package?
Module: A single Python file containing functions, classes, or variables.
Package: A collection of modules organized into directories with an __init__.py file.
16. How do you import modules in Python?
Use the import keyword:
import math
print(math.sqrt(16)) # Output: 4.0
7. File Handling
17. How does Python handle files?
Python uses built-in functions like open(), read(), write(), and close() for file
operations. File modes include:
"r": Read
"w": Write
"a": Append
"r+": Read and write
18. What is the difference between read() and readlines()?
read(): Reads the entire file as a single string.
readlines(): Reads the file line by line into a list.
8. Exception Handling
19. What is exception handling in Python?
Exception handling manages runtime errors using try, except, else, and finally.
20. How do you raise exceptions in Python?
Use the raise keyword:
if age < 18:
raise ValueError("Age must be 18 or above.")
9. Advanced Concepts
21. What is a decorator in Python?
A decorator is a function that modifies the behavior of another function or method. It
is defined using the @decorator_name syntax.
Example:
def decorator(func):
def wrapper():
print("Before the function call")
func()
print("After the function call")
return wrapper
@decorator
def say_hello():
print("Hello!")
say_hello()
22. What are Python generators?
Generators are functions that yield values one at a time using the yield keyword,
instead of returning all at once.
Example:
def generator():
for i in range(3):
yield i
for value in generator():
print(value)
10. Data Structures
23. What is the difference between a list and a tuple?
List: Mutable, defined using [].
Tuple: Immutable, defined using ().
24. What is the difference between a set and a dictionary?
Set: Unordered collection of unique elements.
Dictionary: Collection of key-value pairs.
11. Miscellaneous
25. What are Python's key benefits over other programming languages?
Easy to learn and use
Extensive libraries and frameworks
Versatile for various applications
Active community and support