Python notes
Python notes
md 2025-05-11
Strings questions
Functions questions
1. Explain regular expressions in Python with meta characters and their usage.
Regular expressions (regex) allow complex pattern matching in strings. Python uses the re module for
regex operations. Common meta characters include:
2/9
pfp_theory_ques_3736.md 2025-05-11
def greet(name):
return "Hello, " + name
print(greet("Alice"))
2. Explain lambda functions and their use with map() and filter().
Lambda functions are anonymous and used for simple tasks. map() applies a function to all elements of
4/9
pfp_theory_ques_3736.md 2025-05-11
nums = [1, 2, 3]
squares = list(map(lambda x: x**2, nums))
evens = list(filter(lambda x: x%2 == 0, nums))
def factorial(n):
if n == 1: return 1
return n * factorial(n - 1)
It’s useful in tasks that can be broken down into similar subtasks.
add(b=3, a=2)
def greet(name="Guest"):
return "Hello, " + name
5/9
pfp_theory_ques_3736.md 2025-05-11
Variable-length arguments:
2. Discuss the use of lambda functions, map(), and filter() with examples.\
nums = [1, 2, 3, 4]
squares = list(map(lambda x: x**2, nums))
evens = list(filter(lambda x: x % 2 == 0, nums))
These tools are concise and efficient for functional programming in Python.
3. Explain the concept of modules in Python. How are built-in modules like datetime used?
A module is a file containing Python definitions and functions. Built-in modules provide essential tools.
The datetime module handles dates and times.
import datetime
now = datetime.datetime.now()
print(now.strftime("%Y-%m-%d %H:%M"))
You can also use from datetime import datetime. Custom modules are created by saving functions
in a .py file and importing them in other scripts. Modules promote reusability and modularity.
6/9
pfp_theory_ques_3736.md 2025-05-11
4. What is NumPy?
NumPy is a Python library used for numerical computations, providing support for arrays and matrices.
class Person:
def __init__(self, name):
self.name = name
def greet(self):
return "Hello, " + self.name
p1 = Person("Alice")
print(p1.greet())
7/9
pfp_theory_ques_3736.md 2025-05-11
plt.plot(x, y) # line
plt.bar(x, y) # bar
import numpy as np
arr = np.array([1, 2, 3])
arr2 = arr * 2
Multi-dimensional arrays
Vectorized operations
8/9
pfp_theory_ques_3736.md 2025-05-11
Broadcasting
Array slicing
import numpy as np
a = np.array([[1, 2], [3, 4]])
print(a.shape, a[0, 1])
Operations include arithmetic, statistical functions (np.mean, np.sum), reshaping, and matrix
multiplication.
import pandas as pd
s = pd.Series([10, 20, 30])
Pandas supports reading from files, filtering, aggregation, and more for data wrangling.
9/9