Python Interview Questions 2026
Introduction to Python
Python is a high-level, interpreted programming language created by Guido van Rossum in 1991. It is known for its simplicity, readability, and versatility. Python has gained massive popularity due to its ease of use and vast ecosystem, making it a preferred choice for beginners and professionals alike.
Why is Python Popular?
- Easy to Learn & Use – Python’s syntax is clean and human-readable.
- Interpreted Language – No need for compilation, making development faster.
- Versatile – Used in web development, data science, machine learning, automation, and more.
- Strong Community Support – Extensive libraries and frameworks with continuous updates.
- Cross-Platform Compatibility – Runs on Windows, Mac, and Linux.
Key Features of Python
Python offers several unique features that make it a powerful programming language:
- Dynamically Typed – No need to declare variable types.
- Object-Oriented & Functional – Supports multiple programming paradigms.
- Extensive Standard Library – Comes with built-in modules for file handling, networking, and more.
- High-Level Language – Focus on logic rather than complex syntax.
- Scalability – Used in small scripts to large enterprise applications.
Why Learn Python?
Python is one of the most in-demand programming languages. Here’s why you should learn it:
- Beginner-Friendly – Simple syntax makes it easy to start coding.
- High Demand in Industry – Used in data science, AI, web development, and automation.
- Job Opportunities – Python developers are in high demand with lucrative salaries.
- Large Ecosystem – Thousands of third-party libraries for various applications.
- Great for Rapid Prototyping – Quickly build and test applications.
Python Use Cases & Applications
Python is widely used across different industries:
1. Web Development
- Frameworks: Django, Flask, FastAPI
- Used for building dynamic websites, APIs, and backend services.
2. Data Science & Machine Learning
- Libraries: NumPy, Pandas, Matplotlib, Scikit-Learn, TensorFlow, PyTorch
- Used for data analysis, visualization, and AI model development.
3. Automation & Scripting
- Automate repetitive tasks using Selenium, BeautifulSoup, Requests.
4. Cybersecurity & Ethical Hacking
- Used for penetration testing, vulnerability scanning, and network security.
5. Game Development
- Game engines like Pygame are used for developing simple to complex games.
6. Internet of Things (IoT)
- Python is used to program microcontrollers like Raspberry Pi, Arduino.
7. GUI Development
- Build desktop applications using Tkinter, PyQt, Kivy.
Python Learning Resources
Learning Python doesn't have to be overwhelming. Here are some carefully selected resources to help you get started:
Official Documentation
- Python.org Official Documentation : The authoritative reference for Python
Best Book for Beginners
- Python Crash Course by Eric Matthes : A hands-on, project-based introduction to programming that's perfect for beginners
Top YouTube Tutorial
- Corey Schafer's Python Beginners Tutorial : Clear explanations and practical examples in bite-sized videos
Community Support
- Lets Code : A friendly community where you can ask questions and get help
Quick Reference
- Python Cheat Sheet : Handy reference for Python syntax and common operations
Python Interview Questions and Answers (2026)
Python Fundamentals
1. What is Python?
Python is a high-level, general-purpose, interpreted programming language created by Guido van Rossum in 1991. It emphasizes code readability with clean syntax and supports multiple programming paradigms — procedural, object-oriented, and functional. Python is widely used in web development, data science, AI/ML, automation, and scripting.
2. What are the benefits of using Python?
- Simple and readable syntax — easy to learn and write
- Interpreted — no compilation step, faster development cycle
- Dynamically typed — no need to declare variable types
- Vast standard library and third-party ecosystem (pip packages)
- Cross-platform — runs on Windows, Linux, macOS
- Strong community support
- Ideal for rapid prototyping and scripting
3. Is Python a compiled or an interpreted language?
Python is an interpreted language. The source code is executed line-by-line by the Python interpreter at runtime. However, Python first compiles .py files into bytecode (.pyc) internally, which is then executed by the Python Virtual Machine (PVM). This compilation is transparent to the user.
4. How is Python interpreted?
When you run a Python script:
- The source code (
.py) is compiled into bytecode (.pyc) by CPython. - The bytecode is executed by the Python Virtual Machine (PVM).
- The PVM interprets each bytecode instruction one at a time.
This is why Python is called "interpreted" even though there's an internal compilation step.
5. What is a dynamically typed language?
In a dynamically typed language, variable types are determined at runtime, not at compile time. You don't need to declare the type of a variable — it is inferred automatically.
x = 10 # x is int
x = "hello" # x is now str — no error
Python, JavaScript, and Ruby are dynamically typed. C, Java, and Go are statically typed.
6. What is PEP 8 and why is it important?
PEP 8 (Python Enhancement Proposal 8) is the official style guide for writing Python code. It defines conventions for indentation (4 spaces), line length (79 chars), naming conventions (snake_case for variables, PascalCase for classes), spacing, and documentation. Following PEP 8 makes code consistent, readable, and maintainable across teams.
7. What does the # symbol do in Python?
# marks the start of a single-line comment. Everything after # on that line is ignored by the Python interpreter.
# This is a comment
x = 10 # inline comment
For multi-line comments, triple quotes (""" or ''') are used (technically a string literal, not a comment).
8. Is indentation required in Python?
Yes — indentation is mandatory in Python. Unlike languages that use {} to define blocks, Python uses consistent indentation (typically 4 spaces) to group statements into blocks (functions, loops, conditionals, classes). Incorrect indentation raises an IndentationError.
Data Types and Structures
1. What is the difference between Mutable and Immutable data types?
| Feature | Mutable | Immutable |
|---|---|---|
| Can be changed after creation | Yes | No |
| Examples | list, dict, set | int, float, str, tuple |
| Memory behavior | Modified in-place | New object created on change |
lst = [1, 2, 3]
lst[0] = 99 # valid — list is mutable
tup = (1, 2, 3)
tup[0] = 99 # TypeError — tuple is immutable
2. What are the built-in data types in Python?
- Numeric:
int,float,complex - Sequence:
str,list,tuple,range - Mapping:
dict - Set:
set,frozenset - Boolean:
bool - Binary:
bytes,bytearray,memoryview - None:
NoneType
3. What is the difference between a Set and Dictionary?
| Feature | Set | Dictionary |
|---|---|---|
| Stores | Unique values only | Key-value pairs |
| Ordered (Python 3.7+) | No | Yes |
| Access | Iteration only | By key |
| Syntax | {"{1, 2, 3}"} | {"{"a": 1, "b": 2}"} |
| Duplicates | Not allowed | Keys must be unique |
4. What are lists and tuples? What is the key difference?
- List — mutable, ordered, defined with
[]. Can be changed after creation. - Tuple — immutable, ordered, defined with
(). Cannot be changed after creation.
Use tuples for fixed data (coordinates, RGB values) and lists for data that changes. Tuples are slightly faster and can be used as dictionary keys.
5. What is the difference between Python Arrays and lists?
| Feature | List | Array (array module) |
|---|---|---|
| Element types | Mixed types | Same type only |
| Import needed | No | Yes (import array) |
| Performance | Slower for numeric ops | Faster for numeric ops |
| Use case | General purpose | Numeric data |
For numerical computing, NumPy arrays are preferred over both.
6. What are negative indexes and why are they used?
Negative indexes allow accessing elements from the end of a sequence. -1 is the last element, -2 is second-to-last, etc.
lst = [10, 20, 30, 40]
print(lst[-1]) # 40
print(lst[-2]) # 30
7. What is slicing in Python?
Slicing extracts a portion of a sequence using [start:stop:step].
lst = [0, 1, 2, 3, 4, 5]
print(lst[1:4]) # [1, 2, 3]
print(lst[::2]) # [0, 2, 4]
print(lst[::-1]) # [5, 4, 3, 2, 1, 0] (reverse)
Control Flow and Functions
1. What is pass in Python?
pass is a null statement — it does nothing. Used as a placeholder where syntax requires a statement but no action is needed yet.
def future_function():
pass # TODO: implement later
class EmptyClass:
pass
2. What is the difference between / and // in Python?
/— true division, always returns a float:7 / 2 = 3.5//— floor division, returns the integer part rounded down:7 // 2 = 3
print(7 / 2) # 3.5
print(7 // 2) # 3
print(-7 // 2) # -4 (rounds toward negative infinity)
3. What is break, continue, and pass?
- break — exits the loop immediately
- continue — skips the rest of the current iteration and moves to the next
- pass — does nothing, acts as a placeholder
for i in range(5):
if i == 2: continue # skip 2
if i == 4: break # stop at 4
print(i) # 0, 1, 3
4. Difference between for loop and while loop?
- for loop — iterates over a sequence (list, range, string). Best when the number of iterations is known.
- while loop — repeats as long as a condition is
True. Best when the number of iterations is unknown.
5. What is a lambda function?
A lambda is an anonymous, single-expression function defined with the lambda keyword.
square = lambda x: x ** 2
print(square(5)) # 25
# Used inline with map/filter
nums = [1, 2, 3, 4]
evens = list(filter(lambda x: x % 2 == 0, nums)) # [2, 4]
6. How are arguments passed — by value or by reference?
Python uses pass-by-object-reference (also called pass-by-assignment). The behavior depends on mutability:
- Immutable objects (int, str, tuple) — behave like pass-by-value; the original is unchanged.
- Mutable objects (list, dict) — behave like pass-by-reference; modifications inside the function affect the original.
7. What are *args and `kwargs`?**
*args— allows a function to accept any number of positional arguments as a tuple.**kwargs— allows a function to accept any number of keyword arguments as a dictionary.
def demo(*args, **kwargs):
print(args) # (1, 2, 3)
print(kwargs) # {'name': 'Alice', 'age': 25}
demo(1, 2, 3, name="Alice", age=25)
8. What is self in Python?
self refers to the current instance of a class. It is the first parameter of every instance method and allows access to the object's attributes and other methods. It is passed automatically by Python when you call a method on an object.
9. What is the main function in Python?
Python doesn't require a main() function, but the convention is:
def main():
print("Hello!")
if __name__ == "__main__":
main()
if __name__ == "__main__" ensures the block runs only when the script is executed directly, not when imported as a module.
Comprehensions and Generators
1. What is List Comprehension?
A concise way to create lists using a single line expression.
squares = [x**2 for x in range(5)] # [0, 1, 4, 9, 16]
evens = [x for x in range(10) if x%2==0] # [0, 2, 4, 6, 8]
2. What is Dictionary Comprehension?
Creates dictionaries in a single expression.
squares = {x: x**2 for x in range(5)}
# {0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
3. Is Tuple Comprehension possible?
Not directly. Using () with a comprehension-like syntax creates a generator expression, not a tuple.
gen = (x**2 for x in range(5)) # generator object
tup = tuple(x**2 for x in range(5)) # (0, 1, 4, 9, 16)
4. What are generators in Python?
Generators are functions that yield values one at a time using the yield keyword. They don't store all values in memory — values are produced lazily on demand. This makes them memory-efficient for large datasets.
def count_up(n):
for i in range(n):
yield i
for val in count_up(3):
print(val) # 0, 1, 2
5. What is the difference between yield and return?
| Feature | return | yield |
|---|---|---|
| Exits function | Yes, completely | Pauses and resumes |
| Returns | Single value | Value one at a time |
| State preserved | No | Yes |
| Creates | Regular function | Generator function |
6. What are iterators in Python?
An iterator is an object that implements __iter__() and __next__(). It allows traversal through a collection one element at a time. All generators are iterators. Lists, tuples, and dicts are iterables (they can produce an iterator via iter()), but they are not iterators themselves.
Scope and Namespaces
1. What is Variable Scope in Python?
Scope defines where a variable is accessible. Python follows the LEGB rule:
- Local — inside the current function
- Enclosing — in enclosing functions (closures)
- Global — at the module level
- Built-in — Python's built-in names (
len,print, etc.)
2. What is Scope Resolution in Python?
When Python encounters a variable name, it searches through LEGB scopes in order. The first match wins. Use global to modify a global variable inside a function, and nonlocal to modify an enclosing scope variable.
3. What are Python namespaces?
A namespace is a mapping from names to objects. Python has several:
- Built-in namespace — exists throughout the program lifetime
- Global namespace — per module, created when the module loads
- Local namespace — per function call, created when the function is called
4. What is PYTHONPATH?
PYTHONPATH is an environment variable that tells the Python interpreter where to look for modules and packages when importing. It contains a list of directory paths. Python searches these directories in addition to the default locations.
5. What is a docstring in Python?
A docstring is a string literal placed as the first statement in a module, function, class, or method. It documents what the code does and is accessible via __doc__ or help().
def add(a, b):
"""Returns the sum of a and b."""
return a + b
print(add.__doc__) # Returns the sum of a and b.
Memory Management
1. How is memory managed in Python?
Python manages memory through:
- Private heap — all Python objects are stored in a private heap managed by the Python memory manager
- Reference counting — each object tracks how many references point to it; when count reaches 0, memory is freed
- Garbage collector — handles circular references that reference counting can't resolve (
gcmodule) - Memory pools — small objects (integers, strings) are cached and reused
2. What is the difference between shallow copy and deep copy?
- Shallow copy — creates a new object but references the same nested objects. Changes to nested objects in the copy affect the original.
- Deep copy — creates a completely independent copy including all nested objects.
import copy
original = [[1, 2], [3, 4]]
shallow = copy.copy(original)
deep = copy.deepcopy(original)
original[0][0] = 99
print(shallow[0][0]) # 99 — affected!
print(deep[0][0]) # 1 — not affected
3. What is the purpose of the weakref module?
weakref allows creating weak references to objects. A weak reference does not increase the reference count, so the object can be garbage-collected even if a weak reference exists. Useful for caches and observer patterns where you don't want to prevent garbage collection.
4. What are Python's memory optimization techniques?
- Use generators instead of lists for large datasets
- Use
__slots__in classes to avoid__dict__overhead - Use
arrayor NumPy arrays for numeric data - Reuse objects with object pools
- Use
sys.getsizeof()to measure object memory
Object-Oriented Programming
1. What is __init__() and how does self play a role?
__init__() is the constructor method called automatically when a new object is created. self refers to the instance being created and is used to assign attributes to it.
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
p = Person("Alice", 25)
print(p.name) # Alice
2. How do you create a class in Python?
class Dog:
species = "Canis lupus" # class attribute
def __init__(self, name):
self.name = name # instance attribute
def bark(self):
return f"{self.name} says Woof!"
d = Dog("Rex")
print(d.bark()) # Rex says Woof!
3. What is encapsulation in Python?
Encapsulation bundles data (attributes) and methods that operate on the data into a single unit (class), and restricts direct access from outside.
public— accessible from anywhere (default)_protected— single underscore, convention to indicate internal use__private— double underscore, name-mangled (_ClassName__attr), not directly accessible
4. What are Access Specifiers in Python?
Python doesn't enforce strict access control but uses naming conventions:
name— public, accessible anywhere_name— protected, accessible within class and subclasses (by convention)__name— private, name-mangled to_ClassName__name, discourages external access
5. Does Python support multiple Inheritance?
Yes. Python supports multiple inheritance — a class can inherit from more than one parent class.
class A:
def greet(self): return "Hello from A"
class B:
def greet(self): return "Hello from B"
class C(A, B):
pass
c = C()
print(c.greet()) # "Hello from A" — MRO (Method Resolution Order)
Python uses the C3 linearization (MRO) to resolve method conflicts.
6. What is Polymorphism in Python?
Polymorphism allows different classes to be used through the same interface. Python supports:
- Method overriding — subclass redefines a parent method
- Duck typing — if it walks like a duck and quacks like a duck, it is a duck
class Cat:
def sound(self): return "Meow"
class Dog:
def sound(self): return "Woof"
for animal in [Cat(), Dog()]:
print(animal.sound()) # Works without type checks
7. What are decorators in Python?
A decorator is a function that takes another function and extends or modifies its behavior without changing its source code. Uses the @ syntax.
def logger(func):
def wrapper(*args, **kwargs):
print(f"Calling {func.__name__}")
return func(*args, **kwargs)
return wrapper
@logger
def add(a, b):
return a + b
add(2, 3) # prints "Calling add", returns 5
8. What are metaclasses in Python?
A metaclass is the "class of a class" — it defines how a class behaves. Just as objects are instances of classes, classes are instances of metaclasses. The default metaclass is type. Custom metaclasses are used for ORM frameworks, validation, and API enforcement.
class Meta(type):
def __new__(cls, name, bases, dct):
print(f"Creating class {name}")
return super().__new__(cls, name, bases, dct)
class MyClass(metaclass=Meta):
pass
Modules and Packages
1. What are modules and packages in Python?
- Module — a single
.pyfile containing Python code (functions, classes, variables). - Package — a directory containing an
__init__.pyfile and one or more modules.
import math # module
from os import path # specific name from module
import numpy as np # module with alias
2. What is PIP?
PIP (Pip Installs Packages) is Python's package manager used to install, upgrade, and remove third-party packages from PyPI (Python Package Index).
pip install requests
pip install numpy==1.24
pip list
pip uninstall flask
3. What are commonly used built-in modules?
os— operating system interface (file paths, env vars)sys— system-specific parameters and functionsmath— mathematical functionsdatetime— date and time handlingjson— JSON encoding/decodingre— regular expressionscollections— specialized container typesitertools— efficient iteration toolsfunctools— higher-order functionsrandom— random number generation
4. What is the difference between .py and .pyc files?
.py— source code file written in Python.pyc— compiled bytecode file generated by CPython, stored in__pycache__/. Speeds up loading on subsequent runs. If the.pyfile changes, the.pycis regenerated.
5. What is the use of help() and dir()?
help(obj)— displays the documentation string (docstring) for an objectdir(obj)— lists all attributes and methods of an object
import math
help(math.sqrt) # shows docstring
dir(math) # lists all names in the math module
File Handling
1. How to delete a file using Python?
Use the os module:
import os
if os.path.exists("file.txt"):
os.remove("file.txt")
print("File deleted")
else:
print("File not found")
Use shutil.rmtree("folder") to delete a directory and all its contents.
2. How to make a Python script executable on Unix?
- Add a shebang line at the top of the script:
#!/usr/bin/env python3
- Make it executable:
chmod +x script.py
./script.py
Advanced Features and Tools
1. What is the Global Interpreter Lock (GIL)?
The GIL is a mutex in CPython that allows only one thread to execute Python bytecode at a time, even on multi-core systems. This prevents race conditions in memory management but limits true CPU-bound parallelism with threads. For CPU-bound tasks, use multiprocessing instead of threading. I/O-bound tasks benefit from threading despite the GIL.
2. What is the difference between @classmethod, @staticmethod, and instance methods?
| Type | First Param | Access to | Use case |
|---|---|---|---|
| Instance method | self | instance + class | Normal methods |
| @classmethod | cls | class only | Factory methods, alternative constructors |
| @staticmethod | none | neither | Utility functions logically related to the class |
class MyClass:
count = 0
def instance_method(self): return self
@classmethod
def class_method(cls): return cls.count
@staticmethod
def static_method(): return "No access to class/instance"
3. What are Function Annotations in Python?
Function annotations provide optional type hints for parameters and return values. They don't affect runtime behavior but help IDEs, linters, and mypy for type checking.
def greet(name: str, age: int) -> str:
return f"Hello {name}, age {age}"
4. How do you debug a Python program?
- Use
print()statements (basic) - Use
pdb(built-in debugger):import pdb; pdb.set_trace() - Use
breakpoint()(Python 3.7+) — shortcut for pdb - IDE debuggers (VS Code, PyCharm) with breakpoints
- Use
loggingmodule for structured debug output - Use
tracebackmodule to print stack traces
5. Which sorting technique does sort() and sorted() use?
Python uses Timsort — a hybrid sorting algorithm derived from Merge Sort and Insertion Sort. It is stable (preserves relative order of equal elements) and has O(n log n) average/worst-case time complexity. It performs exceptionally well on real-world data that is partially sorted.
6. What is the difference between xrange and range?
In Python 2, range() returned a list while xrange() returned an iterator (memory-efficient). In Python 3, xrange was removed and range() now behaves like Python 2's xrange — it returns a lazy range object, not a list. Use list(range(n)) if you need an actual list.
7. What is monkey patching in Python?
Monkey patching is dynamically modifying a module, class, or function at runtime — replacing or extending existing behavior without changing the original source code.
class Dog:
def bark(self): return "Woof"
def new_bark(self): return "Meow?"
Dog.bark = new_bark # monkey patch
d = Dog()
print(d.bark()) # Meow?
Used in testing (mocking), bug fixes, and extending third-party libraries.
Data Processing and Special Features
1. What is the zip() function?
zip() combines multiple iterables element-by-element into tuples.
names = ["Alice", "Bob", "Charlie"]
scores = [90, 85, 92]
result = list(zip(names, scores))
# [("Alice", 90), ("Bob", 85), ("Charlie", 92)]
2. Explain split() and join() functions.
split(sep)— splits a string into a list at each separatorjoin(iterable)— joins elements of an iterable into a string with a separator
s = "hello world python"
words = s.split(" ") # ['hello', 'world', 'python']
joined = "-".join(words) # 'hello-world-python'
3. How to check if all characters in a string are alphanumeric?
s = "Hello123"
print(s.isalnum()) # True
s2 = "Hello 123"
print(s2.isalnum()) # False (space is not alphanumeric)
4. How can you generate random numbers?
import random
random.random() # float between 0.0 and 1.0
random.randint(1, 10) # integer between 1 and 10 inclusive
random.choice([1,2,3,4]) # random element from list
random.shuffle(lst) # shuffle list in place
random.sample(lst, 3) # 3 unique random elements
5. What are Pickling and Unpickling?
- Pickling — serializing a Python object into a byte stream (binary format) using the
picklemodule. Used to save objects to disk or send over a network. - Unpickling — deserializing the byte stream back into a Python object.
import pickle
data = {"name": "Alice", "age": 25}
# Pickle (serialize)
with open("data.pkl", "wb") as f:
pickle.dump(data, f)
# Unpickle (deserialize)
with open("data.pkl", "rb") as f:
loaded = pickle.load(f)
6. What is the difference between a coroutine and a normal function?
A normal function runs to completion and returns a value. A coroutine (defined with async def) can pause execution at await points and resume later, enabling non-blocking asynchronous code.
import asyncio
async def fetch():
await asyncio.sleep(1) # pauses here, doesn't block
return "done"
asyncio.run(fetch())
7. What is the Walrus Operator (:=)?
The walrus operator (Python 3.8+) assigns a value to a variable as part of an expression — useful inside conditions and comprehensions.
# Without walrus
data = input("Enter: ")
if len(data) > 5:
print(data)
# With walrus
if (n := len(data := input("Enter: "))) > 5:
print(f"Too long: {n} chars")
8. What is Python's match statement (Switch)?
Python 3.10+ introduced structural pattern matching with match/case, similar to switch statements.
command = "quit"
match command:
case "quit":
print("Quitting")
case "start":
print("Starting")
case _:
print("Unknown command")
NumPy
1. What is NumPy?
NumPy (Numerical Python) is a fundamental library for scientific computing in Python. It provides the ndarray (n-dimensional array) object and a large collection of mathematical functions to operate on arrays efficiently. It is the foundation for pandas, SciPy, TensorFlow, and most data science libraries.
2. How are NumPy arrays advantageous over Python lists?
| Feature | Python List | NumPy Array |
|---|---|---|
| Element types | Mixed | Homogeneous (same type) |
| Speed | Slower | Much faster (C-level operations) |
| Memory | More overhead | Compact, contiguous memory |
| Vectorized ops | Not supported | Built-in element-wise ops |
| Broadcasting | No | Yes |
3. How to create 1D, 2D, and 3D arrays?
import numpy as np
arr1d = np.array([1, 2, 3]) # 1D
arr2d = np.array([[1, 2], [3, 4]]) # 2D
arr3d = np.array([[[1,2],[3,4]],[[5,6],[7,8]]]) # 3D
# Using creation functions
np.zeros((3, 3)) # 3x3 zeros
np.ones((2, 4)) # 2x4 ones
np.arange(0, 10, 2) # [0, 2, 4, 6, 8]
np.linspace(0, 1, 5) # 5 evenly spaced values
4. How to find the shape of a NumPy array?
arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr.shape) # (2, 3) — 2 rows, 3 columns
print(arr.ndim) # 2 — number of dimensions
print(arr.size) # 6 — total number of elements
5. How to reverse a NumPy array?
arr = np.array([1, 2, 3, 4, 5])
reversed_arr = arr[::-1] # [5, 4, 3, 2, 1]
# For 2D array, reverse rows:
arr2d[::-1]
6. How to find the nearest value in a NumPy array?
import numpy as np
arr = np.array([10, 20, 30, 40, 50])
target = 27
nearest = arr[np.abs(arr - target).argmin()]
print(nearest) # 30
7. How to sort an array based on the Nth column?
arr = np.array([[3, 1], [1, 4], [2, 2]])
# Sort by column index 1 (second column)
sorted_arr = arr[arr[:, 1].argsort()]
# [[3, 1], [2, 2], [1, 4]]
8. How to read CSV data into a NumPy array?
import numpy as np
# Using numpy directly
arr = np.genfromtxt("data.csv", delimiter=",", skip_header=1)
# Or using loadtxt for simpler files
arr = np.loadtxt("data.csv", delimiter=",", skiprows=1)
Pandas
1. What is pandas?
Pandas is an open-source Python library for data manipulation and analysis. It provides two main data structures — Series (1D) and DataFrame (2D table) — and tools for reading, cleaning, transforming, and analyzing structured data. Built on top of NumPy.
2. What is a pandas DataFrame?
A DataFrame is a 2D labeled data structure with rows and columns, similar to a spreadsheet or SQL table. Each column is a Series. Columns can have different data types.
import pandas as pd
df = pd.DataFrame({
"name": ["Alice", "Bob"],
"age": [25, 30],
"score": [90.5, 85.0]
})
3. How to combine different pandas DataFrames?
# Concatenate (stack vertically or horizontally)
pd.concat([df1, df2], axis=0) # stack rows
pd.concat([df1, df2], axis=1) # stack columns
# Merge (SQL-style join)
pd.merge(df1, df2, on="id", how="inner") # inner join
pd.merge(df1, df2, on="id", how="left") # left join
# Join (index-based)
df1.join(df2, how="outer")
4. Can you create a Series from a dictionary?
import pandas as pd
data = {"a": 10, "b": 20, "c": 30}
s = pd.Series(data)
# a 10
# b 20
# c 30
5. How to identify and deal with missing values?
df.isnull() # boolean mask of NaN locations
df.isnull().sum() # count of NaNs per column
df.dropna() # remove rows with any NaN
df.dropna(axis=1) # remove columns with any NaN
df.fillna(0) # replace NaN with 0
df.fillna(df.mean()) # replace with column mean
df.interpolate() # interpolate missing values
6. What is reindexing in pandas?
Reindexing changes the row/column labels of a DataFrame. Missing labels get NaN by default.
df = pd.DataFrame({"a": [1, 2, 3]}, index=[0, 1, 2])
df_reindexed = df.reindex([0, 1, 2, 3, 4])
# Rows 3 and 4 will have NaN
7. How to add a new column to a DataFrame?
df["new_col"] = df["score"] * 2 # calculated column
df["status"] = "active" # constant value
df.insert(1, "rank", [1, 2, 3]) # insert at specific position
df = df.assign(grade=lambda x: x["score"] / 10) # using assign
8. How to delete indices, rows, and columns?
# Drop rows by label
df.drop([0, 1], axis=0)
# Drop columns by name
df.drop(["age", "score"], axis=1)
# Reset index
df.reset_index(drop=True)
# Set a column as index
df.set_index("name")
9. How to get items not common to both Series A and B?
import pandas as pd
A = pd.Series([1, 2, 3, 4, 5])
B = pd.Series([3, 4, 5, 6, 7])
# Items in A not in B, and items in B not in A
uncommon = A[~A.isin(B)]._append(B[~B.isin(A)])
# or using symmetric difference on sets
result = pd.Series(list(set(A) ^ set(B)))
10. Can pandas recognize dates when importing data?
Yes. Use parse_dates parameter in read_csv():
df = pd.read_csv("data.csv", parse_dates=["date_column"])
df = pd.read_csv("data.csv", parse_dates=True, index_col="date")
Pandas uses dateutil to automatically parse common date formats. For custom formats use pd.to_datetime(df["col"], format="%d-%m-%Y").
Struggling to Find a Job? Get Specific Batch Wise job Updates ✅ Check now