pwp_imp
pwp_imp
Function Module
A function is a block of reusable
A module is a Python file containing functions, classes
code.
etc.
Used to perform a specific task.
Used to organize and reuse code across files.
Defined using def keyword.
Created by saving code in a .py file.
Called directly using its name. Imported using import statement.
2. what is the scope of variable inside the function and outside the function
Local Scope:
A variable declared inside a function is called a local variable. It is created when the function is called and
destroyed when the function ends. It can only be accessed within that function.
Global Scope:
A variable declared outside of all functions is called a global variable. It exists throughout the program
and can be accessed from any function (unless shadowed by a local variable with the same name).
x = 10 # Global variable
def show():
y = 5 # Local variable
print("Inside function: x =", x)
print("Inside function: y =", y)
show()
print("Outside function: x =", x)
# print("Outside function: y =", y) # This will give error
Overloading Overriding
Same function name with different number or
Same function name and parameters in child class
type of parameters.
redefining parent class method.
Happens within the same class (Python
Happens in inheritance between parent and child
supports limited overloading).
classes.
Achieved using default arguments in Python. Achieved by defining same method in child class.
Example: def area(r) and def area(l, b) Example: child class defines its own display()
(simulated) overriding parent’s display()
4. What is inheritance list it's type support in python
Single Inheritance
Multiple Inheritance
Multilevel Inheritance
Hierarchical Inheritance
Hybrid Inheritance
5. List different file mode available in python
'r' Read mode – Opens file for reading only. File must exist.
'w' Write mode – Opens file for writing. Creates new or overwrites existing file.
'a' Append mode – Opens file to add data at the end. Creates file if not exists.
'r+' Read and write mode – File must exist. Allows reading and writing.
'w+' Write and read mode – Overwrites file if exists, creates new if not.
'a+' Append and read mode – Adds data to end and allows reading.
'b' Binary mode – Used for binary files like images, videos. Used as 'rb', 'wb'.
't' Text mode – Default mode. Opens file in text format. Used as 'rt', 'wt'.
def greet(name):
return f"Hello, {name}!"
Step 2: Use the module in another file (main.py)
# main.py
# User-defined exception
class NegativeNumberError(Exception):
pass
try:
number = int(input("Enter a number: "))
check_number(number)
except NegativeNumberError as e:
msgbox.showerror("Error", str(e))
except ValueError:
msgbox.showwarning("Invalid Input", "Please enter a valid number.")
9. create a module math_ops.py with function add sub mul and div
Step 1: Create the module — math_ops.py
# math_ops.py
import math_ops
x = 10
y=5
x = 20
y=4
11.Explain the concept of Data Hiding and Data Abstraction with real-time examples.
1. Data Hiding
It is the process of restricting access to internal object details. It is implemented using private
variables or methods (prefix or __ in Python).
Real-Time Example
ATM machine hides the internal circuit, balance checks, and security logic.
2. Data Abstraction
It means showing only essential features and hiding complex internal logic. Achieved using classes
and methods.
Real-Time Example
While driving a car, you just use the steering wheel, not engine mechanism inside.
Python Example:
class BankAccount:
def __init__(self, name, balance):
self.name = name
self.__balance = balance # Data hiding with __
def show_balance(self):
print("Balance:", self.__balance)
import os
def get_balance(self):
return self.__balance