0% found this document useful (0 votes)
2 views40 pages

IBCA Python

The document outlines practical exercises for students in the Department of Computer Science & Application at Bharathi Women's Arts and Science College, focusing on Python programming. It includes various programs demonstrating the use of variables, operators, conditional statements, loops, functions, recursion, arrays, strings, modules, lists, and file handling. Each program is presented with code snippets and expected outputs to facilitate learning and understanding of Python programming concepts.

Uploaded by

nithiyapriya
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views40 pages

IBCA Python

The document outlines practical exercises for students in the Department of Computer Science & Application at Bharathi Women's Arts and Science College, focusing on Python programming. It includes various programs demonstrating the use of variables, operators, conditional statements, loops, functions, recursion, arrays, strings, modules, lists, and file handling. Each program is presented with code snippets and expected outputs to facilitate learning and understanding of Python programming concepts.

Uploaded by

nithiyapriya
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

BHARATHI WOMEN’S ARTS AND SCIENCE COLLEGE

(AN ISO 9001-2000 CERTIFIED INSTITUTION)

THATCHUR, KALLAKURICHI-606213.

DEPARTMENT OF COMPUTER SCIENCE & APPLICATION

NAME : …………………………………………………………………………………………

COURSE : …………………………………………………………………………………………

REGISTER NO : …………………………………………………………………………………………
BHARATHI WOMEN’S ARTS AND SCIENCE COLLEGE
(AN ISO 9001-2000 CERTIFIED INSTITUTION)

THATCHUR, KALLAKURICHI-606213.

DEPARTMENT OF COMPUTER SCIENCE & APPLICATION

CERTIFICATE

Certificate that this is the bonafide record of practical done by


…………………………….………………………………. Register Number ……………………………….
year/Branch …………………In the lab …………………………………………………………During
the academic year………………………

Faculty Incharge Head of the Department

Submitted for the University Practical Examination held on……………………

Internal Examiner External Examiner


SUBJECT CODE: 23UBCAP14 SUBJECT NAME: PYTHON PROGRAMMING LAB

SNO DATE CONTENT PAGE NO SIGN

01 Program using variables, constants, I/O


statements in Python.
02 Program using Operators in Python.

03 Program using Conditional Statements.

04 Program using Loops.

05 Program using Jump Statements.

06 Program using Functions.

07 Program using Recursion.

08 Program using Arrays.

09 Program using Strings.

10 Program using Modules.

11 Program using Lists.

12 Program using Tuples.

13 Program using Dictionaries

14 Program for File Handling.


#PROGRAM: 1: PROGRAM USING VARIABLES, CONSTANTS, I/O STATEMENT IN PYTHON

print("*********************************************")
print("VARIABLES,CONSTANTS,I/O STATEMENTS ")
print("*********************************************")
#variable
a=100
b=200
c=a+b
print("USING VARIABLES")
print("*******************")
print("A= ",a)
print("B= ",b)
print("C= ",c)
Message="Bharathi college"
print("Message= ",Message)
#constant
PI=3.14
radius=float(input("Enter the radius of circle: "))
Circumference=2*PI*radius
area=PI*radius**2
print("USING CONSTANTS")
print("********************")
print("Circumference= ",Circumference)
print("Area= ",area)
#I/O statement
print("USING I/O STATEMENTS")
print("*************************")
Name=input("Enter your name= ")
print("Name= ",Name)
Age=input("Enter your age= ")
print("Age= ",Age)
OUTPUT:

**********************************************
VARIABLES,CONSTANTS,I/O STATEMENTS
**********************************************
USING VARIABLES
*******************
A= 100
B= 200
C= 300
Message= Bharathi college
Enter the radius of circle: 3.5
USING CONSTANTS
********************
Circumference= 21.98
Area= 38.465
USING I/O STATEMENTS
**************************
Enter your name= MINI
Name= MINI
Enter your age= 20
Age= 20
#PROGRAM: 2: PROGRAM USING OPERATORS

