DataFrame Merging in Python
Solve 1 (a)
Q1 (a): What is the role of a DataFrame? How can DataFrames be merged?
Role of a DataFrame in Python:
A DataFrame is a two-dimensional, size-mutable, and heterogeneous tabular data structure
provided by the Pandas library in Python. It is one of the most commonly used data
structures for data analysis and manipulation.
Key Roles:
Represents data in rows and columns, similar to a spreadsheet or SQL table.
Allows label-based indexing (by column names) and position-based indexing (by
row/column indices).
Facilitates easy data cleaning, filtering, transformation, and aggregation.
Supports operations like grouping, sorting, joining, and merging.
Enables import/export of data from various file formats like CSV, Excel, SQL, JSON, etc.
1/49
How DataFrames Can Be Merged:
DataFrames can be merged using the merge() function in Pandas, which works similarly to
SQL joins.
Syntax:
python
pd.merge(df1, df2, how='inner', on='common_column')
Common merge types:
Inner Join ( how='inner' ): Returns only the rows with matching keys in both
DataFrames.
Outer Join ( how='outer' ): Returns all rows from both DataFrames, with NaN where no
match is found.
Left Join ( how='left' ): All rows from the left DataFrame and matching rows from the
right.
Right Join ( how='right' ): All rows from the right DataFrame and matching rows from
the left.
Example:
python
import pandas as pd
df1 = pd.DataFrame({'ID': [1, 2, 3], 'Name': ['Alice', 'Bob', 'Charlie']})
df2 = pd.DataFrame({'ID': [1, 2, 4], 'Score': [85, 90, 88]})
merged_df = pd.merge(df1, df2, how='inner', on='ID')
print(merged_df)
Output:
nginx
ID Name Score
0 1 Alice 85
2/49
1 2 Bob 90
Let me know if you’d like the answers to the remaining parts of Q1 too.
1(d)
Q1 (d): Write a short note on different methods to read data from a file in Python.
Python provides several ways to read data from a file. Here are the most common methods:
1. Using open() with read()
Reads the entire content of the file as a single string.
python
file = open("example.txt", "r")
content = file.read()
print(content)
file.close()
2. Using readline()
Reads one line at a time from the file.
python
file = open("example.txt", "r")
line1 = file.readline()
line2 = file.readline()
file.close()
3/49
3. Using readlines()
Reads all lines and returns them as a list of strings.
python
file = open("example.txt", "r")
lines = file.readlines()
file.close()
4. Using with statement (Context Manager)
Automatically closes the file after the block ends.
python
with open("example.txt", "r") as file:
content = file.read()
print(content)
5. Reading CSV files using pandas
python
import pandas as pd
df = pd.read_csv("data.csv")
print(df.head())
6. Reading JSON files
python
4/49
import json
with open("data.json", "r") as file:
data = json.load(file)
print(data)
Summary:
Method Use Case
read() Read entire file as string
readline() Read one line at a time
readlines() Read file into list of lines
with open() Best practice for file handling
pandas.read_csv() Reading structured tabular data
json.load() Reading JSON structured data
Let me know if you want explanations for 1(e) or any other part.
2(b)
Q2 (b): Explain the different numeric, assignment, and augmented assignment operators
used in Python.
[3 Marks]
1. Numeric Operators (Arithmetic Operators):
Used for basic arithmetic calculations.
5/49
Operator Description Example Result
+ Addition 5 + 3 8
- Subtraction 5 - 3 2
* Multiplication 5 * 3 15
/ Division (float) 5 / 2 2.5
// Floor Division 5 // 2 2
% Modulus 5 % 2 1
** Exponentiation 2 ** 3 8
2. Assignment Operator:
Used to assign a value to a variable.
Operator Description Example
= Assigns x = 10
3. Augmented Assignment Operators:
Used to modify the value of a variable and assign the result in one step.
Operator Equivalent To Example Result
+= x = x + y x += 3 Adds 3 to x
-= x = x - y x -= 2 Subtracts 2 from x
*= x = x * y x *= 2 Multiplies x by 2
/= x = x / y x /= 2 Divides x by 2
//= x = x // y x //= 2 Floor division
6/49
Operator Equivalent To Example Result
%= x = x % y x %= 2 Modulus
**= x = x ** y x **= 2 Exponentiation
Example in Code:
python
x = 10
x += 5 # x becomes 15
x *= 2 # x becomes 30
Let me know if you’d like help with 2(c) or any other question.
2(c)
Q2 (c): Explain how operator precedence is used in evaluating expressions.
[3.5 Marks]
✅ What is Operator Precedence?
Operator precedence defines the order in which operators are evaluated in expressions.
Just like in mathematics, certain operations are performed before others (e.g., multiplication
before addition).
In Python, if an expression contains multiple operators, the operator with higher
precedence is evaluated first, unless parentheses are used to change the order.
✅ Example Without Parentheses:
7/49
python
result = 10 + 5 * 2
Here, * has higher precedence than + , so:
First: 5 * 2 → 10
Then: 10 + 10 → 20
So, result = 20
✅ Example With Parentheses:
python
result = (10 + 5) * 2
Now parentheses override default precedence:
First: 10 + 5 → 15
Then: 15 * 2 → 30
So, result = 30
✅ Operator Precedence Order (Top to Bottom):
Precedence Operators Description
Highest () Parentheses
** Exponentiation
+x , -x , ~x Unary plus, minus, bitwise NOT
* , / , // , % Multiplication, division etc.
+, - Addition, subtraction
8/49
Precedence Operators Description
<< , >> Bitwise shift
& Bitwise AND
^ Bitwise XOR
` `
== , != , > , < , >= , <= Comparisons
Lowest = , += , -= , etc. Assignment
✅ Key Tip:
Use parentheses to make your expressions clear and avoid mistakes due to precedence
rules.
✅ Mini Example:
python
a = 5 + 2 * 3 ** 2
# 3 ** 2 = 9
# 2 * 9 = 18
# 5 + 18 = 23
So, a = 23
Let me know if you’d like help with Q2(a) or the next question.
3(a)
9/49
Q3 (a): What are the commonly used operators in Python? Explain operator overloading
with the help of an example.
[6 Marks]
✅ Commonly Used Operators in Python:
Python provides several types of operators:
Operator Type Examples Description
Arithmetic + , - , * , / , % , ** , // Used for mathematical operations
Comparison == , != , > , < , >= , <= Compare values
Assignment = , += , -= , *= , /= Assign or modify values
Logical and , or , not Combine Boolean expressions
Bitwise & ,` , ^ , ~ , << , >>`
Membership in , not in Test presence in a sequence
Identity is , is not Compare memory locations of objects
✅ What is Operator Overloading?
Operator overloading allows the same operator to have different meanings depending on
the operands involved. Python internally uses special methods (also called magic methods)
to achieve this.
✅ Example: Operator Overloading in a Class
python
10/49
class Book:
def __init__(self, pages):
self.pages = pages
def __add__(self, other):
return self.pages + other.pages
b1 = Book(150)
b2 = Book(200)
print("Total pages:", b1 + b2)
Output:
yaml
Total pages: 350
Here, + is overloaded using the __add__() method.
Instead of adding objects directly, it adds their pages attributes.
✅ Real-life Examples of Overloading in Python:
+ for integers: 2 + 3 = 5
+ for strings: "hello" + " world" = "hello world"
+ for lists: [1, 2] + [3, 4] = [1, 2, 3, 4]
This is built-in operator overloading.
Let me know if you'd like 3(b) or another question!
5(a)
Q5 (a): What are functions in Python? Differentiate between built-in and user-defined
functions with suitable examples.
11/49
[5 Marks]
✅ What are Functions in Python?
A function in Python is a block of reusable code that performs a specific task. It improves
code modularity, reusability, and clarity.
Functions are defined using the def keyword and can take parameters and return values.
✅ Types of Functions in Python:
1. Built-in Functions
2. User-defined Functions
🔹 1. Built-in Functions:
These are predefined functions provided by Python. You can use them directly without
defining them.
Examples:
python
print("Hello") # Prints a string
len("Python") # Returns length → 6
max([1, 2, 3]) # Returns maximum → 3
type(10) # Returns data type → <class 'int'>
🔹 2. User-defined Functions:
Functions created by the user using the def keyword.
Example:
12/49
python
def greet(name):
print("Hello,", name)
greet("Alice") # Output: Hello, Alice
✅ Comparison Table:
Feature Built-in Functions User-defined Functions
Defined by Python Programmer
Usage Direct use Must define before use
Examples print() , len() , sum() , type() greet() , add_numbers() , area()
Customization Cannot modify Can customize logic as needed
Availability Always available Available only after definition
✅ Conclusion:
Functions help make code organized and reusable.
Python provides many built-in functions, and we can also create user-defined functions
for custom logic.
Let me know if you’d like the answer to 5(b) too!
Define the following: find(), isalnum), zfill), strip(), split) and
rindex).
Here's a clear and concise explanation of the following Python string methods:
13/49
✅ 1. find()
Definition:
Searches for a substring within a string and returns the lowest index where it is found.
Returns -1 if not found.
Syntax: string.find(substring[, start[, end]])
Example:
python
text = "hello world"
print(text.find("o")) # Output: 4
print(text.find("x")) # Output: -1
✅ 2. isalnum()
Definition:
Returns True if all characters in the string are alphanumeric (i.e., letters or numbers), and
False otherwise.
Example:
python
print("abc123".isalnum()) # Output: True
print("abc 123".isalnum()) # Output: False (space is not alphanumeric)
✅ 3. zfill()
Definition:
Pads the string with zeros ( 0 ) on the left until it reaches the specified length.
Syntax: string.zfill(width)
Example:
python
14/49
print("42".zfill(5)) # Output: '00042'
print("-42".zfill(5)) # Output: '-0042'
✅ 4. strip()
Definition:
Removes leading and trailing whitespaces (or specified characters) from the string.
Syntax: string.strip([chars])
Example:
python
print(" hello ".strip()) # Output: 'hello'
print("$$money$$".strip('$')) # Output: 'money'
✅ 5. split()
Definition:
Splits the string into a list of substrings based on a delimiter (default is whitespace).
Syntax: string.split([separator[, maxsplit]])
Example:
python
text = "one two three"
print(text.split()) # Output: ['one', 'two', 'three']
data = "a,b,c"
print(data.split(',')) # Output: ['a', 'b', 'c']
✅ 6. rindex()
15/49
Definition:
Returns the highest index (last occurrence) of a substring. Raises a ValueError if the
substring is not found.
Syntax: string.rindex(substring[, start[, end]])
Example:
python
text = "banana"
print(text.rindex("a")) # Output: 5
# print(text.rindex("x")) # Raises ValueError
Let me know if you'd like a printable version or more examples!
Write the definition of a function Reverse(X). WAP to display
the elements in reverse order such that each displayed
element is twice of the original element (element *2) of the
List X.
Here’s the definition of the function Reverse(X) as per your requirement:
✅ Function Requirements:
Input: List X
Task:
1. Traverse the list in reverse order
2. Multiply each element by 2
3. Print the result
✅ Python Code:
16/49
python
def Reverse(X):
for i in range(len(X) - 1, -1, -1):
print(X[i] * 2)
# Example usage:
X = [1, 3, 5, 7]
Reverse(X)
✅ Output:
14
10
6
2
This prints:
7×2 = 14
5×2 = 10
3×2 = 6
1×2 = 2
Each in reverse order.
Let me know if you'd like to modify it to return a new list instead of printing!
reverse function
Here’s a more flexible version of the Reverse() function that returns the reversed list with
each element doubled, instead of just printing them:
17/49
✅ Revised Reverse() Function (Returns List)
python
def Reverse(X):
reversed_list = []
for i in range(len(X) - 1, -1, -1):
reversed_list.append(X[i] * 2)
return reversed_list
# Example usage:
X = [2, 4, 6, 8]
result = Reverse(X)
print("Reversed and Doubled List:", result)
✅ Output:
less
Reversed and Doubled List: [16, 12, 8, 4]
✅ One-line Version using List Comprehension:
python
def Reverse(X):
return [x * 2 for x in reversed(X)]
Let me know if you want it to modify the original list, print each value line by line, or
anything else!
default reverse function
18/49
✅ Python's Default reverse() Function:
Python provides a built-in list method called reverse() which reverses a list in place
(modifies the original list). It does not return a new list.
🔹 Syntax:
python
list.reverse()
🔹 Example:
python
X = [10, 20, 30, 40]
X.reverse()
print(X)
Output:
csharp
[40, 30, 20, 10]
⚠️ Important Notes:
reverse() changes the original list.
It returns None .
✅ If You Want a Reversed Copy Without Changing Original:
19/49
Use slicing:
python
X = [1, 2, 3, 4]
Y = X[::-1]
print(Y) # Output: [4, 3, 2, 1]
Or use the built-in reversed() function:
python
X = [1, 2, 3, 4]
Y = list(reversed(X))
print(Y) # Output: [4, 3, 2, 1]
Let me know if you'd like to use this with doubling elements too!
use this for
WAP to display the elements in reverse order such that
each displayed element is twice of the original element
(element *2) of the List X.
Sure! Here's how you can use the default reverse() method to solve the problem:
✅ "Write a program to display the elements in reverse order such that each displayed
element is twice of the original element of the list X."
✅ Solution using reverse() :
python
def Reverse(X):
X.reverse() # Reverses the list in-place
for element in X:
print(element * 2)
20/49
# Example usage
X = [2, 4, 6, 8]
Reverse(X)
✅ Output:
16
12
8
4
X = [2, 4, 6, 8] → reversed: [8, 6, 4, 2]
Then doubled: 16, 12, 8, 4
Let me know if you want to keep the original list unchanged or store the result in another
list instead of printing!
Write a function mul () which accepts list L, odd_L, even_L as
its parameters. Transfer all even values of list L to even_L
and all odd values of L to odd_L.
Here’s how you can write the function mul() that takes a list L and separates its elements
into two lists: even_L and odd_L based on whether they're even or odd.
✅ Python Function: mul()
python
def mul(L, odd_L, even_L):
for item in L:
if item % 2 == 0:
21/49
even_L.append(item)
else:
odd_L.append(item)
✅ Example Usage:
python
L = [10, 15, 20, 25, 30, 35]
odd_L = []
even_L = []
mul(L, odd_L, even_L)
print("Odd List:", odd_L)
print("Even List:", even_L)
✅ Output:
less
Odd List: [15, 25, 35]
Even List: [10, 20, 30]
Let me know if you want a version that returns the two lists instead of modifying them in-
place.
Write a program that accepts filename as an input from the
user. Open the file and count the number of times a
character appears in the file.
Here’s a Python program that:
Accepts a filename from the user,
22/49
Opens the file,
Counts the occurrence of each character, and
Displays the frequency of each character.
✅ Python Program:
python
# Accept filename from user
filename = input("Enter the filename: ")
# Dictionary to store character counts
char_count = {}
try:
# Open and read the file
with open(filename, 'r') as file:
content = file.read()
# Count each character
for char in content:
if char in char_count:
char_count[char] += 1
else:
char_count[char] = 1
# Display character counts
for char, count in char_count.items():
# To make output more readable
if char == '\n':
display_char = '\\n'
elif char == '\t':
display_char = '\\t'
elif char == ' ':
display_char = '[space]'
else:
display_char = char
print(f"'{display_char}' appears {count} times.")
23/49
except FileNotFoundError:
print("Error: File not found.")
✅ Sample Output:
bash
Enter the filename: example.txt
'H' appears 2 times.
'e' appears 5 times.
'l' appears 4 times.
'o' appears 2 times.
[space] appears 1 times.
'w' appears 1 times.
'r' appears 1 times.
'd' appears 1 times.
'\n' appears 1 times.
Let me know if you want to count a specific character only, or make it case-insensitive.
What are files? Why do we need them? What are different
access modes in which you can open file?
✅ What are Files in Python?
A file is a named location on disk used to store data permanently.
It allows data to be saved and retrieved even after the program ends, unlike variables that
are lost when the program stops.
✅ Why Do We Need Files?
We need files to:
Store data permanently
24/49
Exchange data between programs or users
Read or write large data that doesn’t fit in memory
Keep logs, reports, user data, or program output
Example use cases:
Saving user records (like in a student database)
Reading configuration files
Logging errors
✅ File Access Modes in Python
When opening a file using open(filename, mode) , the mode tells Python how you want to
access the file:
Mode Description
'r' Read only (default). File must exist.
'w' Write only. Creates a new file or overwrites existing one.
'a' Append mode. Adds content to end of file if it exists.
'r+' Read and Write. File must exist.
'w+' Write and Read. Overwrites file or creates new.
'a+' Append and Read. Creates file if not exists.
'rb' Read in binary mode (e.g., for images, PDFs).
'wb' Write in binary mode.
'ab' Append in binary mode.
✅ Example:
25/49
python
f = open("data.txt", "r") # Open for reading
content = f.read()
f.close()
Let me know if you'd like a diagram or examples for each mode!
What is directory access? How do you walk through a
directory tree?
✅ What is Directory Access?
Directory access means interacting with folders (directories) in a file system using Python —
to:
List files/folders
Create or delete directories
Walk through folders and subfolders
Access files inside those folders
This is done using Python's built-in os and os.path modules (or pathlib in newer code).
✅ How to Walk Through a Directory Tree
To walk through a directory tree (i.e., access all subfolders and files recursively), you can use:
python
import os
for folder_name, subfolders, filenames in os.walk("path_to_directory"):
print(f"Folder: {folder_name}")
print(f"Subfolders: {subfolders}")
print(f"Files: {filenames}")
print()
26/49
✅ Example:
Assume a directory structure:
my_data/
├── file1.txt
├── file2.txt
└── subdir/
└── file3.txt
Code:
python
import os
for folder, subfolders, files in os.walk("my_data"):
print("Current folder:", folder)
print("Subfolders:", subfolders)
print("Files:", files)
print()
✅ Output:
less
Current folder: my_data
Subfolders: ['subdir']
Files: ['file1.txt', 'file2.txt']
Current folder: my_data/subdir
Subfolders: []
Files: ['file3.txt']
27/49
✅ Summary of os.walk() :
Element Description
folder Current directory being walked
subfolders List of subdirectories in folder
files List of files in folder
Let me know if you want to perform actions like file filtering, searching, or copying during
traversal.
Write a python program to merge two files and then
change the content of this file to upper case letters.
Here is a simple Python program that does the following:
1. Accepts two filenames from the user,
2. Merges their contents into a third file,
3. Converts the merged content into UPPERCASE, and
4. Writes the final result back to the third file.
✅ Python Program to Merge and Convert to Uppercase
python
# Step 1: Get filenames from user
file1 = input("Enter first filename: ")
file2 = input("Enter second filename: ")
merged_file = input("Enter name for merged file: ")
try:
# Step 2: Read contents from both files
with open(file1, 'r') as f1, open(file2, 'r') as f2:
28/49
content1 = f1.read()
content2 = f2.read()
# Step 3: Merge and convert to uppercase
merged_content = (content1 + "\n" + content2).upper()
# Step 4: Write to the merged file
with open(merged_file, 'w') as mf:
mf.write(merged_content)
print(f"Merged content written to '{merged_file}' in uppercase.")
except FileNotFoundError:
print("One of the input files was not found.")
✅ Example:
file1.txt
nginx
hello world
file2.txt
nginx
python programming
After running the program and entering merged.txt as output:
merged.txt
nginx
HELLO WORLD
PYTHON PROGRAMMING
29/49
Let me know if you want to preserve line breaks, ignore blank lines, or add file existence
checks before merging!
Write a Python program to replace last value of tuples in a
list.
Sample list: [(10, 20, 40), (40, 50, 60), (70, 80, 90)]
Expected Output: [(10, 20, 100), (40, 50, 100), (70, 80, 100)]
Here’s a Python program to replace the last value of each tuple in a list with a fixed value
(e.g., 100 ):
✅ Python Program
python
# Sample list
sample_list = [(10, 20, 40), (40, 50, 60), (70, 80, 90)]
# Value to replace the last element with
new_last_value = 100
# Create new list with modified tuples
modified_list = [t[:-1] + (new_last_value,) for t in sample_list]
# Output the result
print("Modified List:", modified_list)
✅ Output:
less
Modified List: [(10, 20, 100), (40, 50, 100), (70, 80, 100)]
30/49
✅ Explanation:
t[:-1] takes all elements except the last from tuple t .
(new_last_value,) creates a one-element tuple with the new value.
+ joins them to form a new tuple with the last value replaced.
Let me know if you want to take the replacement value as user input!
Create a 5x 2 integer array from a range between 100 to
200 such that the difference between each element is 10.
Here's how to create a 5 × 2 integer NumPy array from the range 100 to 200, with a
difference of 10 between elements:
✅ Python Program Using NumPy
python
import numpy as np
# Create a 1D array from 100 to 200 with step 10
arr = np.arange(100, 200, 10)
# Reshape it into a 5x2 array
result = arr.reshape(5, 2)
# Print the result
print("5x2 Array:")
print(result)
✅ Output:
less
5x2 Array:
[[100 110]
31/49
[120 130]
[140 150]
[160 170]
[180 190]]
✅ Explanation:
np.arange(100, 200, 10) → generates numbers: 100, 110, ..., 190
.reshape(5, 2) → reshapes the 10 elements into a 5-row, 2-column matrix
Let me know if you'd like a version without NumPy (using pure Python).
What is lamda function? Explain with example.
✅ What is a Lambda Function in Python?
A lambda function is a small, anonymous (unnamed) function in Python, used to write
simple, one-line functions.
It is defined using the keyword lambda instead of def .
✅ Syntax:
python
lambda arguments: expression
Takes any number of arguments
Can only contain one expression
Returns the result of the expression automatically
✅ Example 1: Simple Lambda Function
python
32/49
# Lambda to add 10 to a number
add10 = lambda x: x + 10
print(add10(5)) # Output: 15
This is equivalent to:
python
def add10(x):
return x + 10
✅ Example 2: Lambda with Multiple Arguments
python
# Lambda to multiply two numbers
multiply = lambda a, b: a * b
print(multiply(4, 5)) # Output: 20
✅ Example 3: Using Lambda with map()
python
numbers = [1, 2, 3, 4]
doubled = list(map(lambda x: x * 2, numbers))
print(doubled) # Output: [2, 4, 6, 8]
✅ Example 4: Using Lambda with filter()
python
33/49
# Filter even numbers
nums = [1, 2, 3, 4, 5, 6]
evens = list(filter(lambda x: x % 2 == 0, nums))
print(evens) # Output: [2, 4, 6]
✅ Summary:
Feature Lambda Function
Name Anonymous (no name)
Syntax lambda args: expression
Usage Simple, short operations
Restrictions Only one expression allowed
Used with map() , filter() , sort() , etc.
Let me know if you want lambda used in sorting, data structures, or GUI examples.
Write a program to calculate GCD of two numbers.
Here’s a simple Python program to calculate the GCD (Greatest Common Divisor) of two
numbers using both:
✅ Method 1: Using the math module (built-in)
python
import math
# Input from user
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
34/49
# Calculate GCD
gcd = math.gcd(a, b)
# Output result
print("GCD is:", gcd)
✅ Method 2: Without using a module (using Euclidean algorithm)
python
# Function to calculate GCD
def compute_gcd(x, y):
while y:
x, y = y, x % y
return x
# Input from user
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
# Call function
print("GCD is:", compute_gcd(a, b))
🧠 Example Input/Output:
yaml
Enter first number: 36
Enter second number: 60
GCD is: 12
Let me know if you also want to compute LCM or GCD of more than 2 numbers!
What will be the output of the following Python code
snippet? for i in " join(reversed(list('abcd'))):
print (i)
35/49
Let's analyze the given code:
❌ Original Code (with typo/syntax error):
python
for i in " join(reversed(list('abcd'))):
print (i)
This code will raise a SyntaxError due to:
An extra double quote ( " ) before join(...)
Possibly incorrect indentation
✅ Corrected Version:
If the intent was to reverse the string 'abcd' , join the characters, and then print each
character, the correct version is:
python
for i in "".join(reversed(list('abcd'))):
print(i)
🔍 Let's break it down:
list('abcd') → ['a', 'b', 'c', 'd']
reversed(...) → ['d', 'c', 'b', 'a']
"".join(...) → 'dcba'
So the loop becomes:
python
for i in 'dcba':
print(i)
36/49
✅ Output:
css
d
c
b
a
Let me know if you want this output in a single line or as a list!
Justify the statement: "Strings are immutable". Explain
string manipulation in python programming with the help
of code.
✅ Justification: "Strings are Immutable in Python"
In Python, strings are immutable, meaning once a string is created, its contents cannot be
changed.
You cannot modify, insert, or delete individual characters directly from a string. Instead,
operations that appear to "change" a string actually return a new string.
🔒 Example: Attempting to modify a string (Error)
python
s = "hello"
s[0] = 'H' # ❌ This will raise an error
Output:
php
TypeError: 'str' object does not support item assignment
37/49
✅ String Manipulation in Python
Even though strings are immutable, Python provides many string methods to create new
modified strings.
1. Concatenation
python
s1 = "Hello"
s2 = "World"
result = s1 + " " + s2
print(result) # Output: Hello World
2. Slicing
python
s = "Python"
print(s[0:3]) # Output: Pyt
print(s[-1]) # Output: n
3. Uppercase / Lowercase
python
s = "hello"
print(s.upper()) # Output: HELLO
print(s.lower()) # Output: hello
4. Replace
38/49
python
s = "I love apples"
new_s = s.replace("apples", "oranges")
print(new_s) # Output: I love oranges
5. Strip (removing spaces)
python
s = " hello "
print(s.strip()) # Output: "hello"
6. Split and Join
python
s = "a,b,c"
parts = s.split(",") # ['a', 'b', 'c']
joined = "-".join(parts) # 'a-b-c'
print(joined)
✅ Summary
Operation Method Used Example
Upper/lowercase .upper() , .lower() "hi".upper() → 'HI'
Replace .replace() "abc".replace('a','x')
Strip spaces .strip() " hi ".strip()
Concatenation + "a" + "b" → "ab"
39/49
Operation Method Used Example
Slicing [] "hello"[1:4] → "ell"
Split/join .split() , .join() 'a-b'.split('-') → ['a', 'b']
So while strings themselves can’t be modified in-place, Python gives us powerful tools to
manipulate and create new strings easily.
What is the best framework for web scraping with python?
How do you extract data from website?
✅ What is the Best Framework for Web Scraping in Python?
The best framework for web scraping depends on your project’s complexity:
🔹 1. BeautifulSoup (bs4) – Best for Beginners
Easy to learn
Parses HTML and XML
Works with requests
Use case: Small projects or scraping static websites.
bash
pip install beautifulsoup4 requests
python
import requests
from bs4 import BeautifulSoup
url = 'https://example.com'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
40/49
# Extract title
print(soup.title.string)
🔹 2. Scrapy – Best for Large, Scalable Crawling
Full-fledged scraping framework
Built-in support for crawling, pipelines, export
Handles asynchronous requests
Use case: Large-scale scraping with advanced control
bash
pip install scrapy
bash
scrapy startproject myproject
cd myproject
scrapy genspider example example.com
Inside the spider file:
python
def parse(self, response):
title = response.css('title::text').get()
yield {'title': title}
🔹 3. Selenium – Best for Dynamic Websites (JavaScript-rendered)
Controls a real browser (Chrome, Firefox)
Can scrape content that appears after user interaction
Use case: Websites with dynamic content, AJAX
41/49
bash
pip install selenium
python
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://example.com')
print(driver.title)
driver.quit()
✅ Summary: Best Frameworks
Framework Best For JavaScript Support Learning Curve
BeautifulSoup Simple, static pages ❌ Easy
Scrapy Large, scalable scraping ❌ (limited) Medium
Selenium Dynamic pages with JS ✅ Medium-High
✅ How to Extract Data from a Website
1. Inspect the website
Right-click → Inspect Element
Identify HTML tags and classes of the data you need
2. Send a request
Use requests.get(URL)
3. Parse the content
Use BeautifulSoup or XPath/CSS selectors
42/49
4. Extract the data
.find() , .find_all() , .select() , .get_text() , .get('href')
5. Store the data
Save to CSV, JSON, database, or file
Let me know if you want a full working example for a real website (e.g., job listings, news,
products)!
How to scrape multiple pages of website using Python.
To scrape multiple pages of a website using Python, you typically follow a pattern of
iterating through paginated URLs and extracting the desired content from each page.
✅ Steps to Scrape Multiple Pages
1. Identify the pagination pattern in the URL (e.g., ?page=1 , page/2 , offset=20 , etc.)
2. Loop through each page number
3. Make HTTP requests for each page
4. Parse and extract data
5. Store or print the results
🔹 Example: Using requests + BeautifulSoup
Let’s say the website URL pattern is:
arduino
https://example.com/products?page=1
https://example.com/products?page=2
...
✅ Code:
43/49
python
import requests
from bs4 import BeautifulSoup
# Loop through pages 1 to 5
for page in range(1, 6):
url = f'https://example.com/products?page={page}'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
# Extract product titles (example tag/class)
products = soup.find_all('h2', class_='product-title')
print(f"\nPage {page} Products:")
for product in products:
print(product.text.strip())
🔹 Example: With Dynamic Pagination (like offset=20 )
python
for offset in range(0, 100, 20): # Assuming 20 items per page
url = f'https://example.com/data?offset={offset}'
# continue as before
🔹 Optional: Save to CSV
python
import csv
with open('products.csv', 'w', newline='') as file:
writer = csv.writer(file)
writer.writerow(['Product Name'])
44/49
for page in range(1, 6):
url = f'https://example.com/products?page={page}'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
products = soup.find_all('h2', class_='product-title')
for product in products:
writer.writerow([product.text.strip()])
🧠 Tips:
Add time.sleep(1) between requests to avoid overloading the server.
Use headers to mimic a real browser ( User-Agent ).
If content is loaded dynamically (JavaScript), use Selenium.
Let Me Know:
If you give me a sample URL pattern, I can write a custom scraper for it.
Write a program to download all images from a web page
in a python.
Here’s a complete Python program to download all images from a web page using
requests , BeautifulSoup , and urllib .
✅ Prerequisites
Install the required libraries (if not already):
bash
pip install requests beautifulsoup4
45/49
✅ Python Code
python
import requests
from bs4 import BeautifulSoup
import os
from urllib.parse import urljoin
import shutil
# URL of the website
url = input("Enter the URL of the webpage: ")
# Create a folder to save images
os.makedirs("downloaded_images", exist_ok=True)
# Get the HTML content
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
# Find all image tags
img_tags = soup.find_all('img')
print(f"Found {len(img_tags)} image(s). Downloading...")
# Download each image
for img in img_tags:
img_url = img.get('src')
if not img_url:
continue
# Convert relative URLs to absolute
img_url = urljoin(url, img_url)
filename = os.path.basename(img_url.split("?")[0]) # Handle query strings
try:
img_data = requests.get(img_url, stream=True)
if img_data.status_code == 200:
with open(f"downloaded_images/{filename}", 'wb') as f:
shutil.copyfileobj(img_data.raw, f)
print(f"Downloaded: {filename}")
46/49
except Exception as e:
print(f"Failed to download {img_url} - {e}")
✅ How It Works
1. Asks for the webpage URL
2. Downloads and parses the HTML
3. Extracts all <img> tags
4. Resolves absolute URLs using urljoin
5. Saves images in a folder named downloaded_images/
🧠 Notes:
This works for static image URLs (not dynamically loaded by JavaScript).
For JavaScript-heavy sites, use Selenium instead.
Would you like a version that uses Selenium for dynamic websites too?
How to create and view HTML files with Python.
You can easily create and view HTML files using Python by writing HTML content into a
.html file and then opening it in a web browser.
✅ Step-by-Step Guide
🔹 1. Create an HTML File in Python
python
47/49
html_content = """
<!DOCTYPE html>
<html>
<head>
<title>My First HTML Page</title>
</head>
<body>
<h1>Hello from Python!</h1>
<p>This is a sample HTML file created using Python.</p>
</body>
</html>
"""
# Write HTML to a file
with open("sample.html", "w") as file:
file.write(html_content)
🔹 2. View HTML File in Browser
You can open it automatically using Python’s webbrowser module:
python
import webbrowser
# Open the file in the default browser
webbrowser.open("sample.html")
✅ Full Program
python
import webbrowser
html_content = """
<!DOCTYPE html>
48/49
<html>
<head>
<title>Generated Page</title>
</head>
<body>
<h1>Hello, World!</h1>
<p>This HTML file was generated using Python.</p>
</body>
</html>
"""
file_path = "sample.html"
# Save the file
with open(file_path, "w") as file:
file.write(html_content)
# Open in browser
webbrowser.open(file_path)
🧠 Notes:
You can customize html_content with any valid HTML code.
Works cross-platform (Windows, macOS, Linux).
This is helpful for generating reports, dashboards, or visualizations.
Would you like an example that includes CSS or JavaScript too?
49/49