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

Python Functions and NumPy Basics

The document provides an introduction to Python functions and the NumPy library, emphasizing their roles in modular programming and scientific computing. It explains the syntax for defining functions and demonstrates basic operations using NumPy arrays. Additionally, it includes two experiments focusing on calculating the sum of squares and performing element-wise operations with NumPy arrays.

Uploaded by

addfgh177
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)
4 views3 pages

Python Functions and NumPy Basics

The document provides an introduction to Python functions and the NumPy library, emphasizing their roles in modular programming and scientific computing. It explains the syntax for defining functions and demonstrates basic operations using NumPy arrays. Additionally, it includes two experiments focusing on calculating the sum of squares and performing element-wise operations with NumPy arrays.

Uploaded by

addfgh177
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

17-Oct-25

Python Lab
03
FUNCTIONS AND NUMPY
MODULAR PROGRAMMING AND SCIENTIFI C
COMPUTING

Introduction to Python Functions


A function is a reusable block of code that performs a specific task. Functions
help make programs modular, readable, and easy to debug.
Basic syntax:
def function_name(parameters):
# code block
return result

Example:
def add(a, b):
return a + b
print(add(3, 5)) # Output: 8

Functions can take zero or more parameters, return values, and even return multiple values.

17-Oct-25

1
17-Oct-25

Introduction to NumPy
NumPy (Numerical Python) is a library for fast numerical computation and array manipulation ideal for
data processing and scientific computing. It provides:
1. Multidimensional array objects (ndarray)
2. Mathematical operations
3. Linear algebra tools
4. Random number generation
Example:
import numpy as np
a = [Link]([1, 2, 3])
b = [Link]([4, 5, 6])
print(a + b) # Output: [5 7 9]

NumPy arrays are faster than Python lists because operations are vectorized — executed directly in C
instead of Python loops.

17-Oct-25

Experiment 01. Sum of Squares


Learning Objective: Write a function that takes two numbers and returns the sum of their squares.
Hint: Use the ** operator for power.

Code Hint Example: def sum_of_squares(x, y):


"""
Returns the sum of the squares of two numbers.
Formula: x² + y²
"""
return (x ** 2) + (y ** 2)

# Test
a=3
b=4
result = sum_of_squares(a, b)
print(f"The sum of squares of {a} and {b} is {result}")

17-Oct-25

2
17-Oct-25

Experiment 02: Element-wise Operations


Learning Objective: Use NumPy arrays to perform operations on lists efficiently.

import numpy as np

# Define arrays
a = [Link]([1, 2, 3])
b = [Link]([4, 5, 6])

# Perform element-wise operations


sum_result = a + b

# Display the results


print("Array A:", a)
print("Array B:", b)
print("\nSum (A + B):", sum_result)

17-Oct-25

You might also like