PP Journal Programs 1 To 13
PP Journal Programs 1 To 13
Here are the ways with which we can run a Python script.
1. Interactive Mode
2. Command Line
3. Text Editor
Interactive Mode:
In Interactive Mode, you can run your script line by line in a sequence. To enter in an interactive
mode, you will have to open Command Prompt on your windows machine and type ‘python’ and
press Enter.
Command Line
To run a Python script store in a ‘.py’ file in command line, we have to write ‘python’ keyword
before the file name in the command prompt.
python hello.py
Text Editor
output
output
=======================
a=20 b=10 c=8 d=5
R=(a+b)*(c-d)= 90
#Experiment-->2C
#Program to evaluate operator precedence
a=10
b=20
c=30
d=40
print("a=%d b=%d c=%d d=%d \n " %(a,b,c,d))
e=(a+b)*c/d
print("Value of (a+b)*c/d is ",e)
e=((a+b)*c)/d
print("Value of ((a+b)*c)/d is ",e)
e=(a+b)*(c/d)
print("Value of (a+b)*(c/d) is ",e)
e=a+(b*c)/d
print("Value of a+(b*c)/d is ",e)
output
======================
a=10 b=20 c=30 d=40
#Experiment-->3A
# Program to find largest of 2 numbers
print("enter 2 numbers to display largest ")
a=int(input("a="))
b=int(input("b="))
if a>b:
print(a,” is large")
else :
print(b,” is large")
output
======================
enter 2 numbers to display largest
a=13
b=18
18 is large
Algorithm to perform addition, subtraction, multiplication and division operations
using elif
#Experiment --3B
#Program to perform addition, subtraction, multiplication and division operations
using elif
print("calculator")
a=int(input("enter one number "))
b=int(input("enter second number "))
print("1.add\t 2.subtract\t 3.multiply\t 4.divide")
ch=int(input("enter your choice of operation(1-4): "))
if ch==1:
sm=a+b
print("the sum is =",sm)
elif ch==2:
sub=a-b
print("the difference is =",sub)
elif ch==3:
pro=a*b
print("the product is =",pro)
elif ch==4:
div=a/b
print("the Quotient is =",div)
else :
print("invalid choice")
output
=========
calculator
enter one number 30
enter second number 2
1.add 2.subtract 3.multiply 4.divide
enter your choice of operation(1-4): 3
the product is = 60
#Experiment –->4A
#program to display multiplication table of a number using while loop
print("multiplication table")
n=int(input("enter the number ="))
i=1
while i<=10 :
r=n*i
print("%d X %d =%d" %(n,i,r))
i=i+1
print("done")
output
======================
multiplication table
enter the number =5
5 X 1 =5
5 X 2 =10
5 X 3 =15
5 X 4 =20
5 X 5 =25
5 X 6 =30
5 X 7 =35
5 X 8 =40
5 X 9 =45
5 X 10 =50 done
#Experiment –->4B
# Program to check weather given number is prime or not
y=0
n=int(input("read a number :"))
for i in range(2,n):
if(n%i==0):
y=1
break
if(y==1):
print("%d is not a prime number" %n)
else:
print("%d is a prime number" %n)
output 1 output 2
========= =============
a)
for i in range(1,5+1):
for j in range(1,i+1):
print( j,end=" ")
print(" ")
b)
for i in range(6,1,-1):
for j in range(1,i):
print( j,end=" ")
print(" ")
c)
n=50
for i in range(1,n,2):
print(i,end=' ')
d)
n=50
for i in range(1,n+1,2):
if(i%7==0):
continue
print(i,end=' ')
#Experiment -5A
#program to implement set operations
months={"january","march","April","may","june","july","august","september"}
print(months)
months.add("February")
print("after adding")
print(months)
months.discard("august")
print("after deleting")
print(months)
months.pop()
print("after removing last item")
print(months)
print("")
print("union of 2 sets")
days1={"monday","tuesday","wednesday","thursday","sunday"}
days2={"friday","saturday","sunday"}
print(days1)
print(days2)
print("union of 2 sets")
print("-----------------")
days=days1.union(days2)
print(days,"\n")
print("intersection of 2 sets")
print("-----------------")
days=days1.intersection(days2)
print(days,"\n")
print("difference between 2 sets")
print("-----------------")
days=days1.difference(days2)
print(days)
Output
=========
{'march', 'june', 'august', 'september', 'April', 'july', 'may', 'january'}
after adding
{'march', 'june', 'august', 'september', 'April', 'july', 'may', 'january', 'February'}
after deleting
{'march', 'june', 'september', 'April', 'july', 'may', 'january', 'February'}
after removing last item
{'june', 'september', 'April', 'july', 'may', 'january', 'February'}
union of 2 sets
{'tuesday', 'wednesday', 'thursday', 'sunday', 'monday'}
{'saturday', 'friday', 'sunday'}
union of 2 sets
-----------------
{'saturday', 'tuesday', 'wednesday', 'thursday', 'sunday', 'friday', 'monday'}
intersection of 2 sets
-----------------
{'sunday'}
output
=========
(4, 6, 10, 30, 40, 13)
(1, 5, 20, 3, 44, 11)
basic operations of tuples
---------------------------------
(4, 6, 10, 30, 40, 13, 1, 5, 20, 3, 44, 11)
(4, 6, 10, 30, 40, 13, 4, 6, 10, 30, 40, 13, 4, 6, 10, 30, 40, 13)
False
True
4 6 10 30 40 13 1 5 20 3 44 11 187
#Experiment -->6A
#Program to add items to the list
#and find sum of odd and even numbers of the list
lst=[]
sume=0
sumo=0
size=int(input("enter size ="))
for i in range(size) :
item=int(input("enter a number "))
lst.append(item)
if(lst[i]%2==0):
sume=sume+lst[i]
else :
sumo=sumo+lst[i]
print("the list items are",lst)
print("sum of even numbers =",sume)
print("sum of odd numbers =",sumo)
Output
=======================
enter size =6
enter a number 10
enter a number 15
enter a number 20
enter a number 2
enter a number 3
enter a number 5
the list items are [10, 15, 20, 2, 3, 5]
sum of even numbers = 32
sum of odd numbers = 23
#program 6B
#Program to search a given number in the list
lst=[]
yes=0
x=int(input("enter the size "))
print("enter %d elements" %x)
for i in range(x):
elem=int(input(":"))
lst.append(elem)
sk=int(input("enter the search key :"))
for i in range(x):
if(lst[i]==sk):
yes=1
loc=i
break
if(yes==1):
print("the search key %d is present at %d location " %(sk,loc+1))
else:
print("the search key %d is not present in the list " %sk)
output
=========
enter the size 5
enter 5 elements
:10
:23
:45
:56
:41
enter the search key :45
the search key 45 is present at 3 location
#Program no --> 7A
#program to implement dictionary,
anjuman={"ce":{1:30,3:20,5:10},"cs":{1:36,3:10,5:24},
"ec":{1:36,3:10,5:10},"ee":{1:60,3:50,5:40},
"me":{1:36,3:38,5:22},"ph":{1:25,3:25,5:25}
}
rp=1
print("list of branches in anjuman")
for x in anjuman.keys():
print(x)
while(rp==1):
print("to know the students strength of branches")
print("1:ce 2:cs 3:ec 4:ee 5:me 6:ph")
ch=int(input("enter your choice :"))
if (ch==1):
sem=int(input("enter sem "))
if(sem==1 or sem==3 or sem==5):
st=anjuman["ce"][sem]
else:
print("enter 1 / 3 / 5 sem")
continue
print(st)
elif(ch==2):
sem=int(input("enter sem "))
if(sem==1 or sem==3 or sem==5):
st=anjuman["cs"][sem]
else:
print("enter 1 / 3 / 5 sem")
continue
print(st)
elif(ch==3):
sem=int(input("enter sem "))
if(sem==1 or sem==3 or sem==5):
st=anjuman["ec"][sem]
else:
print("enter 1 / 3 / 5 sem")
continue
print(st)
elif(ch==4):
sem=int(input("enter sem "))
if(sem==1 or sem==3 or sem==5):
st=anjuman["ee"][sem]
else:
print("enter 1 / 3 / 5 sem")
continue
print(st)
elif(ch==5):
sem=int(input("enter sem "))
if(sem==1 or sem==3 or sem==5):
st=anjuman["me"][sem]
else:
print("enter 1 / 3 / 5 sem")
continue
print(st)
elif(ch==6):
sem=int(input("enter sem "))
if(sem==1 or sem==3 or sem==5):
st=anjuman["ph"][sem]
else:
print("enter 1 / 3 / 5 sem")
continue
print(st)
else:
print("invalid")
rp=int(input("want to continue 1/0 "))
print("thank you")
output
list of branches in anjuman
ce
cs
ec
ee
me
ph
to know the students strength of branches
1:ce 2:cs 3:ec 4:ee 5:me 6:ph
enter your choice :2
enter sem 3
10
want to continue 1/0 0
thank you
#Program no --> 7B
# Program to perform indexing,iterating in dictionary
GADAG={}
GADAG[1]="gadag"
GADAG[2]="mundaragi"
GADAG[3]="nargund"
GADAG[4]="Ron"
GADAG[5]="Shirahatti"
print("taluks of GADAG district")
for x in GADAG.values():
print(x)
==========
taluks of GADAG district
gadag
mundaragi
nargund
Ron
Shirahatti
#Program no --> 7C
#Program to perform dictionary comprehension
sqrs={x:x*x for x in range(11) }
print(sqrs)
==========
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81, 10: 100}
#Program 8(a) : Code, execute and debug programs to perform string
manipulation.
msg = "Hello and wel come to the world of python"
print(msg)
===================o/p==========
Hello and wel come to the world of python
Hello and wel come to the world of python
w
41
2
1
number of spaces 8
['Hello', 'and', 'wel', 'come', 'to', 'the', 'world', 'of', 'python']
False
True
False
True
..........
Hello and wel come back to the world of python
Hello and wel come to the world of python
HELLO AND WEL COME TO THE WORLD OF PYTHON
hello and wel come to the world of python
nohtyp fo dlrow eht ot emoc lew dna olleH
#program 8(B)Code, execute and debug programs to perform array manipulation
import array as arr
a1=arr.array("d",[10.8,7.8,3.8,5.6,6.8,5.7])
l=len(a1)
for i in range(l):
print(a1[i],end=" ")
print()
a=arr.array("i",[5,7,8,4,5,8,3,2])
l=len(a)
for i in range(l):
print(a[i],end=" ")
print("\n poped element is ",a.pop())
l=len(a)
print(l)
for i in range(l):
print(a[i],end=" ")
x=a.index(5)
print("\n index ",x)
a[0]=10
print(a)
sliced=a[1:5]
print(sliced)
================
10.8 7.8 3.8 5.6 6.8 5.7
5 7 8 4 5 8 3 2
poped element is 2
7
5784583
index 0
array('i', [10, 7, 8, 4, 5, 8, 3])
array('i', [7, 8, 4, 5])
#Program 9(a) : Write a Python program to convert the
# decimal number into hexadecimal, octal and binary
#using built-in functions.
x = bin(36)
print(x)
y = hex(36)
print(y)
z = oct(36)
print(z)
output
====
0b100100
0x24
0o44
#Program 9(b) : Write a Python program to print
#factorial of a number using recursion.
def facto(n):
if n ==1:
return n
else:
return n * facto(n-1)
num=int(input("enter a number "))
if num<0:
print("invalid input! please enter a positive number:")
elif num == 0:
print ("factorial of a number 0 is 1")
else:
print("factorial of a number",num,"=",facto(num))
output
====
enter a number 5
factorial of a number 5 = 120
#Program 9(c) : Program to define
#cube function using anonymous function.
def cube(y):
return y*y*y
cbe=lambda y:y*y*y
print("using lambda function =",cbe(3))
output
====
using lambda function = 27
#Program 10(a) :Create modules and packages using Python.
=====
enter radius 5
the area of circle 78.53981633974483
the circumference of circle 31.41592653589793
#program 10 B
#Code, execute and debug programs using built-in modules.
import math
n=int(input("enter a number "))
print("square root of ",n, " number =",math.sqrt(n))
print("the factorial of ",n," number=",math.factorial(n))
print("the power of ",n,"to 3 number=",math.pow(n,3))
=====
enter a number 5
square root of 5 number = 2.23606797749979
the factorial of 5 number= 120
the power of 5 to 3 number= 125.0
Program 11(a) : Python program to demonstrate basic operations on single array using
NumPy module.
import numpy as np
a = np.array([[1,2],[3,4]])
b = np.array([[4,3],[2,1]])
print ("adding 1 to every element : \n",a+1)
print ("subtracting 2 from each element : \n",b-2)
print ("sum of all array elements :\n",a.sum())
print ("array sum:\n",a+b)
=====
adding 1 to every element :
[[2 3]
[4 5]]
subtracting 2 from each element :
[[ 2 1]
[ 0 -1]]
sum of all array elements :
10
array sum:
[[5 5]
[5 5]]
#Program 11(b) : Write a python program to assign your own index
#: to the data using series of Pandas
import pandas as pd
from pandas import Series
arr = Series([25, 50, 75, 100, 125],index=[2, 4, 6, 8, 10])
print(arr)
print('\nValues in this Array : ', arr.values)
print('Index Values of this Array : ', arr.index)
=====
2 25
Note :Procedure to install pandas if its
4 50
not installed
6 75
C:\Users\CS>py -m pip install pandas
8 100
10 125
dtype: int64
=====
Name Age Qualification Address
0 Jai 27 Msc Delhi
1 Princi 24 BE Kanpur
2 Rahim 22 MCA Allahabad
3 Anuj 32 Phd Kannauj
# program 12A) Program to perform read and write operations on files
file1 = open("myfile.txt", "w")
L = ["Anjuman E Islam Polytechnic \n", "Mallasamudra road \n", "Gadag\n"]
file1.write("Hello \n")
file1.writelines(L)
file1.close()
file1 = open("myfile.txt", "r")
print("Output of Read function is ")
print(file1.read())
print()
=====
Output of Read function is
Hello
Anjuman E Islam Polytechnic
Mallasamudra road
Gadag
#Program 13(a) : Write a Python program to handle
# : NameError and ZeroDivisionError.
try :
print("try and exception ")
print(x)
except NameError:
print("Variable x is not defined")
try:
a=6/0
print(a)
except ZeroDivisionError:
print("You can’t divide by zero!")
=====
try and exception
Variable x is not defined
You can’t divide by zero!