Python Program NEW
Python Program NEW
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
Step2:AssignPIconstantValueto3.14. Step
Ex1.py
PI=3.14
EX1.py
Import constant
print("PROGRAMUSINGVARIABLES,CONSTANTS,I/OSTATEMENTSIN PYTHON")
print("************************************************************")
print("INPUT")
print("--------")
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
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:
Step5: Calculate subtraction of a and busing- 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.
print("PROGRAMUSINGOPERATORSINPYTHON")
print("****************************************")
print("INPUT")
print("--------")
print("OUTPUT")
print("======")
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:
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.
print("PROGRAMUSINGCONDITIONAL STATEMENTS")
print("*********************************************")
print("INPUT")
print("=====")
num2=int(input("EnterValueforSecondNumber:"))
print("OUTPUT")
print("======")
if(num1>=num2)and(num1>=num3):
largest=num1
elif(num2>=num1)and(num2>=num3):
largest=num2
else:
largest=num3
INPUT
=====
EnterValueforSecondNumber:20
OUTPUT
======
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:
ALGORITHM:
Step 2:Read input number from user and store it in variable number.
Step 6.2:Incrementcountby1.
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(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:
ALGORITHM:
print("PROGRAMUSINGJUMPSTATEMENTS")
print("************************************")
print("OUTPUT")
print("======")
for i in range(2,10,2):
if i==6:
continue
print(i)
print("End of continue")
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:
ALGORITHM:
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.
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
=====
Enterthesecondnumber:10
OUTPUT
======
Result: Thus the above program for performing mathematical operations using function in Python
executed successfully.
EX.NO:7
PROGRAM USING RECURSION
DATE:
AIM:
ALGORITHM:
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:
ALGORITHM:
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[:]
array('i',[2,4,6,8])
Firstelement:2
Secondelement:4
Lastelement:8
array('i',[2,4,8,10,12,14,16])
array('i',[2,8,10,12,14,16])
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:
ALGORITHM:
print("PROGRAMUSINGSTRINGS")
print("~~~~~~~~~~~~~~~~~~~~~~~")
print("INPUT")
print("******")
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
******
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:
ALGORITHM:
city.py
Step2:Assign places=[“Chennai”,”Mumbai”,”Delhi”,”Bangalore”]
Ex10.py
Step2:Import math,random
Step4:Readnvaluefromuser Step
4: Print math.factorial(n)
6: Assign c=city.places
Step7:Printc[2]
Step8:StoptheExecution.
city.py
places=[“Chennai”,”Mumbai”,”Delhi”,”Bangalore”]
EX10.py
import city
print("PROGRAMUSINGMODULES")
print("**********************")
print("OUTPUT")
print("=====")
print("Randomnumberbetween20to30is:",random.randint(20,30)) c=city.places
OUTPUT
=======
Result: Thus the above program using Predefined and User Defined Modules in Python executed
successfully.
EX.NO:11
AIM:
ALGORITHM:
Step2:Createalistlist1withlistitem10,20,4,45,99
Step6:Find even number in the list by dividing list items by 2 and if reminder zero print
that items.
print("PROGRAMUSINGLISTS")
print("*******************")
print("OUTPUT")
print("=====")
list1=[10,20,4,45, 99]
list1.sort()
print(list1)
if num%2==0:
print(num,end="")
list2=[100,110]
list3=list1+list2
print(list3)
PROGRAM USING LISTS
************************
OUTPUT
=======
[4,10,20,45,99]
Smallestelementis:4
41020
[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:
ALGORITHM:
Step4:Printthelengthofthetupleusinglen() function
Step5:Check the Element C and Python is present in tuple and print it.
Step9:Zip both language and os tuple together using zip and print the zipped values.
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
ElementsintheTuple2are:
Windows
Linux
Android
Joiningbothtuples:('Python','Java','C++','Windows','Linux','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:
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.
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
{'TamilNadu':'Chennai','AndhraPradesh':'Amaravati','Telangana':'Hyderabad',
{'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
AIM:
ALGORITHM:
Step5:Callfunctioncreate_filewithfilename.
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.
print("**************************")
print("OUTPUT")
print("=====")
def create_file(filename):
try:
f.write('Hello, world!\n')
IO Error:
def read_file(filename):
try:
contents = f.read()
print(contents)
except IO Error:
try:
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)
IOError:
create_file(filename)
read_file(filename)
append_file(filename,t1)
read_file(filename)
delete_file(filename)
PROGRAM FOR FILE HANDLING
*******************************
OUTPUT
=======
Enter the File Name to Create with Extension:sample.txt
Result: Thus the above file handling program in Python executed successfully.