Affiliated to MG University, Kottayam
Approved by AICTE & Govt. of Kerala.
Accredited by NAAC ‘B’ Grade with
CGPA 2.40 (1st cycle)
PRACTICAL RECORD BOOK
Name:
Course:
Semester: Roll No:.
University Exam Reg.No:
1
CERTIFICATE
Certified that this is the Bonafide record of practical work done by
Mr/Ms ........................................................................... in the Python Lab of
Cochin Arts & Science during the academic year……………....…………………
Signature of staff in charge Signature of H.O.D
Name: Name:
Date :…………………………………
Date of University Practical Examination……………………………………
Name & Signature of Name & Signature of
Internal Examiner External Examiner
2
INDEX
SL DATE PROGRAM PAGE SIGN OF
no NO TEACHER
3
PROGRAM -1
Aim
Write a python program to implement the different Data Types in Python
Source Code
age = 25
print("Age:", age, type(age))#int
height = 5.9
print("Height:", height, type(height))#float
name = "Alice"
print("Name:", name, type(name))#string
is_student = True
print("Is Student:", is_student, type(is_student))#bool
scores = [85, 90, 78, 92]
print("Scores:", scores, type(scores))#list
student = {"name": "Bob", "age": 20, "grade": "A"}
print("Student Info:", student, type(student))#dict
Result
The program ran successfully and the output was verified.
1
Output
2
PROGRAM -2
Aim
Write a python program to implement String Manipulations
Source Code
text = "Hello, Python!"
# Accessing characters
print("First character:", text[0])
# Slicing strings
print("Slice (0-5):", text[0:5])
# String length
print("Length of text:", len(text))
# Changing case
print("Uppercase:", text.upper())
print("Lowercase:", text.lower())
# String concatenation
greeting = "Hi"
name = "Charlie"
message = greeting + " " + name
print("Concatenated String:", message)
# String formatting
age = 30
formatted_string = f"{name} is {age} years old."
print("Formatted String:", formatted_string)
Result
The program ran successfully and the output was verified.
3
Output
4
PROGRAM -3
Aim
Write a python program to implement Arrays using list
Source Code
# Defining a list
numbers = [1, 2, 3, 4, 5]
print("Numbers:", numbers)
# Accessing elements
print("First number:", numbers[0])
# Modifying elements
numbers[2] = 99
print("Modified Numbers:", numbers)
# Adding elements
numbers.append(6)
print("After appending 6:", numbers)
# Removing elements
numbers.remove(99)
print("After removing 99:", numbers)
# Looping through a list
for num in numbers:
print("Number:", num)
Result
The program ran successfully and the output was verified.
5
Output
6
PROGRAM -4
Aim
Write a python program to implement Control Statements in Python
Source Codes
a. if-else statement
age = 18
if age >= 18:
print("You are eligible to vote.")
else:
print("You are not eligible to vote.")
b. for loop
# Using a for loop to iterate over a list
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print("Fruit:", fruit)
c. while loop
# Using a while loop
count = 0
while count < 5:
print("Count:", count)
count += 1
d. break and continue
# Using break
for num in range(1, 10):
if num == 5:
break # Exit the loop when num is 5
print("Number (break):", num)
# Using continue
for num in range(1, 10):
if num == 5:
continue # Skip the rest of the loop when num is 5
print("Number (continue):", num)
Result
The program ran successfully and the output was verified.
7
Outputs
8
PROGRAM -5
Aim
Write a python program to define a function to demonstrate control flow.
Source Code
# Function to check if a number is even or odd
def check_even_odd(number):
if number % 2 == 0:
print(number, "is even.")
else:
print(number, "is odd.")
check_even_odd(10)
check_even_odd(7)
# Function to calculate the sum of elements in a list
def calculate_sum(numbers):
total = 0
for num in numbers:
total += num
return total
numbers_list = [1, 2, 3, 4, 5]
print("Sum of numbers:", calculate_sum(numbers_list))
Result
The program ran successfully and the output was verified.
9
Output
10
PROGRAM -6
Aim
Write a python program to define a basic Function.
Source Code
def greet():
print("Hello, welcome to Python programming!")
# Calling the function
greet()
Result
The program ran successfully and the output was verified.
11
Output
12
PROGRAM -7
Aim
Write a python program to define a function with parameters.
Source Code
def greet_person(name):
print(f"Hello, {name}! How are you today?")
# Calling the function with an argument
greet_person("Alice")
greet_person("Bob")
Result
The program ran successfully and the output was verified.
13
Output
14
PROGRAM -8
Aim
Write a python program to define a function with return.
Source Code
def add_numbers(a, b):
return a + b
# Calling the function and printing the result
result = add_numbers(5, 3)
print("Sum:", result)
Result
The program ran successfully and the output was verified.
15
Output
16
PROGRAM -9
Aim
Write a python program to define a function with default parameter value.
Source Code
def greet_with_default(name="Guest"):
print(f"Hello, {name}!")
# Calling the function without an argument
greet_with_default()
# Calling the function with an argument
greet_with_default("Charlie")
Result
The program ran successfully and the output was verified.
17
Output
18
PROGRAM -10
Aim
Write a python program to define a function with multiple return value.
Source Code
def get_name_and_age():
name = "Eve"
age = 25
return name, age
# Unpacking the return values
person_name, person_age = get_name_and_age()
print("Name:", person_name)
print("Age:", person_age)
Result
The program ran successfully and the output was verified.
19
Output
20
PROGRAM -11
Aim
Write a python program to define a function with variable length arguments.
Source Code
# Using *args for a variable number of arguments
def print_numbers(*args):
for number in args:
print("Number:", number)
# Calling the function with different numbers of arguments
print_numbers(1, 2, 3)
print_numbers(10, 20, 30, 40, 50)
# Using **kwargs for a variable number of keyword arguments
def print_student_info(**kwargs):
for key, value in kwargs.items():
print(f"{key}: {value}")
# Calling the function with different keyword arguments
print_student_info(name="David", age=22, grade="A")
print_student_info(name="Sophia", age=21, major="Physics")
Result
The program ran successfully and the output was verified.
21
Output
22
PROGRAM -12
Aim
Write a python program to define a recursive function.
Source Code
# Function to calculate the factorial of a number using recursion
def factorial(n):
if n == 0 or n == 1:
return 1
else:
return n * factorial(n - 1)
# Calling the recursive function
print("Factorial of 5:", factorial(5))
print("Factorial of 7:", factorial(7))
Result
The program ran successfully and the output was verified.
23
Output
24
PROGRAM -13
Aim
Write a python program to define Lambda Functions.
Source Code
# Using a lambda function to add two number
add = lambda x, y: x + y
# Calling the lambda function
print("Lambda Add Result:", add(4, 5))
# Using a lambda function with the `map` function
numbers = [1, 2, 3, 4, 5]
squared_numbers = map(lambda x: x ** 2, numbers)
print("Squared Numbers:", list(squared_numbers))
Result
The program ran successfully and the output was verified.
25
Output
26
PROGRAM -14
Aim
Write a python program to define a function inside a function (nested function).
Source Code
def outer_function(text):
def inner_function():
print(text)
inner_function()
# Calling the outer function
outer_function("Hello from the inner function!")
Result
The program ran successfully and the output was verified.
27
Output
28
PROGRAM -15
Aim
Write a python program to define a function with documentation (docstring).
Source Code
def multiply(a, b):
"""This function multiplies two numbers and returns the result."""
return a * b
# Accessing the docstring
print(multiply. doc )
# Calling the function
print("Multiplication Result:", multiply(4, 5))
Result
The program ran successfully and the output was verified.
29
Output
30
PROGRAM -16
Aim
Write a program in python to Creating a class & object.
Source Code
# Defining a simple class
class Dog:
# Constructor to initialize the object
def init (self, name, age):
self.name = name # Attribute
self.age = age # Attribute
# Method to display dog information
def display_info(self):
print(f"Dog's Name: {self.name}, Age: {self.age}")
# Creating an object of the Dog class
dog1 = Dog("Buddy", 3)
dog1.display_info()
Result
The program ran successfully and the output was verified.
31
Output
32
PROGRAM -17
Aim
Write a python program to implement the concept of Encapsulation in OOP.
Source Code
class BankAccount:
def init (self, account_number, balance):
self.account_number = account_number # Public attribute
self. balance = balance # Private attribute (encapsulated)
# Method to deposit money
def deposit(self, amount):
if amount > 0:
self. balance += amount
print(f"Deposited: {amount}. New Balance: {self. balance}")
# Method to withdraw money
def withdraw(self, amount):
if amount <= self. balance:
self. balance -= amount
print(f"Withdrew: {amount}. New Balance: {self. balance}")
else:
print("Insufficient balance.")
# Method to check balance
def get_balance(self):
return self. balance
# Creating a BankAccount object
account = BankAccount("12345", 1000)
account.deposit(500)
account.withdraw(300)
print("Balance:", account.get_balance())
# Trying to access the private attribute directly (will raise an error)
# print(account. balance) # This will fail
Result
The program ran successfully and the output was verified.
33
Output
34
PROGRAM -18
Aim
Write a program in python to implement the concept of Abstraction in OOP.
Source Code
from abc import ABC, abstractmethod
# Abstract class
class Animal(ABC):
@abstractmethod
def make_sound(self):
pass
# Derived class
class Dog(Animal):
def make_sound(self):
print("Woof! Woof!")
class Cat(Animal):
def make_sound(self):
print("Meow! Meow!")
# Creating objects
dog = Dog()
cat = Cat()
dog.make_sound()
cat.make_sound()
Result
The program ran successfully and the output was verified.
35
Output
36
PROGRAM -19
Aim
Write a program in python to implement the concept of Inheritance.
Source Code
class Animal:
def init (self, name):
self.name = name
def make_sound(self):
print("Some generic sound")
# Derived class
class Dog(Animal):
def make_sound(self):
print(f"{self.name} says: Woof! Woof!")
# Derived class
class Cat(Animal):
def make_sound(self):
print(f"{self.name} says: Meow! Meow!")
# Creating objects of derived classes
dog = Dog("Buddy")
cat = Cat("Whiskers")
dog.make_sound()
cat.make_sound()
Result
The program ran successfully and the output was verified.
37
Output
38
PROGRAM -20
Aim
Write a program in python to implement the concept of Polymorphism.
Source Code
class Bird:
def fly(self):
print("Bird is flying")
class Sparrow(Bird):
def fly(self):
print("Sparrow is flying swiftly")
class Penguin(Bird):
def fly(self):
print("Penguin can't fly but swims")
# Function demonstrating polymorphism
def make_it_fly(bird):
bird.fly()
# Using polymorphism
sparrow = Sparrow()
penguin = Penguin()
make_it_fly(sparrow)
make_it_fly(penguin)
Result
The program ran successfully and the output was verified.
39
Output
40