print("***************")
print(" OPERATORS ")
print("**************")
#Arthimetic operators
num1=10
num2=5
add=num1+num2
sub=num1-num2
mul=num1*num2
div=num1/num2
mod=num1%num2
exp=num1**num2
floordivision=num1//num2

print(" ARITHEMETIC OPERATORS ")


print("******************************")
print("Addition =",add)
print("Subtraction =",sub)
print("Multiplication =",mul)
print("Division =",div)
print("Modules =",mod)
print("Exponent =",exp)
print("Floor division =",floordivision)
#Comparision operators
num1=10
num2=5
print(" COMPARISION OPERATORS ")
print("******************************")
print("num1>num2 =",num1>num2)
print("num1<num2 =",num1<num2)
print("num1>=num2 =",num1>=num2)
print("num1<=num2 =",num1<=num2)
print("num1==num2 =",num1==num2)
print("num1!=num2 =",num1!=num2)
#Logical operators
is_raining=True
is_sunny=False
print(" LOGICAL OPERATORS ")
print("*************************")
print("AND operator =",is_raining&is_sunny)
print(" OR operator =",is_raining|is_sunny)
print("NOT operator =",is_raining!=is_sunny)
OUTPUT:

***************
OPERATORS
***************
ARITHEMETIC OPERATORS
*****************************
Addition = 15
Subtraction = 5
Multiplication = 50
Division = 2.0
Modules =0
Exponent = 100000
Floor division = 2
COMPARISION OPERATORS
*****************************
num1>num2 = True
num1<num2 = False
num1>=num2 = True
num1<=num2 = False
num1==num2 = False
num1!=num2 = True
LOGICAL OPERATORS
***********************
AND operator = False
OR operator = True
NOT operator = True
#PROGRAM: 3: PROGRAM USING CONDITIONAL STATEMENT

print("*******************************************************")
print(" PROGRAM USING CONDITIONAL STATEMENT(if-elif)")
print("*******************************************************")
name=input("Name:")
dept=input("Departement:")
print("Enter the marks of")
m1=int(input("Tamil:"))
m2=int(input("English:"))
m3=int(input("Python:"))
m4=int(input("C:"))
m5=int(input("Maths:"))
total=m1+m2+m3+m4+m5
percent=total/5

if percent>=80:
grade="A++"
elif percent>=70 and percent<80:
grade="A+"
elif percent>=60 and percent<70:
grade="a"
elif percent>=40 and percent<60:
grade="B"
else:
grade="Fail"
print("\n \n STUDENT DATABASE")
print("---------------------------------")
print("NAME: ",name)
print("DEPARTMENT: ",dept)
print("TAMIL: ",m1)
print("ENGLISH: ",m2)
print("PYTHON: ",m3)
print("C: ",m4)
print("MATHS: ",m5)
print("TOTAL MARKS: ",total)
print("PERCENTAGE: ",percent)
print("GRADE: ",grade)
OUTPUT:

********************************************************
PROGRAM USING CONDITIONAL STATEMENT (if-elif)
********************************************************
Name: MINI
Departement: BCA
Enter the marks of
Tamil: 98
English: 97
Python: 100
C: 100
Maths: 100

STUDENT DATABASE
----------------------------
NAME: MINI
DEPARTMENT: BCA
TAMIL: 98
ENGLISH: 97
PYTHON: 100
C: 100
MATHS: 100
TOTAL MARKS: 495
PERCENTAGE: 99.0
GRADE: A++
#PROGRAM: 4: PROGRAM USING WHILE LOOP

print("****************************")
print("WHILE LOOP STATEMENT ")
print("****************************")
n=int(input("Enter a Positive Integer= "))
if(n<0):
print("No factorial for negative integer")
elif(n==0):
print("The factorial for 0 in 1")
else:
f=1
i=1
while(i<=n):
f=f*i
i=i+1
print("The Factorial of ",n,"is",f)
OUTPUT:

****************************
WHILE LOOP STATEMENT
****************************

Enter a Positive Integer= 5


