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

AI_LabManual02

The document is a lab manual for a course on Artificial Intelligence, focusing on Python programming concepts such as regular expressions, lambda functions, and object-oriented programming (OOP). It covers essential OOP principles including classes, inheritance, encapsulation, and polymorphism, along with practical examples and tasks for students. The lab tasks involve writing functions and programs that apply these concepts in various scenarios.

Uploaded by

20i0863 Maryam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

AI_LabManual02

The document is a lab manual for a course on Artificial Intelligence, focusing on Python programming concepts such as regular expressions, lambda functions, and object-oriented programming (OOP). It covers essential OOP principles including classes, inheritance, encapsulation, and polymorphism, along with practical examples and tasks for students. The lab tasks involve writing functions and programs that apply these concepts in various scenarios.

Uploaded by

20i0863 Maryam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Artificial Intelligence

BS (CS) _SP_2024

Lab_02 Manual

Learning Objectives:
1. Regular expressions
2. Lambda Functions.
3. Lambda Functions(regular expression basis)
4. OOP’s Concepts (Polymorphism,Abstractions,Inheritance,Encapsulation)
Lab Manual
Basics of Python

Regular expression:
Many times a lot of data would be stored in files and we may have to pick and change only
relevant portions from a file. Even though there are string functions that allow us to
manipulate strings, when dealing with more complicated requirements, we would need more
powerful tools.

Regular expressions (regex) in Python provide a powerful way to search, manipulate, and
validate strings based on specific patterns. The re module in Python allows you to work with
regular expressions.

Python has a module named ‘re‘ for regular expressions.


Example:

Lambda Functions:
Lambdas are functions without names, in other words they are anonymous functions. They
take inputs and return outputs but do not have a name. They are shortcuts to create simple
temporary functions.

Syntax:
lambda arguments : expression
Example:
f1 = lambda x: x**2

# is equivalent to

def f2(x):
return x**2

Lambda Functions(regular expression basis):


In the context of regular expressions (regex), lambda functions can be used in languages that
support them to define custom behavior for matching patterns. For example, in Python, you
can use lambda functions with the re module to perform regex operations.

Example:
import re

# List of words
words = ["apple", "banana", "cherry", "date"]

# Filter words that start with 'a'


filtered_words = filter(lambda x: re.match("^a", x), words)

# Print the filtered words


print(list(filtered_words))
# Output: ['apple']
OOP Concepts
Python is a multi-paradigm programming language. Meaning, it supports different
programming approach. One of the most popular approach to solve a programming problem
is by creating objects. This is known as Object-Oriented Programming (OOP).

An object has two characteristics:


 Attributes
 Actions (behavior)

The concept of OOP in Python focuses on creating reusable code.


This concept is also known as DRY (Don't Repeat Yourself).
Object Oriented programming is a programming style that is associated with the concept of
Class, Objects and various other concepts revolving around these two, like Inheritance,
Polymorphism, Abstraction, Encapsulation etc.

Classes
A class is a blueprint for creating objects. It serves as a template that defines the attributes and
behaviors of an object.
It encapsulates data (attributes) and methods (functions) that operate on that data. Also,
promote code re-usability and maintainability.
Syntax:
class ClassName:
#class attributes and methods
def functionName:
Example:
class Dog:
def init (self, name):
self.name = name
def bark(self):
return f"{self.name} says woof!"
dog1 = Dog("Buddy")
dog2 = Dog("Max")
print(dog1.bark())
print(dog2.bark())

Objects:
An object is an instance of a class. It is a concrete entity created based on the blueprint
defined by the class.
Inheritance:
Inheritance is a mechanism where a new class (subclass) can inherit properties and behaviors
from an existing class (superclass). This promotes code reusability and allows for the creation
of specialized classes.

Syntax:
class SubClassName(SuperClassName):
# Subclass definition
# Additional methods/attributes here
Pass
Example:
class Animal:
def speak(self):
return "Animal speaks"

class Dog(Animal):
def bark(self):
return "Dog barks"

dog = Dog()
print(dog.speak())
print(dog.bark())

Encapsulation:
Encapsulation refers to the bundling of data (attributes) and methods that operate on the data
within a single unit (class). It hides the internal state of an object from the outside world and
allows controlled access to it through methods.

Syntax:
class ClassName:
def init (self, parameters):
self. attribute_name = value # Private attribute

def get_attribute_name(self):
return self. attribute_name

Example:
class Car:
def init (self, make, model):
self. make = make
self. model = model

def get_make(self):
return self. make

def get_model(self):
return self. model

car = Car("Toyota", "Corolla")


print(car.get_make())
print(car.get_model())
Polymorphism:
Polymorphism allows objects of different classes to be treated as objects of a common super
class. It enables flexibility by allowing methods to behave differently based on the object they
operate on.

Syntax:
class SuperClassName:
def method_name(self):
# Method definition
pass

class SubClassName1(SuperClassName):
def method_name(self):
# Overridden method definition
pass

class SubClassName2(SuperClassName):
def method_name(self):
# Overridden method definition
Pass

Example:
class Animal:
def speak(self):
return "Animal speaks"

class Dog(Animal):
def speak(self):
return "Dog barks"

class Cat(Animal):
def speak(self):
return "Cat meows"

def make_sound(animal):
print(animal.speak())

dog = Dog()
cat = Cat()

make_sound(dog) # Output: Dog barks


make_sound(cat) # Output: Cat meows
Lab Task
1. Write a lambda function to sort a list of tuples based on the second element of each tuple
in descending order.
2. Write a Python program that validates email addresses based on the following rules:
 The email address must contain the "@" symbol.
 The part before "@" can contain alphanumeric characters, dots (.), underscores (_), and
hyphens (-).
 The part after "@" must contain a domain name with at least one dot (.) and only
alphanumeric characters.
 The domain name must end with a valid top-level domain (e.g., .com, .org, .net).
3. Use lambda functions and regular expressions to filter out words from a given list that
start with a vowel (a, e, i, o, u).
4. Create a base class Shape with an abstract method area(). Then, create two subclasses
Rectangle and Circle that inherit from Shape. Implement the area() method in both
subclasses to calculate the area of a rectangle and a circle, respectively. Finally, create a
function calculate_total_area() that takes a list of shapes and calculates the total area of
all shapes.
5. Create a base class Animal with properties name and sound. Then, create two
subclasses Dog and Cat that inherit from Animal. Implement the sound property in
each subclass to return the appropriate sound ("woof" for Dog and "meow" for Cat).
Finally, create instances of both subclasses and display their names and sounds.
Additionally, create a method named make_sound() in the base class Animal that prints
the sound of the animal. Override this method in each subclass to include the animal's
name in the output.

You might also like