Unit 04. PWP (22616)
Unit 04. PWP (22616)
Python provides built-in functions like print(), len(), and also allows user-defined functions.
Example:
def greet():
Output:
They help save time as they eliminate the need to write complex logic from scratch.
Built-in functions do not require imports, making them efficient and easy to use.
Example:
print(len(numbers)) # Output: 4
Example:
num = "100"
4. max(x, y, ...): Returns the largest value from the given numbers. Example: max(10, 20) → 20.
5. min(x, y, ...): Returns the smallest value from the given numbers. Example: min(5, 15) → 5.
o Example Usage:
double = lambda x: x * 2
print(double(5)) # Output: 10
A user-defined function can accept parameters, process data, and return output.
Example:
def greet():
Output:
The function body contains statements that define what the function does.
Example:
If the function has parameters, values (arguments) must be passed during the call.
Example:
def square(n):
return n * n
print(result) # Output: 16
They act as placeholders for values that will be passed during function calls.
➤ Arguments
Arguments are the actual values passed to the function when calling it.
Example:
Output:
Hello, Rahul
Example:
return x * y
result = multiply(5, 3)
print(result) # Output: 15
In Python, user-defined functions (UDFs) are created using the def keyword. A function can take
parameters (inputs), perform operations, and return a result.
def function_name():
# Function body
statements
def greet():
◆ Output:
def square(x):
print("Square =", x * x)
◆ Output:
Square = 4
return a + b
result = add(5, 3)
◆ Output:
Addition = 8
1. Global Variable
◆ Definition:
A global variable is a variable that is declared outside any function and can be accessed from
anywhere in the program (inside or outside functions).
◆ Example:
x = 10 # Global variable
def show():
show()
◆ Output:
z’‘ Here, x is a global variable and is accessible both inside and outside the function.
2. Local Variable
◆ Definition:
A local variable is a variable declared inside a function, and it can only be accessed within
that function. It cannot be used outside the function.
◆ Example:
def show():
y = 20 # Local variable
show()
◆ Output:
Accessible Both inside and outside functions Only inside the function
Explain Module
A module in Python is a file containing Python definitions, functions, variables, and classes. It is used
to organize code into separate files for better readability and reusability.
Namespace: Every module creates its own namespace to avoid conflicts between
different modules.
Modularity: Modules help in dividing large programs into smaller, manageable, and
logically organized parts.
Uses of a Module
2. Organization: Modules help in organizing code logically into separate files, which makes
the program easier to maintain.
4. Avoids Redundancy: Instead of writing the same functions or classes again, you can
import and reuse them from a module.
5. Ease of Debugging: When code is modularized into separate files, it is easier to identify
and fix issues in specific sections.
6. Improves Readability: By splitting the code into smaller modules, the overall readability
of the program improves.
A module is simply a .py file that contains Python code (functions, classes, variables, etc.).
To define a module, follow these steps:
Example:
# mymodule.py def
greet(name):
# greetings.py def
greet(name):
# main_program.py
message = greetings.greet(name) # Use the greet function from the greetings module print(message)
Explanation:
In the main_program.py file, we import the greetings module using the import keyword
and then call the greet() function from the module.
# program_name.py
def get_program_name():
# main_program.py
print(message)
Explanation:
In the module program_name.py, the function get_program_name() asks the user for
the program name and returns the formatted message.
In the main program main_program.py, we import the program_name module and use
the get_program_name() function to display the entered program name.
मराठी टिप:
1) What is Namespace?
Each namespace exists in a specific scope and can be local, global, or built-in,
determining how Python looks for variables and functions.
2) What is Scoping?
Scoping refers to the region of the program where a particular variable or function is
accessible. The scope determines the visibility of a variable within different parts of the
code.
There are mainly four types of scopes in Python: Local, Enclosing, Global, and Built-in
(often abbreviated as LEGB rule).
1. Avoiding Name Conflicts: Namespaces ensure that variables and functions in different
parts of the program do not clash, even if they have the same name.
3. Modular Programming: Helps in organizing code by defining variables and functions that
are specific to certain sections of the code or modules.
# Global Namespace
x = 10 # Global variable
def func1():
def func2():
3. Packages promote code reusability by allowing users to import only required modules.
2. Inside the folder, create an init .py file (this makes it a package).
4. Import and use the modules in a Python script using import package.module.
‘’z Example:
# Step 2: Inside it, create a file ` init .py` (empty or with initialization code)
# greet.py
def hello():
import mypackage.greet as gt
gt.hello()
Output:
3) Using Standard Packages in Python ( i, ii only read kara iii, iv, v most imp)
◆ Uses:
z’‘ Example:
import math
◆ Definition: SciPy is used for scientific computing, including optimization, integration, and
signal processing.
◆ Uses:
Signal/image processing.
‘’z Example:
data = [1, 2, 3, 4, 5, 5, 6, 8, 9]
◆ Definition: NumPy provides support for multi-dimensional arrays and numerical computations.
◆ Uses:
z’‘ Example:
import numpy as np
◆ Definition: Matplotlib is used for data visualization like plots, graphs, and charts.
◆ Uses:
‘’z Example:
x = [1, 2, 3, 4]
plt.plot(x, y, marker="o")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.title("Simple Line
Graph") plt.show()
◆ Uses:
‘’z Example:
import pandas as pd
df = pd.DataFrame(data)
print(df)
Output:
Name Age
0 Alice 25
1 Bob 30
2 Charlie 35
mypackage/
│── greetings.py
│── math_ops.py
greetings.py
def say_hello():
math_ops.py
return a + b
mypackage.math_ops as mops
3)) # Output: 8
Marathi Tip:
◆ Python मधील Math, NumPy, SciPy, Pandas यासारखी पॅकेजेस डेिा सायन्स आटि
संगिकीय गिनांमध्ये महत्त्वाची भूटमका बजावतात.
◆ स्वतः चे पॅकेज तयार करून मोठ्या प्रमािावर कोटडंग करिे सोपे होते.
Summer 2022
2. Explain how to use user defined function in python with example. (4marks)
3. Write a program for importing module for addition and subtraction of two numbers. (4marks)
Winter 2022
2. What is local and global variables? Explain with appropriate example. (4marks)
numbers given as input from command line arguments and print its sum.
(4marks)
Summer 2023
5. Write a Python program to calculate sum of digit of given number using function. (6marks)
Winter 2023
2. Write python program using module, show how to write and use module by importing it.
(4marks)
Summer 2024
3. Write a python program to create a user defined module that will ask your program name
and display the name of the program. (4marks)
Winter 2024
2. Explain how to use user defined function in Python with example . (4marks)