EX.
NO:1
PROGRAM USING VARIABLES,CONSTANTS,I/O
STATEMENTS IN PYTHON
DATE:
AIM:
To write a Python program to find Area of Circle and Perimeter of a Circle Using Variables,
Constants, I/O Statement.
ALGORITHM:
constant.py
Step1:Start the Execution.
Step2:AssignPIconstantValueto3.14. Step
3: Stop the Execution.
Ex1.py
Step1:Start the Execution.
Step 2: Import constant.py
Step3:Read radius value from user input.
Step 4:Assign area=constant value PI*radius *radius.
Step 5: Calculate perimeter = 2* constant PI * radius.
Step 6: Print Area Value.
Step7:Print Perimeter Value.
Step 8: Stop the Execution.
constant.py
PI=3.14
EX1.py
Import constant
print("PROGRAMUSINGVARIABLES,CONSTANTS,I/OSTATEMENTSIN PYTHON")
print("************************************************************")
print("INPUT")
print("--------")
radius=float(input("Enter Radius Value="))
area = round(constant.PI*radius*radius,2)
perimeter=round(2*constant.PI*radius,2)
print("OUTPUT")
print("======")
print("Area of Circle=",area)
print("Perimeter of Circle=",perimeter)
PROGRAMUSINGVARIABLES,CONSTANTS,I/OSTATEMENTSINPYTHON
**********************************************************************
INPUT
Enter Radius Value=5
OUTPUT
======
Area of Circle= 78.5
Perimeter of Circle=31.4
Result: Thus the above program using Variables, Constants , I/O Statements in Python executed
successfully.
EX.NO:2
PROGRAM USING OPERATORS IN PYTHON
DATE:
AIM:
To write a program for Arithmetic and comparison operations using Operators in Python.
ALGORITHM:
Step1: Start the Execution.
Step 2: Read a value.
Step3: Read b value.
Step 4: Calculate addition of a and b using + operator and Print a + b value.
Step5: Calculate subtraction of a and busing- operator and Print a-b value.
Step6: Calculate multiplication of a and busing *operator and Print a *b value.
Step 7: Calculate division of a and b using / operator and Print a / b value.
Step 8: Calculate reminder of a and b using % operator and Print a % b value.
Step 9: Calculate Exponent of a and b using ** operator and Print a ** b value.
Step 10:Check floor division of a and b using || operator and Print a || b value.
Step11:Check whether a and b is equal using == operator and Print a==b value.
Step12:Check whether a and b is not equal using !=operator and Print a !=b value.
Step 13: Check whether a is Greater than b using > operator and Print a > b value.
Step14:Check whether a is Less than b using< operator and Print a<b value.
Step 15: Stop the Execution.
EX2.py
print("PROGRAMUSINGOPERATORSINPYTHON")
print("****************************************")
print("INPUT")
print("--------")
a=int(input("Enter Value for a:"))
b=int(input("Enter Value for b:"))
print("OUTPUT")
print("======")
print("Addition of a and b :",a+b)
print("Subtraction of a and b :",a-b)
print("Multiplication of a and b :",a*b)
print("Division of a and b :",round(a/b,2))
print("Reminder of a division b :",a%b)
print("Exponent ofa and b :",a**b)print("Floor
Division of a and b :",a//b) print("Check
Whether a equal to b:",a==b) print("Check
Whether a not equal to b:",a!=b) print("Check
Whether a is Greater than b:",a>b)
print("Check Whether a is Less than b:",a<b)
PROGRAMUSINGOPERATORSINPYTHON
****************************************
INPUT
--------
EnterValuefora:5
EnterValueforb:3
OUTPUT
=======
Addition of a and b : 8
Subtraction of a and b : 2
Multiplicationofaandb:15
Division of a and b : 1.67
Reminder of a division b : 2
Exponent of a and b : 125
Floor Division of a and b : 1
Check whether a equal to b: False
Check whether a not equal to b: True
Check whether a is Greater than b: True
Check whether a is Less than b: False
Result: Thus the above program using Arithmetic and Comparison Operator program in Python
executed successfully.
EX.NO:3
PROGRAM USING CONDITIONAL STATEMENTS
DATE:
AIM:
To find largest number among three numbers using Conditional Statements in Python.
ALGORITHM:
Step1:Start the Execution.
Step 2:Read num1,num2,and num3 values from user.
Step 3:Check whether num1greater than num2 and num3 if true assign largest=num1.
Step 4:else Check whether num2greaterthannum1 and num3 if true assign largest as
num2.
Step 5:Otherwise assign largest as num3.
Step 6: Print largest variable value.
Step 7:Stop the Execution.
EX3.py
print("PROGRAMUSINGCONDITIONAL STATEMENTS")
print("*********************************************")
print("INPUT")
print("=====")
num1=int(input("Enter Value for First Number:"))
num2=int(input("EnterValueforSecondNumber:"))
num3=int(input("Enter Value for Third Number:"))
print("OUTPUT")
print("======")
if(num1>=num2)and(num1>=num3):
largest=num1
elif(num2>=num1)and(num2>=num3):
largest=num2
else:
largest=num3
print("The Largest number among the three numbers is:",largest)
PROGRAMUSINGCONDITIONALSTATEMENTS
*********************************************
INPUT
=====
Enter Value for First Number: 15
EnterValueforSecondNumber:20
Enter Value for Third Number: 05
OUTPUT
======
The Largest number among the three numbers is:20
Result: Thus the above program for finding greatest among three numbers using conditional
statement in Python executed successfully.
EX.NO:4
PROGRAM USING LOOPS
DATE:
AIM:
To print multiplication tables in Python using Loops.
ALGORITHM:
Step 1:Start the Execution.
Step 2:Read input number from user and store it in variable number.
Step 3: Assign count=1.
Step 4:Do until count<=15.
Step 5.1:print number *count.
Step 6.2:Incrementcountby1.
Step 7:Stop the Execution.
EX4.py
print("PROGRAMUSINGLOOP")
print("*******************")
print("INPUT")
print("=====")
number= int(input("Enter the number of which you want to print the multiplication
table: "))
count=1
print("OUTPUT")
print("======")
print("The Multiplication Table of:",number)
while count <= 15:
print(number,'x',count,'=',number*count)
count += 1
PROGRAM USING LOOP
***********************
INPUT
=====
Enter the number of which you want to print the multiplication table:5
OUTPUT
======
The Multiplication Tableof:5 5
x1=5
5x2=10
5x3=15
5x4=20
5x5=25
5x6=30
5x7=35
5x8=40
5x9=45
5x10=50
5x11=55
5x12=60
5x13=65
5x14=70
5x15=75
Result: Thus the multiplication table is printed using loops in Python executed successfully.
EX.NO:5
PROGRAM USING JUMP STATEMENTS
DATE:
AIM:
To write a program to implement Jump Statement in Python.
ALGORITHM:
Step1:Start the Execution.
Step 2:Execute until i inrange2to10, incrementby2.
Step 3: If i== 6 then continue print i.
Step 4:Execute until j inrange2to10 incrementby2.
Step 5: If i==6 then break and print j.
Step 6:Stop the Execution.
EX5.py
print("PROGRAMUSINGJUMPSTATEMENTS")
print("************************************")
print("OUTPUT")
print("======")
for i in range(2,10,2):
if i==6:
continue
print(i)
print("End of continue")
for j in range (2,10,2):
if j==6:
break
print(j)
print("End of break")
PROGRAM USING JUMP STATEMENTS
************************************
OUTPUT
======
2
4
8
End of continue
2
4
Result: Thus the above program using jump statements in Python executed successfully.
EX.NO:6
PROGRAM USING FUNCTIONS
DATE:
AIM:
Write a program to perform mathematical operation using functions in Python.
ALGORITHM:
Step 1:Start the Execution.
Step 2: Define function add with two input parameters n1 and n2 and return n1 + n2.
Step 3:Define function sub with two input parameters n1 and n2 and return n1 - n2.
Step 4:Define function multi with two input parameters n1and n2andreturnn1 *n2.
Step 5:Define function div with two input parameters n1 and n2 and return n1 / n2.
Step 6:Readnum1andnum2fromuserinput.
Step 7: Call function add() and print returned value.
Step 8: Call function sub() and print returned value.
Step 9:Call function multi()and print returned value.
Step 10:Call function div() and print returned value.
Step 11: Stop the Execution.
EX6.py
print("PROGRAM USING FUNCTIONS")
print("*****************************")
def add(n1,n2):
returnn1+n2
def sub(n1,n2):
return n1-n2
def multi(n1,n2):
return n1*n2
def div(n1,n2):
returnn1/n2
print("INPUT")
print("=====")
num1 = int(input ("Enter the first number: "))
num2=int(input("Enter the second number:"))
print("OUTPUT")
print("======")
print ("Function Calls to perform arithmetic operations") print("+++++++++++
+++Add Function Call++++++++++++++")
print(num1,"+",num2,"=",add(num1,num2))
print("--------------Subtraction Function Call---------------------------")
print(num1,"-",num2,"=",sub(num1,num2))
print("***************Multiplication Function Call*************")
print(num1,"*",num2,"=",multi(num1,num2))
print("////////////////////////////DivisionFunctionCall///////////////////////")
print(num1,"/",num2,"=",div(num1,num2))
PROGRAM USING FUNCTIONS
INPUT
=====
Enter the first number: 20
Enterthesecondnumber:10
OUTPUT
======
Function Calls to perform arithmetic operations
++++++++++++++Add Function Call++++++++++++++ 20
+ 10 = 30
--------------Subtraction Function Call------------
20 - 10 = 10
***************Multiplication Function Call************* 20
* 10 = 200
////////////////Division Function Call///////////// 20
/ 10 = 2.0
Result: Thus the above program for performing mathematical operations using function in Python
executed successfully.
EX.NO:7
PROGRAM USING RECURSION
DATE:
AIM:
To write a python program to find factorial number using Recursion.
ALGORITHM:
Step 1:Start the Execution.
Step 2:Definefunctionrecur_fact with n value as parameter
Step 3 : If n equal to 1 then return 1
Step 4.2:Otherwise return n*recur_fact(n-1)recursion function call.
Step 5: Read the value for num form user to find the factorial of the number
Step 6:Checkif num less than zero print Factorial not exist f or negative number
Step 7: Also check num equal to zero if zero print factorial=1
Step 8: Else print the returned value of function recur_fact recursive function value as
factorial value
Step 9: stop the Execution.
EX7.py
print("PROGRAMUSINGRECURSION")
print("***********************")
def recur_fact(n):
ifn==1:
returnn
else:
returnn*recur_fact(n-1)
print("INPUT")
print("=====")
num=int(input("Enterthenumbertofindfactorial:"))
print("OUTPUT")
print("======")
if num<0:
print("SorryFactorialdoesnotexistfornegativenumber") elif
num==0:
print("Factorialof0is1")
else:
print("TheFactorialof",num,"is",recur_fact(num))
PROGRAM USING RECURSION
****************************
INPUT
=====
Enterthenumbertofindfactorial: 5
OUTPUT
======
TheFactorialof5 is120
Result: Thus the above program for finding factorial using recursion in Python executed
successfully.
EX.NO:8
PROGRAM USING ARRAYS
DATE:
AIM:
To Write a Python Program to implement Array and its operations.
ALGORITHM:
Step 1: Start the Execution.
Step2:Import array class as arr
Step3:Create array object a with array methods add integer items2,4,6,8
Step 4: Print first item a[0]
Step5:Print second itema[1]
Step 6:Print last element a[-1] by negative indexing
Step7:Using appendmethodadd10 attend of the array
Step8:Using extend method addelements12,14,16intoarraya
Step 9: Remove third item from the list using del a[2]
Step10:Removeelement4fromarrayusing a. remove(4)
Step11:After Slicing array with different index values print the array a[2:4],a[:-3],a[:]
Step 12: Stop the Execution.
EX8.py
Import array as arr
print("PROGRAMUSINGARRAYS")
print("~~~~~~~~~~~~~~~~~~~~~~~")
print("OUTPUT")
print("******")
a=arr.array('i',[2,4,6,8])
print(a)
print("First element:", a[0])
print("Second element:", a[1])
print("Last element:", a[-1])
print("After Adding element a tend")
a.append(10)
print(a)
print("After extending array with new elements")
a.extend([12,14,16])
print(a)
print("After Removing third element") del a[2]
print(a)
print("AfterRemoving4fromarray")
a.remove(4)
print(a)
print("After Slicing array with different index positions")
print(a[2:4])
print(a[:-3])
print(a[2:])
print(a[:])
PROGRAM USING ARRAYS
~~~~~~~~~~~~~~~~~~~~~~~~~
OUTPUT
********
array('i',[2,4,6,8])
Firstelement:2
Secondelement:4
Lastelement:8
After Adding element at end
array('i', [2, 4, 6, 8, 10])
After extending array with new elements
array('i', [2, 4, 6, 8, 10, 12, 14, 16])
After Removing third element
array('i',[2,4,8,10,12,14,16])
After Removing 4 from array
array('i',[2,8,10,12,14,16])
After Slicing array with different index positions
array('i', [10, 12])
array('i',[2,8,10])
array('i',[10,12,14,16])
array('i',[2,8,10,12,14,16])
Result: Thus the above program for array operation in Python executed successfully.
EX.NO:9
PROGRAM USING STRINGS
DATE:
AIM:
To manipulate the strings using various String function in Python.
ALGORITHM:
Step1:Start the Execution.
Step2:Read email value from user.
Step3:Using split function split string values.
Step 4: Assign check=’@.’
Step5:While I in range length of string email
Step 6.1:[email protected] in the string email by using index
Step 7.2: If so increment c by one
Step 8:Check whether c==2 [email protected] in string print Valid E-Mail
Step 9: Otherwise print in valid email.
Step 10:Stop the Execution.
EX9.py
print("PROGRAMUSINGSTRINGS")
print("~~~~~~~~~~~~~~~~~~~~~~~")
print("INPUT")
print("******")
email=input("Enter your E-mail id:")
email.split()
c=0
check='@.'
For I in range(len(email)):
if email[i] in check:
c+=1
print("OUTPUT")
print("******")
ifc==2:
print("Valid E-Mail")
else:
print("In-Valid E-Mail")
PROGRAM USING STRINGS
~~~~~~~~~~~~~~~~~~~~~~~~~~
INPUT
******
EnteryourE-mailid:[email protected]
OUTPUT
********
Valid E-Mail
INPUT
******
Enter your E-mail id:ramkumar@gmailcom
OUTPUT
********
In-Valid E-Mail
Result: Thus the above program for String manipulation in Python executed successfully.
EX.NO:10
PROGRAM USING MODULES
DATE:
AIM:
To write a program in Python using predefined and user defined modules.
ALGORITHM:
city.py
Step1:Start the Execution.
Step2:Assign places=[“Chennai”,”Mumbai”,”Delhi”,”Bangalore”]
Step 3: Stop the Execution.
Ex10.py
Step 1: Start the Execution.
Step2:Import math,random
Step 3: Import city
Step4:Readnvaluefromuser Step
4: Print math.factorial(n)
Step5:Printrandom numberusingrandintbetween20to30 Step
6: Assign c=city.places
Step7:Printc[2]
Step8:StoptheExecution.
city.py
places=[“Chennai”,”Mumbai”,”Delhi”,”Bangalore”]
EX10.py
Import math, random
import city
print("PROGRAMUSINGMODULES")
print("**********************")
print("OUTPUT")
print("=====")
print("Predefined modules math and random:")
n=int(input("Enter n value to find factorial:"))
print("Factorial of entered ",n ,"is:",math.factorial(n))
print("Randomnumberbetween20to30is:",random.randint(20,30)) c=city.places
print("Second place In city list is :",c[1])
PROGRAM USING MODULES
***************************
OUTPUT
=======
Predefined modules math and random:
Enter n value to find factorial:3
Factorial of entered 3is: 6
Random number between 20 to30 is :21
Second place in city list is : Mumbai
Result: Thus the above program using Predefined and User Defined Modules in Python executed
successfully.
EX.NO:11
PROGRAM USING LISTS
DATE:
AIM:
To write a Python program to implement List and its built-in functions.
ALGORITHM:
Step1:Start the Execution.
Step2:Createalistlist1withlistitem10,20,4,45,99
Step 3: Print the elements in the list1
Step4:Sort the list with sort() and print list1aftersorting
Step 5: Print the smallest element after sort as list1[0]
Step6:Find even number in the list by dividing list items by 2 and if reminder zero print
that items.
Step7:Create list2andadd list2withlist1andstore it inlist3andprint list3.
Step 8: Stop the Execution.
EX11.py
print("PROGRAMUSINGLISTS")
print("*******************")
print("OUTPUT")
print("=====")
list1=[10,20,4,45, 99]
print ("Elements in the List are:",list1)
print("After Sorting List elements are:")
list1.sort()
print(list1)
print("Smallest element is:",list1[0])
print("Even Number in the list are:")
for num in list1:
if num%2==0:
print(num,end="")
print("\n After adding two list the list become:")
list2=[100,110]
list3=list1+list2
print(list3)
PROGRAM USING LISTS
************************
OUTPUT
=======
Elements in the List are:[10,20,4,45,99]
After Sorting List elements are:
[4,10,20,45,99]
Smallestelementis:4
Even Number in the list are:
41020
After adding two list the list become:
[4,10,20,45,99,100, 110]
Result: Thus the above program implementing list and its built in function executed successfully.
EX.NO:12
PROGRAM USING TUPLES
DATE:
AIM:
To write a Python program to implement Tuples and its built-in functions.
ALGORITHM:
Step1:Start the Execution.
Step2:Create a tuple languages with item–Python, Java, C++
Step 3: Print the elements in the tuple
Step4:Printthelengthofthetupleusinglen() function
Step5:Check the Element C and Python is present in tuple and print it.
Step6:Create another tuple os with elements–Windows,Linux,and Android
Step 7: Print elements in tuple os
Step8:By using + print both tuples joined together
Step9:Zip both language and os tuple together using zip and print the zipped values.
Step 10: Stop the Execution.
EX12.py
print("PROGRAMUSINGTUPLES")
print("********************")
print("OUTPUT")print("=====")
languages = ('Python', 'Java', 'C++')
print("ElementsintheTuple1are:") for i
in languages:
print(i)
print()
print("The Length of the tuple is:",len(languages))
print("Check the Element C present in tuple:")
print('C' in languages)
print("Check the Element Python present in tuple:")
print('Python' in languages)
os=('Windows','Linux','Android')
print("ElementsintheTuple2are:")
for j in os:
print(j)
print()
print("Joining both tuples:",languages+os)
print("After Zipping languages and os")
zipped=zip(languages,os)
zip1=tuple(zipped)
print(zip1)
PROGRAMUSINGTUPLES
********************************
OUTPUT
=======
Elements in the Tuple1 are:
Python
Java
C++
TheLengthofthetupleis:3
Check the Element C present in tuple: False
Check the Element Python present in tuple: True
ElementsintheTuple2are:
Windows
Linux
Android
Joiningbothtuples:('Python','Java','C++','Windows','Linux','Android')
After Zipping languages and os
(('Python', 'Windows'),('Java', 'Linux'),('C++','Android'))
Result: Thus the above program implementing tuple and its built in function executed
successfully.
EX.NO:13
PROGRAM USING DICTIONARIES
DATE:
AIM:
To write a Python program to implement Dictionaries in Python and its built-in functions.
ALGORITHM:
Step1:Start the Execution.
Step2:Create a Dictionary state_capital with Keys and Values.
Step 3: Print the keys and values in Dictionary state_capital.
Step4:Print the length of the Dictionary using len() function.
Step 5: Access the Dictionary item by key TamilNadu and print it.
Step 6: Change the Value in Dictionary by key Telangana and print it.
Step 7: Add the new item Kerala in the Dictionary and print it.
Step8: Remove the item Karnataka and pint the Dictionary value after deleting.
Step 9: Stop the Execution.
EX13.py
print("PROGRAMUSINGDICTIONARIES")
print("**************************")
print("OUTPUT")
print("=====")
state_capital={
"Tamil Nadu":"Chennai",
"Andhra Pradesh":"Thirupathi",
"Telangana":"Hyderabad",
"Karnataka":"Bengaluru"
}
print("Key and its item sin Dictionary are:")
print(state_capital)
print()
print("The Length of the Dictionary is:",len(state_capital))
print("AccessDictionaryitembyusingTamilNadukey:",state_capital["Tamil Nadu"])
state_capital["AndhraPradesh"]="Amaravati"
print("ChangetheDictionaryvaluebykeyTelangana:",state_capital["Andhra Pradesh"])
print("After Adding Item Kerala the Dictionary values are:")
print()
state_capital["Kerala"]="Thiruvananthapuram"
print(state_capital)
print()
print("After Deleting Item Karnataka the Dictionary Values are:")
del state_capital["Karnataka"]
print(state_capital)
PROGRAM USING DICTIONARIES
********************************
OUTPUT
=======
Key and its items in Dictionary are:
{'TamilNadu':'Chennai','AndhraPradesh':'Thirupathi','Telangana':'Hyderabad',
'Karnataka': 'Bangalore'}
TheLengthoftheDictionaryis:4
Access Dictionary item by using Tamil Nadu key: Chennai
Change the Dictionary value by key Telangana:Amaravati
After Adding Item Kerala the Dictionary values are:
{'TamilNadu':'Chennai','AndhraPradesh':'Amaravati','Telangana':'Hyderabad',
'Karnataka': 'Bangalore', 'Kerala': 'Thiruvananthapuram'}
After Deleting Item Karnataka the Dictionary Values are:
{'TamilNadu':'Chennai','AndhraPradesh':'Amaravati','Telangana':'Hyderabad',
'Kerala': 'Thiruvananthapuram'}
Result: Thus the above program implementing Dictionary and its built in function executed
successfully.
EX.NO:14
PROGRAM FOR FILE HANDLING
DATE:
AIM:
To write a Python for File Handling in Python.
ALGORITHM:
Step1:Start the Execution.
Step 2: Import os module.
Step3:Create functions for create_file, read_file, append_file,delete_file with exceptions.
Step 4: Read file name from user and store it in filename.
Step5:Callfunctioncreate_filewithfilename.
Step 6: Print the content of file using read_file function.
Step7:Read the text to append into the file and store it int1
Step8:Call the function append_file to add the text t1 into the file.
Step 9: Print the content of file using read_file function.
Step10:Delete the function using delete_file function.
Step 11: Stop the Execution.
EX14.py
Import os
print("PROGRAM FOR FILE HANDLING")
print("**************************")
print("OUTPUT")
print("=====")
def create_file(filename):
try:
with open(file name,'w')asf:
f.write('Hello, world!\n')
print("File"+filename+"created successfully.") except
IO Error:
print("Error: could not create file"+ filename)
def read_file(filename):
try:
with open(file name,'r')asf:
contents = f.read()
print(contents)
except IO Error:
print("Error:could not read file"+filename)
def append_file(file name,text):
try:
with open(file name,'a')asf:
f.write(text)
print("Text appended to file"+filename+"successfully.") except
IO Error:
print("Error:couldnotappendtofile"+ filename)
def delete_file(filename):
try:
os.remove(filename)
print("File"+filename+"deleted successfully.") except
IOError:
print("Error:could not delete file"+ filename)
filename= input("Enter the File Name to Create with Extension:")
create_file(filename)
print("The content of the file is:")
read_file(filename)
t1=input("Enter Text to add into the file:")
append_file(filename,t1)
print("After adding the text the file content is:")
read_file(filename)
print("Deleting the file",file name)
delete_file(filename)
PROGRAM FOR FILE HANDLING
*******************************
OUTPUT
=======
Enter the File Name to Create with Extension:sample.txt
File sample.txt created successfully.
The content of the file is :Hello,world!
Enter Text to add into the file:welcometoPython
Text appended to file sample.txt successfully.
After adding the text the file content is:
Hello, world!
Welcome to Python
Deleting the filesample.txt
File sample.txt deleted successfully.
Result: Thus the above file handling program in Python executed successfully.