0% found this document useful (0 votes)
0 views5 pages

Python Interview Guide

This document provides a comprehensive overview of Python interview questions and answers, covering basic concepts such as Python's features, built-in data types, variable declaration, and memory management. It also includes explanations of lists, tuples, sets, conditional statements, exception handling, functions, decorators, and lambda functions. The document is structured to aid in preparing for Python interviews, with clear examples and definitions.

Uploaded by

sanjeetverma9572
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views5 pages

Python Interview Guide

This document provides a comprehensive overview of Python interview questions and answers, covering basic concepts such as Python's features, built-in data types, variable declaration, and memory management. It also includes explanations of lists, tuples, sets, conditional statements, exception handling, functions, decorators, and lambda functions. The document is structured to aid in preparing for Python interviews, with clear examples and definitions.

Uploaded by

sanjeetverma9572
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Python Interview Questions and Answers

📆 Basic: Laying the Foundation


1. What is Python, and its key features?
Python is a high-level, interpreted programming language known for its
simplicity and readability. It supports multiple programming paradigms,
including object-oriented, functional, and procedural. Python’s syntax is
clean and allows developers to write fewer lines of code compared to other
languages like Java or C++.
Key features:
 Interpreted and dynamically typed
 Easy to learn and read
 Extensive standard library
 Cross-platform compatibility
 Automatic memory management
 Support for third-party libraries

2. What are Python’s built-in data types?


Python has several built-in data types:
 Numeric types: int, float, complex
 Sequence types: str, list, tuple, range
 Set types: set, frozenset
 Mapping type: dict
 Boolean type: bool
 Binary types: bytes, bytearray, memoryview
 None type: NoneType

3. How do you declare and use variables?


Variables in Python are declared by assigning a value using the = operator.
Python does not require specifying the data type explicitly.
x = 10
name = "Alice"
price = 10.5

Variables can be reassigned to different types.


4. Explain the difference between a list, tuple, and set.
 List: Mutable, ordered, and allows duplicate elements. Defined with
square brackets [].
 Tuple: Immutable, ordered, and allows duplicates. Defined with
parentheses ().
 Set: Mutable, unordered, and does not allow duplicates. Defined with
curly braces {}.

5. How do you iterate over a list in Python?


You can iterate using a for loop:
my_list = [1, 2, 3]
for item in my_list:
print(item)

6. What are Python’s conditional statements, and how are they


used?
Conditional statements allow execution of code based on certain conditions.
x = 10
if x > 0:
print("Positive")
elif x < 0:
print("Negative")
else:
print("Zero")

7. How does Python handle memory management?


Python uses a private heap space and manages memory through:
 Reference counting
 Garbage collection for cyclic references
 Built-in memory allocators and deallocators

8. Explain the use of the len() function.


The len() function returns the number of items in an object such as a string,
list, tuple, or dictionary.
len("hello") # 5
len([1, 2, 3]) # 3

9. What is the difference between is and == in Python?


 ==: Compares values (equality).
 is: Compares identities (whether two objects point to the same
memory location).
a = [1, 2]
b = [1, 2]
a == b # True
a is b # False

10. How do you handle exceptions in Python?


Use try, except, finally blocks:
try:
result = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero")
finally:
print("Execution complete")

11. What are Python functions, and how do you define them?
Functions are blocks of reusable code. Defined using the def keyword:
def greet(name):
return f"Hello, {name}"

12. What is the difference between *args and **kwargs?


*args: Allows variable number of positional arguments.
**kwargs: Allows variable number of keyword arguments.
def func(*args, **kwargs):
print(args)
print(kwargs)
13. How is Python’s for loop different from other programming
languages?
Python’s for loop iterates directly over items in a sequence (e.g., list, string),
not index-based unless using range() or enumerate().

14. Explain the purpose of the range() function.


Generates a sequence of numbers, commonly used in loops.
for i in range(3):
print(i) # 0 1 2

15. How do you import and use modules?


Use the import statement:
import math
print(math.sqrt(16)) # 4.0

16. What are Python decorators, and how do they work?


Decorators are functions that modify the behavior of other functions.
def decorator(func):
def wrapper():
print("Before")
func()
print("After")
return wrapper

@decorator
def say_hello():
print("Hello")

17. How do you reverse a string in Python?


Use slicing:
s = "hello"
print(s[::-1]) # "olleh"
18. How do you check if an element exists in a list?
Use the in keyword:
if 5 in [1, 2, 5]:
print("Found")

19. What is a lambda function? Provide an example.


A lambda is an anonymous function:
square = lambda x: x * x
print(square(5)) # 25

[Intermediate and Advanced sections to be continued in the next part due to


length. Would you like to proceed with those now?]

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy