Python interview questions with answers
1. What is Python?
- Python is a high-level, interpreted programming language known for its simplicity and readability. It supports multiple programming paradigms, including procedural, object-oriented, and functional programming.
2. What are the key features of Python?
- Simple and easy to learn syntax: Python has a clean and readable syntax, making it easy for beginners to learn and use.
- Interpreted and dynamically typed: Python is an interpreted language, meaning code is executed line by line. It is dynamically typed, allowing variables to be assigned without specifying their type.
- High-level data structures: Python comes with built-in data structures like lists, dictionaries, sets, and tuples, making it suitable for handling complex data.
- Rich standard library: Python has a vast standard library with modules and packages for various tasks, from web development to scientific computing.
- Platform-independent: Python code can run on any platform with the Python interpreter installed, including Windows, macOS, and Linux.
3. What are the different data types in Python?
- Integer (int): Represents whole numbers.
- Float (float): Represents floating-point numbers.
- String (str): Represents sequences of characters.
- Boolean (bool): Represents True or False values.
- List: Ordered and mutable collection of items.
- Tuple: Ordered and immutable collection of items.
- Dictionary (dict): Unordered collection of key-value pairs.
- Set: Unordered collection of unique items.
4. What is the difference between list and tuple in Python?
- List:
- Mutable: Elements can be added, removed, or modified.
- Defined using square brackets
[ ]
. - Slower than tuples due to dynamic resizing.
- Tuple:
- Immutable: Elements cannot be changed after creation.
- Defined using parentheses
( )
. - Faster than lists due to fixed size.
5. How do you create a function in Python?
- Functions in Python are created using the
def
keyword followed by the function name and parameters. For example:pythondef greet(name): return f"Hello, {name}!"
6. What is a lambda function in Python?
- A lambda function is a small, anonymous function defined using the
lambda
keyword. It can take any number of arguments but can only have one expression. Lambda functions are commonly used as inline functions. For example:pythonadd = lambda x, y: x + y
7. How do you handle exceptions in Python?
- Exceptions in Python are handled using
try
,except
, and optionallyfinally
blocks. For example:pythontry: result = 10 / 0 except ZeroDivisionError: print("Division by zero!")
8. What are decorators in Python?
- Decorators are a powerful feature in Python that allow you to modify the behavior of functions or methods without changing their code. They are implemented using the
@decorator_name
syntax. For example:pythondef 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()
9. How do you read/write files in Python?
- Files in Python can be opened using the
open()
function, and data can be read or written using file object methods likeread()
,write()
,readline()
, andwritelines()
. For example:pythonwith open("example.txt", "r") as file: data = file.read()
10. What is the difference between ==
and is
in Python?
- The ==
operator checks for equality of values, whereas the is
operator checks for identity, i.e., whether two objects refer to the same memory location.
11. What is the purpose of __init__
method in Python classes?
- The __init__
method is a special method in Python classes that is called automatically when a new instance of the class is created. It is used to initialize the object's attributes.
12. How do you handle modules and packages in Python?
- Modules are Python files containing Python code, and packages are directories containing multiple modules. You can import modules or packages using the import
statement. For example:
python import math from mypackage import mymodule
These questions cover various aspects of Python and can help assess a candidate's understanding and proficiency in the language.
Comments
Post a Comment