The Factorial of 5 is 120

Enter a Positive Integer= 6


The Factorial of 6 is 720

Enter a Positive Integer= -5


No factorial for negative integer

Enter a Positive Integer= 0


The factorial for 0 in 1
#PROGRAM USING FOR LOOP

print("*************************")
print("FOR LOOP STATEMENT ")
print("*************************")
n=int(input("Enter a Positive Integer="))
if(n<0):
print("No Factorial for Negative Integer")
elif(n==0):
print("The Factorial of 0 is 1")
else:
f=1
for i in range(1,n+1):
f=f*i
print("The Factorial of",n,"is",f)
OUTPUT:

*************************
FOR LOOP STATEMENT
*************************

Enter a Positive Integer=5


The Factorial of 5 is 120

Enter a Positive Integer=9


The Factorial of 9 is 362880

Enter a Positive Integer=-3


No Factorial for Negative Integer

Enter a Positive Integer=0


The Factorial of 0 is 1
#PROGRAM: 5: PROGRAM USING JUMP STATEMENTS
(BREAK, CONTINUES, AND PASS)

print("************************")
print(" JUMP STATEMENTS ")
print("************************")
print("BREAK STATEMENT")
print("*********************")
for blog in "BHARATHI COLLEGE":
if blog=="O":
break
print(blog,end="")
print()

#PROGRAM USING CONTINUE


print("CONTINUE STATEMENT")
print("*************************")
for blog in "BHARATHI COLLEGE":
if blog=="O":
continue
print(blog,end="")
print()

#PROGRAM USING PASS


print("PASS STATEMENT")
print("*******************")
for blog in "BHARATHI COLLEGE":
if blog=="o":
pass
print(blog,end="")
print()
OUTPUT:

*************************
JUMP STATEMENTS
*************************
BREAK STATEMENT
*********************
B
H
A
R
A
T
H
I

CONTINUE STATEMENT
*************************
B
H
A
R
A
T
H
I

C
L
L
E
G
E

PASS STATEMENT
*******************
B
H
A
R
A
T
H
I

C
O
L
L
E
G
#PROGRAM: 6: PROGRAM USING FUNCTIONS (FOR LOOP)

print("*********************")
print(" FUNCTIONS ")
print("*********************")

def compute_hcd(x,y):
if x>y:
smaller=y
else:
smaller=x
for i in range(1,smaller+1):
if((x%i==0)and(y%i==0)):
hcd=i
return hcd
num1=int(input('Enter First Number:'))
num2=int(input('Enter Second Number:'))
print('The HCD of',num1,'and',num2,'is',compute_hcd(num1,num2))
OUTPUT:

*********************
FUNCTIONS
*********************
Enter First Number: 50
Enter Second Number: 100
The HCD of 50 and 100 is 50

Enter First Number:5


Enter Second Number: 85
The HCD of 5 and 85 is 5
#PROGRAM USING FUNCTIONS (WHILE LOOP)

print("*********************")
print(" FUNCTIONS ")
print("*********************")

def compute_lcm(x,y):
if x>y:
greater=y
else:
greater=x
while(True):
if((greater%x==0)and(greater%y==0)):
lcm=greater
break
greater+=1
return lcm
num1=int(input('Enter First Number:'))
num2=int(input('Enter Second Number:'))
print('The LCM of',num1,'and',num2,'is',compute_lcm(num1,num2))
OUTPUT:

*********************
FUNCTIONS
*********************
Enter First Number: 9
Enter Second Number: 81
The LCM of 9 and 81 is 81

Enter First Number: 5


Enter Second Number: 50
The LCM of 5 and 50 is 50
#PROGRAM: 7 : PROGRAM USING RECURSION

print("*********************")
print(" RECURSION ")
print("*********************")
def recursive_fibonacci(n):
if n<=1:
return n
else:
return(recursive_fibonacci(n-1)+recursive_fibonacci(n-2))
n_terms=10
if n_terms<=0:
print("Invalid input!please input a positive value")
else:
print("Fibonacci series:")
for i in range(n_terms):
print(recursive_fibonacci(i))
OUTPUT:

