CS406 Python Lab April-2024
CS406 Python Lab April-2024
Lab Manual
CS406 Programming Practices
(c) Python
NAME:
ENROLLMENT NUMBER:
Faculty Incharge
Prof.Pankaj K.Jain/ Prof. Kamaljeet S.kalsi
CSE, GGCT
Session: 2023-24
List of Experiments
SIGN
CONDUCTION
S.NO AIM WITH GRADE(10)
DATE
DATE
def calculator():
while True:
# Print options for the user
print("Enter '+' to add two numbers")
print("Enter '-' to subtract two numbers")
print("Enter '*' to multiply two numbers")
print("Enter '/' to divide two numbers")
print("Enter 'quit' to end the program")
Patterns can be printed in python using simple for loops. First outer loop is used to
handle the number of rows and the Inner nested loop is used to handle the number of
columns. Manipulating the print statements, different number patterns, alphabet patterns, or
star patterns can be printed.
# printing stars
print("* ",end="")
# Driver Code
n = 5
pypart(n)
Aim-04- Python program to check palindrome Number.
Palindrome algorithm
num=int(input("enter number"))
print("num====", num)
num1=num
sum=0
while(num>0):
rem=num%10
sum=sum*10+rem
num=int(num/10)
print("sum==", sum)
if num1==sum:
print("number is palandrome")
else:
print("number is not palandrome")
Aim-05- write python programs to demonstrate TUPLE python collection.
There are four collection data types in the Python programming language:
Tuple
Tuple is one of 4 built-in data types in Python used to store collections of data, the
other 3 are List, Set, and Dictionary, all with different qualities and usage.
# Empty tuple
my_tuple = ()
print(my_tuple)
--------------------------------------------
my_tuple = (1, 2, 3)
print(my_tuple)
--------------------------------------------
print(my_tuple)
--------------------------------------------
# nested tuple
print(my_tuple)
--------------------------------------------
print(thistuple[1])
--------------------------------------------
print(thistuple[-1])
--------------------------------------------
print(thistuple[2:5])
----------------------------------------------
print(thistuple[:4])
--------------------------------------------------------------
if "apple" in thistuple:
--------------------------------------------
my_tuple = ('p', 'r', 'o', 'g', 'r', 'a', 'm', 'i', 'z')
print(my_tuple[1:4])
# elements beginning to 2nd
print(my_tuple[:-7]))
print(my_tuple[7:])
print(my_tuple[:])
Aim-06- Write python programs to demonstrate LIST python collection.
Python lists are mutable type which implies that we may modify its element after it
has been formed.
The items in the list are separated with the comma (,) and enclosed with the square
brackets [].
# a simple list
print(list1)
print(list2)
print(type(list1))
print(type(list2))
---------------------------
print(thislist[1])
--------------------------------------------
print(thislist[-1])
----------------------------------------------
---------------------------------------------------
print(thislist[:4])
--------------------------------------------------
print(thislist[2:])
--------------------------
thislist[1] = "blackcurrant"
print(thislist)
----------------------------------------------------------------
Change the values "banana" and "cherry" with the values "blackcurrant" and
"watermelon":
print(thislist)
-----------------------------------------------------------------
print(thislist)
-------------------------------------------------------------------
Change the second and third value by replacing it with one value:
thislist[1:3] = ["watermelon"]
print(thislist)
-----------------------------------
Some more examples
# Initialize list
List = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print(List[3:9:2])
print(List[::2])
print(List[::])
Aim-07- Python program to creates the instance of the class and object
The process of inheriting the properties of the parent class into a child class is called inheritance.
The existing class is called a base class or parent class and the new class is called a subclass or child
class or derived class.
In inheritance, the child class acquires all the data members, properties, and functions from the parent
class. Also, a child class can also provide its specific implementation to the methods of the parent
class.
1. Single inheritance
2. Multiple Inheritance
3. Multilevel inheritance
4. Hierarchical Inheritance
5. Hybrid Inheritance
For example, In the real world, Car is a sub-class of a Vehicle class. We can create a Car by
inheriting the properties of a Vehicle such as Wheels, Colors, Fuel tank, engine, and add extra
properties in Car as required.
Single Inheritance
# Base class
class Vehicle:
def Vehicle_info(self):
print('Inside Vehicle class')
# Child class
class Car(Vehicle):
def car_info(self):
print('Inside Car class')
# Parent class 1
class Person:
def person_info(self, name, age):
print('Inside Person class')
print('Name:', name, 'Age:', age)
# Parent class 2
class Company:
def company_info(self, company_name, location):
print('Inside Company class')
print('Name:', company_name, 'location:', location)
# Child class
class Employee(Person, Company):
def Employee_info(self, salary, skill):
print('Inside Employee class')
print('Salary:', salary, 'Skill:', skill)
# access data
emp.person_info('Jessa', 28)
emp.company_info('Google', 'Atlanta')
emp.Employee_info(12000, 'Machine Learning')
Multilevel inheritance
# Base class
class Vehicle:
def Vehicle_info(self):
print('Inside Vehicle class')
# Child class
class Car(Vehicle):
def car_info(self):
print('Inside Car class')
# Child class
class SportsCar(Car):
def sports_car_info(self):
print('Inside SportsCar class')
Hierarchical Inheritance
class Vehicle:
def info(self):
print("This is Vehicle")
class Car(Vehicle):
def car_info(self, name):
print("Car name is:", name)
class Truck(Vehicle):
def truck_info(self, name):
print("Truck name is:", name)
obj1 = Car()
obj1.info()
obj1.car_info('BMW')
obj2 = Truck()
obj2.info()
obj2.truck_info('Ford')
Hybrid Inheritance
class Vehicle:
def vehicle_info(self):
print("Inside Vehicle class")
class Car(Vehicle):
def car_info(self):
print("Inside Car class")
class Truck(Vehicle):
def truck_info(self):
print("Inside Truck class")
# create object
s_car = SportsCar()
s_car.vehicle_info()
s_car.car_info()
s_car.sports_car_info()
Multilevel inheritance
In multilevel inheritance, a class inherits from a child class or derived class. Suppose three classes A,
B, C. A is the superclass, B is the child class of A, C is the child class of B. In other words, we can
say a chain of classes is called multilevel inheritance.
# Base class
class Vehicle:
def Vehicle_info(self):
print('Inside Vehicle class')
# Child class
class Car(Vehicle):
def car_info(self):
print('Inside Car class')
# Child class
class SportsCar(Car):
def sports_car_info(self):
print('Inside SportsCar class')
Hierarchical Inheritance
In Hierarchical inheritance, more than one child class is derived from a single parent class. In other
words, we can say one parent class and multiple child classes.
class Vehicle:
def info(self):
print("This is Vehicle")
class Car(Vehicle):
def car_info(self, name):
print("Car name is:", name)
class Truck(Vehicle):
def truck_info(self, name):
print("Truck name is:", name)
obj1 = Car()
obj1.info()
obj1.car_info('BMW')
obj2 = Truck()
obj2.info()
obj2.truck_info('Ford')
Aim-09- Python program to demonstrate the File Input / Output operations
# open a file
file1 = open("test.txt", "r")
When we are done with performing operations on the file, we need to properly close the file.
Closing a file will free up the resources that were tied with the file. It is done using the close() method
in Python. For example,
# open a file
file1 = open("test.txt", "r")
There are a number of ways we can take to get the current date. We will use the date class of
the date time module to accomplish this task.
The date time library provides necessary methods and functions to handle the following scenarios.
today = date.today()
print("Today's date:", today)
If we need to get the current date and time, you can use the datetime class of the datetime module.
# dd/mm/YY H:M:S
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
print("date and time =", dt_string)