Lecture - 4
Lecture - 4
❑ Functions
▪ User – Defined
▪ Built – in
❑ Libraries
Functions
▪ Functions are a powerful tool in Python that help you write more
organized, efficient, and maintainable code.
✓ Modularity
✓ Code reuse
Functions...
(a Stamp of an institution)
▪ When you call a function, you provide the actual values that you want to
pass to the function's parameters. These values are called arguments.
▪ Example:
▪ Return Values:
▪ Functions can return values back to the caller using the return keyword.
Scope:
▪ Variables defined within a function are local to that function
and can only be accessed within the function's code block.
▪ Functions created by the user for specific tasks using the def keyword.
print(my_function("Yisak"))
User Defined Functions
▪Recursive Functions
User Defined Functions
▪Higher-Order Functions
Exercises
Solution Using User-Defined Function
1) Write a function
def find_maximum(numbers):
that takes a list if not numbers:
raise ValueError # input check
of numbers as
input and returns max_value = numbers[0]
for number in numbers:
the maximum if number > max_value:
max_value = number
value in the list. return max_value
# Example
numbers_list = [31, 72, 26, 95, 47]
print(find_maximum(numbers_list))
Exercises
def find_maximum(numbers):
if not numbers:
raise ValueError # list check error
return max(numbers) # max() is built-in function
Exercises...
print(num)
# Example:
print_odd_numbers(7)
Exercises...
Class Exercise 4
divisors = []
for i in range(1, num):
if num % i == 0:
divisors.append(i)
divisors_sum = sum(divisors)
return divisors_sum == num
More on Functions in Python
✓ develop applications
✓ automate tasks
✓ manipulate data
▪ NumPy
✓Fundamental library for numerical computing, supports
arrays and matrices.
Python Popular Library...
▪ pandas
✓ Powerful data manipulation and analysis tool; great for working with
structured data like tables.
▪ Dask
✓Parallel and distributed computing for large datasets.
▪ Polars
✓A lightning-fast Data Frame library with a Rust backend.
Python Popular Library...
scikit-learn
✓Easy-to-use machine learning library with support for
regression, classification, clustering, etc.
TensorFlow
✓Open-source library for deep learning and neural networks.
Python Popular Library...
PyTorch
✓Dynamic computation graph library favored for deep learning research.
XGBoost
✓Gradient boosting library for efficient and accurate machine learning models.
Keras
✓High-level neural networks API running on top of TensorFlow.
Python Popular Library...
3. Data Visualization
5. Scientific Computing
▪ SciPy: Extends NumPy for complex scientific computations.
▪ SymPy: Library for symbolic mathematics.
▪ NetworkX: For analysing and visualizing complex networks.
Python Popular Library...
7. Testing
▪ unittest: Built-in library for unit testing.
▪ pytest: Powerful and flexible testing framework.
▪ mock: Library for creating mock objects in testing.
8. Game Development
▪ Pygame: Framework for building 2D games.
▪ Godot-Python: Integrates Python scripting into
Godot game engine.
Python Popular Library...
9. Database Interaction
▪ Import Libraries
▪ Math Library
▪ Date Time
▪ Calendar Application
DEMONSTRATION SESSION ON THE SELECTED LIBRARIES
• https://www.mygreatlearning.com/blog/open-source-python-
libraries/
Thank you,
Questions?