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

Python Interview Notes

The document provides notes on key Python concepts relevant for a data analyst job interview, including mutable vs immutable types, *args and **kwargs, list comprehension, lambda functions, dictionary methods, exception handling, and falsy values. Each section includes definitions, code examples, and interview questions with answers. It emphasizes the differences between similar concepts and their practical applications in Python programming.
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 views3 pages

Python Interview Notes

The document provides notes on key Python concepts relevant for a data analyst job interview, including mutable vs immutable types, *args and **kwargs, list comprehension, lambda functions, dictionary methods, exception handling, and falsy values. Each section includes definitions, code examples, and interview questions with answers. It emphasizes the differences between similar concepts and their practical applications in Python programming.
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

Python Interview Notes — DA Job

Sprint
1. Mutable vs Immutable
Concept: Mutable objects can be changed after creation. Immutable objects cannot.

Quick Reference:
Type Mutable?
List ✅ Yes
Dictionary ✅ Yes
Set ✅ Yes
Tuple ❌ No
String ❌ No
Integer / Float ❌ No

Code Example:
my_list = [1, 2, 3]
my_list[0] = 99 # ✅ Works — List is mutable
my_tuple = (1, 2, 3)
my_tuple[0] = 99 # ❌ TypeError — Tuple is immutable

⚠️Tricky Interview Point:


x = [1,2,3] then y = x — y is NOT a copy. Both point to same memory. Changing y changes x too.
[] == [] → True (same values)
[] is [] → False (different memory locations)

Interview Q: List aur Tuple mein kya difference hai?


Answer: List mutable hai — elements change, add, remove ho sakte hain. Tuple immutable hai — ek baar bana,
change nahi hota. Tuple faster hai aur fixed data ke liye use hota hai (e.g. coordinates).

2. *args and **kwargs


Concept: Use when you don't know how many arguments will be passed to a function.

Kya leta hai Andar banta hai


*args 1, 2, 3 (positional) Tuple (1, 2, 3)
**kwargs a=1, b=2 (keyword) Dict {'a':1,'b':2}

Code Example:
def student(*args, **kwargs):
print('Marks:', args)
print('Details:', kwargs)
student(85, 90, 78, name='Shruti', city='Pune')
# Marks: (85, 90, 78)
# Details: {'name': 'Shruti', 'city': 'Pune'}

⚠️Order Rule:
def func(*args, **kwargs) → *args ALWAYS before **kwargs

Interview Q: *args aur **kwargs mein kya difference hai?


Answer: *args multiple positional arguments ko tuple mein collect karta hai. **kwargs multiple keyword
arguments ko dictionary mein collect karta hai. Dono tab use hote hain jab pata na ho kitne arguments aayenge.

3. List Comprehension
Concept: Short way to create a list using a single line of code.

Syntax:
[expression for item in iterable if condition]

Examples:
evens = [i for i in range(1,11) if i % 2 == 0]
# [2, 4, 6, 8, 10]
squares = [i**2 for i in range(1,6)]
# [1, 4, 9, 16, 25]
words = [f for f in ['apple','banana','cherry'] if len(f) > 5]
# ['banana', 'cherry']

Interview Q: List comprehension vs normal for loop — kab kaunsa?


Answer: Simple filtering ya transformation ke liye list comprehension better — concise aur faster. Complex
multi-step logic ke liye normal for loop zyada readable hota hai.

4. Lambda Functions
Concept: Anonymous (nameless) function — single line mein likha jaata hai.

Syntax:
lambda arguments : expression

Examples:
square = lambda x: x**2
add = lambda a, b: a + b
check = lambda x: 'Positive' if x > 0 else ('Negative' if x < 0 else
'Zero')

Real-world Pandas use:


df['new_col'] = df['salary'].apply(lambda x: x * 1.1)

Interview Q: Lambda aur normal function mein kya difference hai?


Answer: Lambda ek single expression wala anonymous function hai — sirf ek line. Normal function def
keyword se banta hai, multiple lines aur complex logic rakh sakta hai. Pandas mein .apply() ke saath lambda
daily use hota hai.

5. Dictionary Methods
3 Key Methods — Interview Favourite:

Method Returns Use


.keys() Sirf keys Column names check
.values() Sirf values Data access
.items() Key-value pairs Loop karna ho dono se

Code Example:
d = {'name': 'Shruti', 'city': 'Pune', 'age': 25}
for key, value in [Link]():
print(key, ':', value)
6. Exception Handling
Concept: Errors ko gracefully handle karo — program crash na ho.

Syntax:
try:
# risky code
except ErrorType:
# error handle karo
finally:
# hamesha chalta hai

Common Errors:
Error Kab aata hai
ZeroDivisionError 0 se divide karo
TypeError Wrong data type
ValueError Sahi type, galat value
IndexError List index exist na kare
KeyError Dict mein key na ho

⚠️Key Point:
Ek baar specific except catch ho jaaye — baaki except blocks skip ho jaate hain. finally HAMESHA chalta hai,
error ho ya na ho.

7. Falsy Values in Python


Concept: Yeh values if condition mein False maani jaati hain.

0, '', [], {}, (), None, False

x = 0
if x:
print('True') # nahi chalega
else:
print('False') # yeh chalega

Interview Q: Python mein kaunsi values False treat hoti hain?


Answer: 0, empty string, empty list, empty dict, empty tuple, None aur False — yeh sab Falsy hain. Baaki sab
Truthy.

You might also like