0% found this document useful (0 votes)
7 views6 pages

pwp_imp

The document outlines key concepts in Python programming, including the differences between functions and modules, variable scope, overloading vs. overriding, and inheritance types. It also covers file modes, user-defined exceptions, and demonstrates importing functions from modules. Additionally, it provides examples of data hiding and abstraction, as well as practical programs for various tasks such as counting vowels and listing directory files.

Uploaded by

Yash raundal
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)
7 views6 pages

pwp_imp

The document outlines key concepts in Python programming, including the differences between functions and modules, variable scope, overloading vs. overriding, and inheritance types. It also covers file modes, user-defined exceptions, and demonstrates importing functions from modules. Additionally, it provides examples of data hiding and abstraction, as well as practical programs for various tasks such as counting vowels and listing directory files.

Uploaded by

Yash raundal
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
You are on page 1/ 6

1.

What is the different between a function and module in python

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

3. Different between overloading and overriding

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

Inheritance is an object-oriented programming feature where a child class (or subclass)


inherits properties and methods from a parent class (or superclass).
It helps in code reuse, modularity, and extending functionality.

Types of Inheritance 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'.

6. write a program to demonstrate importing specific function from a module

Step 1: Create a module file (my_module.py)


# my_module.py

def add(a, b):


return a + b

def greet(name):
return f"Hello, {name}!"
Step 2: Use the module in another file (main.py)

# main.py

from my_module import add, greet

# Using functions from user-defined module


print(add(5, 3))
print(greet("Ravi"))

7. Write a function to count vowels in a string


def count_vowels(text):
vowels = "aeiouAEIOU"
count = 0
for char in text:
if char in vowels:
count += 1
return count

# Input from user (screen)


user_input = input("Enter a string: ")
vowel_count = count_vowels(user_input)

print("Number of vowels:", vowel_count)

8. Write a program to raise a user-defined exception if a negative number is entered

import tkinter.messagebox as msgbox

# User-defined exception
class NegativeNumberError(Exception):
pass

# Function to check number


def check_number(num):
if num < 0:
raise NegativeNumberError("Negative number is not allowed!")
else:
msgbox.showinfo("Success", f"Entered number is: {num}")

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

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):


if b != 0:
return a / b
else:
return "Division by zero is not allowed"

Step 2: Use the module in another Python file (e.g., main.py)


# main.py

import math_ops

x = 10
y=5

print("Addition:", math_ops.add(x, y))


print("Subtraction:", math_ops.sub(x, y))
print("Multiplication:", math_ops.mul(x, y))
print("Division:", math_ops.div(x, y))

10.Write a program to demonstrate importing all functions from a module.


Step 1: Create a module file – math_ops.py
# math_ops.py

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):


if b != 0:
return a / b
else:
return "Cannot divide by zero"
Step 2: Main file – Import all functions
# main.py

from math_ops import *

x = 20
y=4

print("Addition:", add(x, y))


print("Subtraction:", sub(x, y))
print("Multiplication:", mul(x, y))
print("Division:", div(x, y))

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 deposit(self, amount): # Data abstraction


if amount > 0:
self.__balance += amount

def show_balance(self):
print("Balance:", self.__balance)

# Using the class


acc = BankAccount("Ravi", 5000)
acc.deposit(1000)
acc.show_balance()
# print(acc.__balance) # This will give an error (hidden)
12.Write a Python program to list all files in a directory using the os module.

import os

# Get directory path from user


directory = input("Enter the directory path: ")

# Check if the directory exists


if os.path.exists(directory):
print("Files in directory:")
for file in os.listdir(directory):
if os.path.isfile(os.path.join(directory, file)):
print(file)
else:
print("Directory does not exist.")

13.write a program to implement a simple bank system


class BankAccount:
def __init__(self, balance):
self.__balance = balance

def get_balance(self):
return self.__balance

def deposit(self, amount):


self.__balance += amount

You might also like