Assignment- 3
Course: B.Tech. Semester: I
Subject: Introduction to Python Programming Subject Code: TCS-102
Submission Date: 30-November-2025
1.
data = [5, 10, 15, 20, 25, 30]
print(data[2:5])
print(data[-4:-1])
print(data[::-2])
a) Predict the output.
b) Explain what [::-2] does.
2. a) Explain the difference between the following list methods: append(), insert() and
extend().
b) Predict the output:
nums = [1, 2, 3]
nums.append([4, 5])
nums.extend([6, 7])
print(nums)
3. a) Why are tuples considered immutable in Python?
b) What will happen if you try to modify an element in a tuple?
Give an example to support your answer.
4.
x = (10, [20, 30], 40)
x[1][0] = 99
print(x)
Why did the code not raise an error even though tuples are immutable?
5.
marks = {'Math': 88, 'Science': 92, 'English': 85}
marks['History'] = 90
marks['Math'] = marks['Math'] + 5
del marks['English']
print(marks)
print('Science' in marks)
a) Predict the final output.
b) What does 'Science' in marks check for?
6. a) What is the difference between dict.get(key) and direct access dict[key]?
b) Explain with an example where dict.get() is safer.
7.
A = {1, 2, 3, 4}
B = {3, 4, 5, 6}
print(A - B)
print(B - A)
print(A.symmetric_difference(B))
a) What will be printed?
b) Explain the difference between difference() and symmetric_difference().
8. Write a Python code using the re module to check if a given email address is valid.
(Hint: A valid email must contain @ and end with .com)
Example input: "user123@gmail.com" → Output: "Valid email"
9. Write a Python program that:
Opens a text file info.txt in write mode and writes N lines of text.
Then reopens the same file in read mode and read every alternate line one by one.
10. Write a Python program to read the content of data.txt file and find the size of file in
bytes.
11.
class Student:
def __init__(self, name):
self.name = name
s1 = Student("Ravi")
print(s1.name)
a) What will be the output of the above program?
b) What is the purpose of the __init__() method?
12. a) Explain the difference between instance attributes and class attributes with an
example.
b) Predict the output:
class Car:
wheels = 4
def __init__(self, brand):
self.brand = brand
c1 = Car("BMW")
print(c1.brand, c1.wheels)
13. a) What is inheritance in Python OOP?
b) Write a short example showing a child class inheriting attributes or methods from a parent
class.
14. a) What is polymorphism?
b) Write a simple Python example where two classes (Dog and Cat) have the same method
name speak(), and both can be called using a common interface.
15. a) What is encapsulation? Demonstrate it using private (__var) and public variables in a
class.
(b) Why do we use abstraction in object-oriented programming? Demonstrate with a python
code.
16.
import numpy as np
arr = np.array([10, 20, 30, 40])
print(arr + 5)
print(arr[1:3])
What will be the output?
17.
a) Write a NumPy code to find the mean and standard deviation of the array [1, 2, 3, 4, 5].
b) What function is used to perform matrix multiplication in NumPy?
18. A weather station records the temperature at morning, afternoon, evening for 5 days:
temp = [
[20, 28, 24],
[22, 30, 25],
[19, 27, 23],
[21, 29, 24],
[23, 31, 26]
]
a)Find the hottest afternoon temperature.
b) Calculate the average temperature for each time of day.
c) Find the day with the lowest evening temperature.
19. Following is the employee attendance data records of 4 employees working for 5 days
represented in hours.
hours = [
[8, 7, 9, 8, 6],
[9, 8, 8, 8, 7],
[7, 7, 8, 9, 9],
[8, 9, 9, 8, 8]
]
a) Find each employee’s total hours worked.
b) Find the day with the least total attendance.
c) Find the employee with the highest average attendance.
20.
Assume a file candidates.csv has columns Name and Age. Write a script in python using the
pandas library to add another column Age_Group to it that shows the status as “Adult” if the
age of the person is greater than 18 otherwise show the person as “Minor” if the age is less
than 18. Later display all the names of the candidates who are “Adult” i.e. age >18.
21
import pandas as pd
data = {'Name': ['A', 'B', 'C'], 'Marks': [85, 90, 80]}
df = pd.DataFrame(data)
print(df['Marks'].mean())
a) What will be printed?
b) What is a DataFrame in pandas?
22.
a) Which function is used to create a line plot in Matplotlib?
b) What kind of data visualization is a box plot useful for?
c) How can you detect outliers using a box plot?
23.
a) What is Exploratory Data Analysis (EDA) and why is it important?
b) Name two data cleaning techniques used before performing EDA.
c) What does a correlation value close to +1 indicate ?