0% found this document useful (0 votes)
16 views

MP 1 Arsd Lecture 5

Uploaded by

sciyasingh
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)
16 views

MP 1 Arsd Lecture 5

Uploaded by

sciyasingh
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/ 14

Lecture 5

10-Sept-2024
on
“Functions”

Mathematical Physics -1
by
Prof. Vinita Tuli
Prof. Anjali Kaushik
Shivnandi
Sneh
Functions Basics

➢ In programming, a function is a sequence of instructions that performs a


specific task. A function is a block of code that can run when it is called. A
function can have input arguments, which are made available to it by the
user, the entity calling the function. Functions also have output
parameters, which are the results of the function that the user expects to
receive once the function has completed its task. For example, the
function math.sin has one input argument, an angle in radians, and one
output argument, an approximation to the sin function computed at the
input angle (rounded to 16 digits).

TRY IT! Verify that len is a built-in function using the type function.
type(len)
builtin_function_or_method
Functions Basics

➢ In programming, a function is a sequence of instructions that performs a


specific task.

TRY IT! Verify that np.linspace is a function using the type function.
And find how to use the function using the question mark.
import numpy as np

type(np.linspace)

function
Functions Basics

➢ In programming, a function is a sequence of instructions that performs a


specific task.

TRY IT! Verify that np.linspace is a function using the type function.
And find how to use the function using the question mark.
np.linspace? # this will work with jupyter notebook
Define your own function

➢ In programming, a function is a sequence of instructions that performs a


specific task.

TRY IT! We can define our own functions. A function can be specified in several ways. Here we will
introduce the most common way to define a function which can be specified using the keyword def, as
showing in the following:
def function_name(argument_1, argument_2, ...):
'''
Descriptive String
'''
# comments about the statements
function_statements

return output_parameters (optional)


Functions Basics

➢ In programming, a function is a sequence of instructions that performs a


specific task.

TRY IT! Define a function named my_adder to take in 3 numbers


and sum them.
def my_adder(a, b, c):
"""
function to sum the 3 numbers
Input: 3 numbers a, b, c
Output: the sum of a, b, and c
author:
date:
"""

# this is the summation


out = a + b + c

return out
Functions Basics
TRY IT! Define a function named my_adder to take in 3 numbers
and sum them.
def my_adder(a, b, c):
"""
function to sum the 3 numbers
Input: 3 numbers a, b, c
Output: the sum of a, b, and c
author:
date:
"""
# this is the summation
out = a + b + c

return out

d = my_adder(1, 2, 3)
print(d)
Local and Global variable

TRY IT!

x = "global"

def my_func():
x = "local"
print("Inside function:", x)

my_func()
print("Outside function:", x)
Passing Functions as Arguments

TRY IT! In Python, functions are first-class objects, meaning they can be
passed as arguments to other functions.
def add(a, b):
return a + b

def apply_operation(func, x, y):


return func(x, y)

result = apply_operation(add, 5, 3)
print("Result:", result)
Modules and Importing Modules

TRY IT! Modules: Files containing Python code (functions, variables,


classes) that can be reused across scripts.
Importing Modules: Use import to bring modules into your script.

import math
print(math.sqrt(16))
Creating and Importing Your Own Module

TRY IT! To create a module, save a .py file with functions and
variables.
Import your module using import module_name.

# mymodule.py
def square(x):
return x ** 2

# main.py
import mymodule
print(mymodule.square(4)) # Outputs 16
Generating Pseudo-Random Numbers

TRY IT! Use the random module to generate pseudo-random


numbers.
Examples: random.randint(), random.random(), random.choice().

import random
print("Random integer:", random.randint(1, 10))
print("Random float:", random.random())
Finding the Largest of Three Numbers

TRY IT!
def largest(a, b, c):
return max(a, b, c)

print(largest(5, 9, 3))
Listing Prime Numbers

TRY IT!
Write your own code to print prime number between 3 to 100?

You might also like