0% found this document useful (0 votes)
3 views4 pages

Python_Notes_with_Interview_Questions (1)

This document provides an overview of Python, covering its features, basics, functions, data structures, object-oriented programming, exception handling, file handling, and functional programming concepts. Each section includes code examples and interview questions with answers to reinforce understanding. It serves as a comprehensive guide for both learning Python and preparing for interviews.

Uploaded by

Saurabh Deshmukh
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)
3 views4 pages

Python_Notes_with_Interview_Questions (1)

This document provides an overview of Python, covering its features, basics, functions, data structures, object-oriented programming, exception handling, file handling, and functional programming concepts. Each section includes code examples and interview questions with answers to reinforce understanding. It serves as a comprehensive guide for both learning Python and preparing for interviews.

Uploaded by

Saurabh Deshmukh
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/ 4

Python Notes with Interview Questions

1. Introduction to Python
Python is a high-level, interpreted programming language known for its readability and
versatility.

Code Example:

print("Hello, World!")

Interview Question:
What are the key features of Python?

Answer:
Python is interpreted, dynamically typed, garbage-collected, supports multiple paradigms,
and has a large standard library.

2. Python Basics
Covers variables, data types, operators, input/output, and basic control structures.

Code Example:

x = 10
y = 20
print("Sum:", x + y)

Interview Question:
What is the difference between list and tuple in Python?

Answer:
Lists are mutable whereas tuples are immutable.

3. Functions and Modules


Functions in Python are defined using the def keyword. Modules are files containing Python
code.
Code Example:

def greet(name):
return f"Hello, {name}"

print(greet("Alice"))

Interview Question:
What is the difference between a module and a package in Python?

Answer:
A module is a single Python file, while a package is a collection of modules in directories
that include a __init__.py file.

4. Data Structures
Python provides built-in data structures such as lists, tuples, sets, and dictionaries. These
are highly optimized and easy to use.

Code Example:

my_list = [1, 2, 3, 4]
my_tuple = (1, 2, 3, 4)
my_set = {1, 2, 3, 4}
my_dict = {'a': 1, 'b': 2}

Interview Question:
How is a set different from a list in Python?

Answer:
A set is an unordered collection of unique elements, whereas a list is an ordered collection
that can contain duplicates.

5. Object-Oriented Programming (OOP)


Python supports OOP paradigms like encapsulation, inheritance, and polymorphism. Classes
are defined using the `class` keyword.

Code Example:

class Person:
def __init__(self, name):
self.name = name
def greet(self):
return f'Hello, {self.name}'

p = Person('John')
print(p.greet())

Interview Question:
What is inheritance in Python?

Answer:
Inheritance allows one class to inherit the attributes and methods of another class,
promoting code reusability.

6. Exception Handling
Python uses try-except blocks to handle exceptions gracefully and prevent program crashes.

Code Example:

try:
result = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero.")

Interview Question:
What is the purpose of finally block in exception handling?

Answer:
The finally block is executed regardless of whether an exception occurred, and is typically
used for cleanup actions.

7. File Handling
Python provides functions to read from and write to files. The `with` statement is used to
handle file operations safely.

Code Example:

with open('example.txt', 'w') as file:


file.write('Hello, File!')

Interview Question:
Why is the 'with' statement used when working with files?
Answer:
The 'with' statement ensures the file is properly closed after its suite finishes, even if an
exception is raised.

8. Lambda, Map, Filter, Reduce


Python supports functional programming features like lambda functions and higher-order
functions such as map, filter, and reduce.

Code Example:

from functools import reduce


nums = [1, 2, 3, 4]
squared = list(map(lambda x: x**2, nums))
even = list(filter(lambda x: x % 2 == 0, nums))
sum_all = reduce(lambda x, y: x + y, nums)

Interview Question:
What is a lambda function in Python?

Answer:
A lambda function is an anonymous function defined with the lambda keyword, often used
for short, throwaway functions.

You might also like