AI_LabManual02
AI_LabManual02
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.
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
Example:
import re
# List of words
words = ["apple", "banana", "cherry", "date"]
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
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()