*********************
RECURSION
*********************
Fibonacci series:
0
1
1
2
3
5
8
13
21
34
#PROGRAM:8: PYTHON PROGRAM FOR SUM OF THE ARRAY
ELEMENTS

#Functions to find sum of elements


print("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")
print(" SUM OF THE ELEMENT USING ARRAYS ")
print("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")

#Approach 1
def sum1(arr):
result=0
for x in arr:
result+=x
return result
#Approach 2
def sum2(arr):
result=sum(arr)
return result
#Main code
arr=[10,20,30,40,50,]
print("sum1=",sum1(arr))
print("sum2=",sum2(arr))
OUTPUT:

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
SUM OF THE ELEMENT USING ARRAYS
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
sum1= 150
sum2= 150
#PROGRAM: 9 : PYTHON PROGRAM TO CHECK IF A STRING IS
PALINDROME OR NOT.

#Function to check palindrome string.


print("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&")
print(" PYTHON PROGRAM USING STRINGS ")
print("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&")
def ispalindrome(string):
string=[Link]("","").lower()
length=len(string)
for i in range(length//2):
if string[i]!=string[(length-i)-1]:
return False
return True
#Main code
string=input("Enter the string:")
if ispalindrome(string):
print(string,"is a palindrome string")
else:
print(string,"is not a palindrome string")
OUTPUT:

&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
PYTHON PROGRAM USING STRINGS
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

Enter the string: MALAYALAM


MALAYALAM is a palindrome string

Enter the string: NISHA


NISHA is not a palindrome string

Enter the string: MALAYalam


MALAYalam is a palindrome string
#PROGRAM: 10 : PROGRAM USING MODULES

#Creating [Link] modules

def addition(num1,num2):
return num1+num2
def subtraction(num1,num2):
return num1-num2
def multiplication(num1,num2):
return num1*num2
def division(num1,num2):
return num1/num2
#Create another program [Link]
#import the Mathoperation program

print("************************")
print(" MODULES PROGRAM ")
print("************************")

import Mathoperation
print("The sum is:",[Link](10,4))
print("The difference is:",[Link](100,34))
print("The multiplication is:",[Link](4,31))
print("The division is:",[Link](200,5))
OUTPUT:

************************
MODULES PROGRAM
************************
The sum is: 14
The difference is: 66
The multiplication is: 124
The division is: 40.0
#PROGRAM:11: PROGRAM USING LIST

print("***************************************")
print("DEMONSTRATING LIST OPERATIONS")
print("***************************************")
list1=[]
print("Blank List:")
print(list)
#creating a list of number
list2=[10,20,30,40,50]
print("List of number:")
print(list2)
print("Accessing a element from the list")

list3=[10,20,30,'Mini','Asha',200000.234]
print(list3)

print(list3[0])
print(list3[5])

#calculating the len()


print(len(list1))
print(len(list2))
print(len(list3))

#Adding of the element in the list


[Link](60)
[Link](70)
print("After addition of three number:")
print(list2)
#addition of element at specific position(using insert method)
[Link](3,'deepika')
print("List after insert operation:")
print(list3)

#removing element for a list


[Link](30)
print("List after removal of one element:")
print(list3)

#reverse a list
[Link]()
print(list3)
OUTPUT:

***************************************
DEMONSTRATING LIST OPERATIONS
***************************************
Blank List:
<class 'list'>
List of number:
[10, 20, 30, 40, 50]
Accessing a element from the list
[10, 20, 30, 'Mini', 'Asha', 200000.234]
10
200000.234
0
5
6
After addition of three number:
[10, 20, 30, 40, 50, 60, 70]
List after insert operation:
[10, 20, 30, 'deepika', 'Mini', 'Asha', 200000.234]
List after removal of one element:
[10, 20, 'deepika', 'Mini', 'Asha', 200000.234]
[200000.234, 'Asha', 'Mini', 'deepika', 20, 10]
#PROGRAM:12: PROGRAM USING TUPLES

print("******************************************")
print("DEMONSTRATING TUPLES OPERATIONS")
print("******************************************")

#creating an empty tuple


empty_tuple=()
print("Enter Tuple: ",empty_tuple)

#creating tuple having integers


int_tuple=(4,6,8,10,12,24)
print("Tuple with Integers: ",int_tuple)

#creating a tuple having objects of different data types


mixed_tuple=(4,"python",9.3)
print("Tuple with different Data Types: ",mixed_tuple)

nested_tuple=("python",{4:5,6:2,8:2},(5,3,5,6))
print("A Nested Tuple: ",nested_tuple)

tuple_1=("python","tuple")
print("Original Tuple is: ",tuple_1)

tuple_1=tuple_1*3
print("New Tuple is: ",tuple_1)

tuple_2=("python","tuple","ordered","immutable")
print("Adding a tuples to the tuples: ")
print(tuple_2+(4,5,6))
OUTPUT:

***********************************************
DEMONSTRATING TUPLES OPERATIONS
***********************************************
Enter Tuple: ()
Tuple with Integers: (4, 6, 8, 10, 12, 24)
Tuple with different Data Types: (4, 'python', 9.3)
A Nested Tuple: ('python', {4: 5, 6: 2, 8: 2}, (5, 3, 5, 6))
Original Tuple is: ('python', 'tuple')
New Tuple is: ('python', 'tuple', 'python', 'tuple', 'python', 'tuple')
Adding a tuples to the tuples: ('python', 'tuple', 'ordered', 'immutable', 4, 5, 6)
#PROGRAM: 13: PROGRAM USING DICTIONARY

print("###############")
print(" DICTIONARY ")
print("###############")
Dict={}
print("Empty Dictionary:")
print(Dict)
Dict=dict({1:'BHARATHI',2:"WOMEN'S",3:'COLLEGE'})
print("\nDictionary with the use of dict():")
print(Dict)

Dict=dict([(1,'BHARATHI'),(2,'COLLEGE')])
print("\nDictionary with each item as a pair:")
print(Dict)
Dict1={}
print("Empty Dictionary:")
print(Dict1)

Dict1[0]='priya'
Dict1[1]='mini'
Dict1[2]='deepika'
print("\nDictionary after adding 3 elements:")
print(Dict1)

Dict1[2]='asha'
print("updated dictionary:")
print(Dict1)
print([Link]())
[Link]({3:"supriya"})
print(Dict1)
OUTPUT:

###############
DICTIONARY
###############
Empty Dictionary: {}
Dictionary with the use of dict(): {1: 'BHARATHI', 2: "WOMEN'S", 3:
'COLLEGE'}
Dictionary with each item as a pair: {1: 'BHARATHI', 2: 'COLLEGE'}
Empty Dictionary: {}
Dictionary after adding 3 elements: {0: 'priya', 1: 'mini', 2: 'deepika'}
updated dictionary: {0: 'priya', 1: 'mini', 2: 'asha'}
dict_keys([0, 1, 2]) {0: 'priya', 1: 'mini', 2: 'asha', 3: 'supriya'}
#PROGRAM: 14 : PROGRAM USING FILE HANDLING

print("*******************")
print("FILE HANDLING ")
print("*******************")

f1=open("[Link]","r")
if f1:
print("File is Opened Successfully")

with open("[Link]","r")as f:
content=[Link]()
print(content)

f2=open("[Link]","w")
[Link]("'Python is a user Friendly Language'")
[Link]()

f2=open("[Link]","a")
[Link]("'Python has an Easy Syntax")
[Link]()

f4=open("[Link]","r")
content1=[Link]()
print(content1)

[Link]()
OUTPUT:

********************
FILE HANDLING
********************

File is Opened Successfully

Python is a general purpose ,dynamic ,high-level and interpreted programming


language.

'Python is a user Friendly Language' Python has an Easy Syntax

You might also like