0% found this document useful (0 votes)
2 views12 pages

Python

The document provides Python programming examples covering various topics such as finding the longest word in a list, object serialization using pickle, exception handling, and the differences between modules and packages. It also includes practical examples of file reading, turtle graphics for drawing patterns, and short notes on networking concepts like SMTP, POP3, firewalls, and DHCP servers. Overall, it serves as a comprehensive guide to Python programming and networking fundamentals.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views12 pages

Python

The document provides Python programming examples covering various topics such as finding the longest word in a list, object serialization using pickle, exception handling, and the differences between modules and packages. It also includes practical examples of file reading, turtle graphics for drawing patterns, and short notes on networking concepts like SMTP, POP3, firewalls, and DHCP servers. Overall, it serves as a comprehensive guide to Python programming and networking fundamentals.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

2 (a) Write a Python program to find the longest word in a list of

strings

words = ["apple", "banana", "cherry", "watermelon",


"kiwi"]

longest = max(words, key=len)


print("Longest word:", longest)

2 (b) What is object serialization? Write a program to


serialize and deserialize a Python object

Object Serialization is the process of converting a Python


object into a byte stream so it can be stored in a file or sent
over a network.​
Deserialization is the reverse process—converting the byte
stream back into a Python object.

Example using pickle:

import pickle
# Object to be serialized

data = {"name": "PSR", "age": 20, "course": "Python"}

# Serialization

with open("[Link]", "wb") as f:

​ [Link](data, f)

# Deserialization
with open("[Link]", "rb") as f:

​ new_data = [Link](f)

print("Deserialized object:", new_data)

3 (a) Differentiate between built-in and user-defined exceptions


in Python (with examples)

Built-in Exceptions User-defined


Exceptions

Already defined in Python Created by the


programmer

Used for common errors Used for custom error


handling

Examples: ZeroDivisionError, Created using class


ValueError

Example (Built-in):

x = 10 / 0 # Raises ZeroDivisionError

Example (User-defined):

class AgeError(Exception):

​ pass

age = -5
if age < 0:

​ raise AgeError("Age cannot be negative")

3 (b) Python program using try, except, and finally to handle


ZeroDivisionError

try:

​ a = int(input("Enter a number: "))

​ b = int(input("Enter another number: "))

​ result = a / b

​ print("Result:", result)

except ZeroDivisionError:

​ print("Error: Division by zero is not allowed")

finally:

​ print("Program execution completed")

4 (a) Explain the difference between a module and a


package in Python
●​ Module:​
A module is a single Python file (.py) that contains
functions, classes, or variables.​
It helps in organizing code and reusing it.

Example:​
[Link] (built-in module), [Link]

●​ Package:​
A package is a collection of related modules organized in
a directory.​
It contains a special file called __init__.py (optional in
newer versions).

Example:

mypackage/

​ __init__.py

​ [Link]

​ [Link]

Key Difference:​
A module is a single file, while a package is a folder containing
multiple modules.

4 (b) Explain the concept of intra-package references with


an example. How does Python resolve package imports?

Intra-package references allow modules within the same


package to import each other.

Example:
mypackage/

​ __init__.py

​ [Link]

​ [Link]

[Link] :

def show():

​ print("Hello from module1")

[Link] :

from .module1 import show

show()

5 (a) What is module search path? Explain with an example


how Python searches for a module

The module search path is the list of directories where Python


looks for a module when an import statement is executed.​
This path is stored in [Link].

Python searches for a module in this order:

1. Current working directory

2. Directories in the PYTHONPATH environment variable

3. Standard library directories

4. Site-packages directory (third-party modules)

Example:
import sys

print([Link])

If you write:

import calculator

5 (b) Create a user-defined module named calculator and


demonstrate different ways to import and use it

[Link]

def add(a, b):

​ return a + b

def sub(a, b):

​ return a - b

def mul(a, b):

​ return a * b

def div(a, b):


​ return a / b

Different ways to import and use the module

1. Import the whole module

import calculator

print([Link](5, 3))

2. Import specific functions

from calculator import add, sub

print(add(10, 5))

print(sub(10, 5))

3. Import with alias

import calculator as calc

print([Link](4, 5))

4. Import all functions

from calculator import *


print(div(20, 4))

6 (a) Write a Python program to read a text file and count


the number of lines, words, and characters in the file

file = open("[Link]", "r")

lines = 0

words = 0

characters = 0

for line in file:

​ lines += 1

​ characters += len(line)

​ words += len([Link]())

[Link]()

print("Number of lines:", lines)

print("Number of words:", words)

print("Number of characters:", characters)


6 (b) Write a program using turtle graphics to draw a colorful
pattern using loops.

import turtle

t = [Link]()

[Link](0)

colors = ["red", "blue", "green", "purple"]

# Square pattern

for i in range(36):

​ [Link](colors[i % 4])

​ for j in range(4):

​ [Link](100)

​ [Link](90)

​ [Link](10)
[Link]()

[Link](-200, -150)

[Link]()

# Triangle pattern

for i in range(36):

​ [Link](colors[i % 4])

​ for j in range(3):

​ [Link](100)

​ [Link](120)

​ [Link](10)

[Link]()

[Link](200, -150)

[Link]()

# Circle pattern

for i in range(36):

​ [Link](colors[i % 4])

​ [Link](50)

​ [Link](10)
[Link]()

7. Write short notes on the following:

(a) SMTP (Simple Mail Transfer Protocol)​


SMTP is an application-layer protocol used to send emails
from a client to a mail server or between mail servers.​
It works on port 25 (default) and ensures reliable email
transmission over the internet.

(b) POP3 (Post Office Protocol version 3)​


POP3 is used to receive emails from a mail server.​
It downloads emails to the user’s device and usually deletes
them from the server.​
It works on port 110.
(c) Firewall​
A firewall is a network security system that monitors and
controls incoming and outgoing network traffic based on
predefined security rules.​
It protects systems from unauthorized access and cyber
attacks.

(d) DHCP Server (Dynamic Host Configuration Protocol)​


A DHCP server automatically assigns IP addresses and other
network configuration details (like subnet mask and gateway) to
devices on a network, reducing manual configuration.

You might also like