Lab Experiment 3 - v01 - AI
Lab Experiment 3 - v01 - AI
3.1 Objectives
Develop and use reusable code by encapsulating tasks in functions.
Package functions into flexible and extensible classes.
3.2 Pre-Lab
The code from the previous lab experiments was limited to short snippets. Solving more complex
problems means more complex code - stretching over hundreds, to thousands, of lines, and—if you
want to reuse that code—it is not convenient to copy and paste it multiple times. Worse, any error is
magnified, and any changes become tedious to manage.
In Python, “functions” are complete suite of functions grouped around a set of related tasks is called a
library or module. Libraries permit you to inherit a wide variety of powerful software solutions
developed and maintained by other people.
Functions in Python are defined using the def keyword, and they play a crucial role in organizing code,
promoting reusability, and improving readability. Here are key aspects and concepts related to
functions in Python:
Function Definition:
Functions are defined using the “def” keyword, followed by the function name and a pair of
parentheses. Any parameters the function takes are listed inside the parentheses.
Function Call:
To execute a function, you "call" it by using its name followed by parentheses. If the function takes
parameters, you provide the values inside the parentheses.
Return Statement:
Functions can return a value using the return keyword. The returned value can be assigned to a
variable or used directly.
Default Parameters:
You can provide default values for parameters. If a value is not passed for a parameter, the default
value is used.
Variable-Length Arguments:
Functions can accept a variable number of arguments using *args and **kwargs.
Lambda Functions:
Also known as anonymous functions, these are small, one-line functions defined using the
lambda keyword.
Scope:
Variables defined inside a function have local scope, meaning they are only accessible within
that function. Variables defined outside functions have global scope.
80 84 A- 3.66
75 79 B+ 3.33
71 - 74 B 3.00
68 70 B- 2.66
64 67 C+ 2.33
61 63 C 2.00
58 60 C- 1.66
54 - 57 D+ 1.30
50 53 D 1.00
Below 50 F 0.00
Calculate the Grade Point Average (GPA) for each student by averaging their Grade Points.
The function should return a list of dictionaries where each dictionary has keys 'name', 'grades' (a list
of course grades), 'grade_points', and 'gpa'.
Create a dictionary which have at least record to five students (Name and Scores in 5 Courses)
Class Declaration: To create a class, you use the class keyword. Here's a simple example of a
class:
Attributes: You can define attributes (characteristics) inside the class. These attributes
represent the data associated with objects of the class.
In this example, the __init__ method is a special method called a constructor. It is called when an
object is created. self refers to the instance of the class (the object), and you can set attributes like
make and model for each object.
Methods: You can also define methods inside the class. Methods are functions associated with
the class.
The display_info method can be called on an instance of the Car class to display information about
the car.
Creating Objects (Instances): Once a class is defined, you can create objects (instances) of
that class.
This creates an instance of the Car class called my_car with the specified make and model.
Here, my_car.make accesses the make attribute, and my_car.display_info() calls the display_info
method.
In-Lab Task 3
Create a Python class named student to manage student information. The class should have the
following attributes:
3. calculate_average: A method to calculate and return the average marks of the student.
Create an instance of the student class, add some marks, and calculate the average.
3.4 Post-Lab
Post-Lab Task
Create a simple library management system using Python. Define a class Book to represent a
book with attributes such as title, author, and availability status. Implement functions to borrow and
return books.
The student performance for the assigned task during the lab session was:
The student could not complete all assigned tasks and showed
Average 2
partial results.