0% found this document useful (0 votes)
10 views3 pages

Python Modules, Functions, and File Handling

The document provides an overview of Python programming concepts, including modules, packages, command-line arguments, Armstrong number checks, string operations, programming paradigms, file access modes, and dictionary methods. It includes examples for each concept, illustrating how to implement them in Python code. Key distinctions between class and object variables, as well as file handling techniques, are also highlighted.

Uploaded by

vaishnavi.sapkal
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)
10 views3 pages

Python Modules, Functions, and File Handling

The document provides an overview of Python programming concepts, including modules, packages, command-line arguments, Armstrong number checks, string operations, programming paradigms, file access modes, and dictionary methods. It includes examples for each concept, illustrating how to implement them in Python code. Key distinctions between class and object variables, as well as file handling techniques, are also highlighted.

Uploaded by

vaishnavi.sapkal
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

Q1 a)

Modules and packages:

- A module is a file containing Python code (functions, classes, variables).

- A package is a directory with an __init__.py file that contains modules.

Example:

Module: [Link] with a function greet()

Package: mypackage/ with [Link], [Link]

Q1 b)

Command-line arguments:

Using [Link] from sys module.

Example:

import sys

name = [Link][1]

print(f"Hello, {name}")

Q1 c)

Armstrong number check:

def is_armstrong(num):

digits = [int(d) for d in str(num)]

power = len(digits)

return sum([d**power for d in digits]) == num

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

print(is_armstrong(n))

Q3 a)

String operations:

Concatenation: 'Hello' + 'World'

Appending: s += ' World'

Multiplication: 'Hi' * 3
Q3 b)

String formatting:

Using % operator: 'Name: %s' % name

Q3 c)

Char search without find():

for i in range(len(string)):

if string[i] == char:

return i

Q5 a)

Programming paradigms:

- Procedural: step-by-step functions

- Structured: loops, conditionals

- OOP: classes, objects

Q5 b)

Class vs Object variable:

Class: shared across all instances

Object: unique per instance

Q5 c)

Book class:

class Book:

def __init__(self):

[Link] = input()

...

Q7 a)

File access modes:

'r', 'w', 'a', 'r+', 'w+', 'a+', 'b'

Q7 b)
File methods:

seek(), writelines(), readline()

Q7 c)

Reverse file line-wise:

lines = [Link]()

[Link]()

[Link](lines)

Q8 a)

getcwd(): returns current dir

rmtree(): deletes folder recursively

makedirs(): creates nested dirs

Q8 b)

File paths:

Absolute: full path

Relative: from current dir

Q8 c)

Dictionary methods:

fromkeys(): dict with default values

setdefault(): set default if key missing

update(): merge dictionaries

You might also like