0% found this document useful (0 votes)
81 views

Python Practical

The document contains several Python programs that demonstrate basic concepts like input/output, conditional statements, loops, functions, and arithmetic operations. The programs cover topics such as student data entry, calculators, relational operators, triangle properties, and more.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
81 views

Python Practical

The document contains several Python programs that demonstrate basic concepts like input/output, conditional statements, loops, functions, and arithmetic operations. The programs cover topics such as student data entry, calculators, relational operators, triangle properties, and more.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

➢ # program to read and print details of a student

rollno=input("enter your rollno")


name=input("Enter your Name")
course=input("Enter Your Course")
section=input("Enter your Section")
print("Student Details*")
print("Student Roll No=",rollno,end='')
print("Student Name=",name,end='')
print("Course Name=",course,end='')
print("Section=",section)
print("")

Input/Output
enter your rollno1001
Enter your NameAs
Enter Your CourseBCA
Enter your SectionBCA21&22
********************Student Details***********************
Student Roll No= 1001Student Name= AsCourse Name= BCASection= BCA21&22
************************************************************

➢ # Program to Design a simple calculator


print("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@")
a=int(input("Enter first number"))
b=int(input("Enter second number"))
print("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@")
print("$$$$$$$$$$$ Basic Calculator$$$$$$$$$$$$$$")
print("Sum of two no.=",(a+b))
print("Difference of two no.=",(a-b))
print("Product of two no.=",(a*b))
print("Floating Quotient of two no.=",(a/b))
print("Integer Quotient of two no.=",(a//b))
print("Modulus of two no.=",(a%b))
print("Exponent of a^b=",(a**b))
print("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$")

Input/Output
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Enter first number13
Enter second number7
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
$$$$$$$$$$$ Basic Calculator$$$$$$$$$$$$$$
Sum of two no.= 20
Difference of two no.= 6
Product of two no.= 91
Floating Quotient of two no.= 1.8571428571428572
Integer Quotient of two no.= 1
Modulus of two no.= 6
Exponent of a^b= 62748517
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

➢ # Program to works on Relational and Logical Operators


a=int(input("Enter First No."))
b=int(input("Enter Second No."))
c=int(input("Enter Third No."))
print(a>b)
print(a<b)
print(a>=b)
print(a<=b)
print(a==b)
print(a!=b)
print(a>b and b>c)
print(a==b or b!=c)
print(not a==b)

Input/Output
Enter First No.5
Enter Second No.7
Enter Third No.4
False
True
False
True
False
True
False
True
True

➢ # program to calcualte and print area of scalene trinage


import math
a=int(input("Enter first side of scalene triangle"))
b=int(input("Enter second side of scalene triangle"))
c=int(input("Enter third side of scalene triangle"))
s=(a+b+c)/2
area=math.sqrt(s*(s-a)(s-b)(s-c))
print("Area of scalene triangle=",area)

input/output
Enter first side of scalene triangle10
Enter second side of scalene triangle12
Enter third side of scalene triangle15
Area of scalene triangle= 59.81168364124187

➢ # program to find Hypoteneous of right angle triangle


import math
b=int(input("Enter base of right angle triangle")) # b=base
a=int(input("Enter height of right angle tringle")) # a=height or altitude
h=math.sqrt(math.pow(b,2)+math.pow(a,2)) # h=hypoteneous
print("Hypoteneous=",h)

input/output
Enter base of right angle triangle15
Enter height of right angle tringle20
Hypoteneous= 25.0

➢ """ program to calculate surface area and volume of sphere"""


import math
r=float(input("Enter radius of sphere"))
sa=4*math.pi*math.pow(r,2)
volume=(4/3)*math.pi*math.pow(r,3)
print("Surface Area=",sa,"Volume=",volume)

input/output
Enter radius of sphere15
Surface Area= 2827.4333882308138 Volume= 14137.166941154068

➢ # program of conditional statement


a=5
b=3
c=1
a>b and a>c

output: true

➢ #program to print largest and smallest number among three


inputted numbers using nested if statement.
# Taking input from the user
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
num3 = float(input("Enter the third number: "))

# Finding the largest number


if num1 >= num2:
if num1 >= num3:
largest = num1
else:
largest = num3
else:
if num2 >= num3:
largest = num2
else:
largest = num3

# Finding the smallest number


if num1 <= num2:
if num1 <= num3:
smallest = num1
else:
smallest = num3
else:
if num2 <= num3:
smallest = num2
else:
smallest = num3

# Printing the largest and smallest numbers


print("The largest number is:", largest)
print("The smallest number is:", smallest)

input/output
Enter the first number: 15
Enter the second number: 25
Enter the third number: 20
The largest number is: 25.0
The smallest number is: 15.0
➢ # program to read five subject marks (Physics, Chemistry,
Math, Computer and English) obtained by a class 12th student
and print report card of student
name=input("Enter Your Name")
rollno=int(input("Enter your Roll No."))
m1=int(input("Enter Marks obtained in Physics"))
m2=int(input("Enter Marks obtained in Chemistry"))
m3=int(input("Enter Marks obtained in Math"))
m4=int(input("Enter Marks obtained in Computer"))
m5=int(input("Enter Marks obtained in English"))
sum=m1+m2+m3+m4+m5
per=sum/5
print("ISC Result-2024*")
print("Roll No=",rollno, "Student Name=",name)
print("Statements of Marks*")
print("Physics=",m1,"Chemistry=",m2,"Mathematics=",m3)
print("Computer=",m4,"English=",m5)
print("")
print("Total Marks Obtained=",sum,"/500")
print("Percentage Marks Obtained=",per,"%")
print("")
print("Result is ")
if(per>=90 and per<=100):
print("Division=Distinction")
elif(per>=60 and per<90):
print("Division=First")
elif(per>=45 and per<60):
print("Division=Second")
elif(per>=33 and per<45):
print("Division=Third")
else:
print("Sorry, You are not pass")
print("")

input/output
Enter Your NameAs
Enter your Roll No.10101
Enter Marks obtained in Physics88
Enter Marks obtained in Chemistry80
Enter Marks obtained in Math93
Enter Marks obtained in Computer96
Enter Marks obtained in English79
****************ISC Result-2024*******************
Roll No= 10101 Student Name= As
**Statements of Marks*****************************
Physics= 88 Chemistry= 80 Mathematics= 93
Computer= 96 English= 79
****************************************************
Total Marks Obtained= 436 /500
Percentage Marks Obtained= 87.2 %
****************************************************
Result is
Division=First
****************************************************
➢ Write a program to read monthly income of an employee and
calculate annual income. also calculate income tax based on
following conditions:
for income up to 500000 tax=0
for next 5 lakh tax=10%
for next 10 lakh tax=20%
for rest tax=30%
name=input("enter your name")
mi=float(input("enter monthly Salary"))
ai=mi*12
if(ai<=500000):
tax=0
elif(ai>500000 and ai<=1000000):
tax=(ai-500000)*0.10
elif(ai>1000000 and ai<=2000000):
tax=(500000*0.10)+(ai-1000000)*0.20
else:
tax=(500000*0.10)+(1000000*0.20)+(ai-2000000)*.30
print("Employee Name=",name)
print("Monthly Income=",mi)
print("Annual Income=",ai)
print("Total Income Tax=",tax)

input/output
enter your nameAs
enter monthly Salary54000
Employee Name= As
Monthly Income= 54000.0
Annual Income= 648000.0
Total Income Tax= 14800.0

➢ #programs on quadratic equation roots:


import math
#program to calculate and print roots of quadratic equation ax2+bx+c=0
a=int(input("enter value of a"))
b=int(input("enter value of b"))
c=int(input("enter value of c"))
d=math.pow(b,2)-(4*a*c)
if(d<0):
print("Roots are imaginary")
elif(d==0):
r1=r2=(-b/(2*a))
print("Roots are real and equal")
print("Postive Root=",r1,"and Negative Root=",r2)
else:
r1=(-b+math.sqrt(d))/(2*a)
r2=(-b-math.sqrt(d))/(2*a)
print("Roots are real and unequal")
print("Postive Root=",r1,"and Negative Root=",r2)

input/output
enter value of a50
enter value of b25
enter value of c30
Roots are imaginary
➢ #Program on match and case statement (alternative of switch
statement)
# match statement alterntive of switch statement
n=int(input("Enter a day number"))
match n:
case 1:
print("Monday")
case 2:
print("Tuesday")
case 3:
print("Wednesday")
case 4:
print("Thursday")
case 5:
print('Friday')
case 6:
print('Saturday')
case 7:
print('Sunday')
case _:
print("Invalid Day No.")

input/output
>>>Enter a day number5
Friday
>>>Enter a day number7
Sunday
>>>Enter a day number9
Invalid Day No.

➢ Program on match and case statement (alternative of switch


statement)
name=input("enter your name")
match name:
case "raj":
print("Name=raj")
case "akash":
print("Name=Akash")
case "vinod":
print("Name=Vinod")
case default:
print("Name is not availabe")

input/output
>>>enter your nameraj
Name=raj
>>>enter your nameShivam
Name is not available
>>>enter your nameakash
Name=Akash

➢ #program to print table of an inputted number


n=int(input("Enter a no"))
i=1
while(i<=10):
print(n,"*",i,"=",(n*i))
i=i+1
print("End of program")

input/output
Enter a no5
5*1=5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
End of program

➢ #program to check whether an inputted number is armstrong


number or not.
import math
n=int(input("Enter a number"))
m=n
sum=0
while(n>0):
p=n%10
sum=sum+math.pow(p,3)
n=n//10
if(m==sum):
print("Armstrong number")
else:
print("Not armstrong number")

input/output
>>>Enter a number123
Not armstrong number
>>>Enter a number153
Armstrong number

#program to check whether an inputted number is palindrome or


not.
n=int(input("Enter a number"))
m=n
rev=0
while(n>0):
p=n%10
rev=rev*10+p
n=n//10
if(rev==m):
print("Number is palindrome")
else:
print("Number is not palindrome")

input/output
Enter a number14641
Number is palindrome
Enter a number151
Number is palindrome
Enter a number152
Number is not palindrome

➢ #for loop in python


Example 1:
for x in range(1,10):
print("value of x=",x)

output:
value of x= 1
value of x= 2
value of x= 3
value of x= 4
value of x= 5
value of x= 6
value of x= 7
value of x= 8
value of x= 9

Example 2:
for x in range(1,11,2):
print("value of x=",x)

output: -

value of x= 1
value of x= 3
value of x= 5
value of x= 7
value of x= 9
Example 3:
x={3.5,1.09,4.59,3.68}
for p in x:
print("element-",p)

output: -
element- 3.68
element- 1.09
element- 3.5
element- 4.59

Example 4:-
str="BBDU"
for i in str:
print(i)

output: -

B
B
D
U
n=int(input("enter a number"))
enter a number8

➢ #program to print table of an inputted number using for loop


for i in range(1,11):
print(n,"*",i,"=",(n*i))

output: -
8*1=8
8 * 2 = 16
8 * 3 = 24
8 * 4 = 32
8 * 5 = 40
8 * 6 = 48
8 * 7 = 56
8 * 8 = 64
8 * 9 = 72
8 * 10 = 80

➢ for else loop statement

for i in range(1,11):
print(n,"*",i,"=",(n*i))
else:
print("end of for loop:")

output: -
8*1=8
8 * 2 = 16
8 * 3 = 24
8 * 4 = 32
8 * 5 = 40
8 * 6 = 48
8 * 7 = 56
8 * 8 = 64
8 * 9 = 72
8 * 10 = 80
end of for loop:

➢ nested for loop: for loop within a for loop

example 1: -
for i in range(1,6):
for j in range(1,i+1):
print(j,end=' ')
print('')

output: -
1
12
123
1234
12345

example 2:-
for i in range(1,6):
for j in range(1,i+1):
print(j,end=' ')
print("*",end=' ')
print('')

output: -
1*
1*2*
1*2*3*
1*2*3*4*
1*2*3*4*5*

example 3: -
for i in range(1,6):
print(i,end=' ')
for j in range(1,i+1):
print(j,end=' ')
print("*",end=' ')
print('')

output: -
11*
21*2*
31*2*3*
41*2*3*4*
51*2*3*4*5*

➢ Programs on user defined function:


import math
def sarea(a, b, c):
s=(a+b+c)/2
area=math.sqrt(s*(s-a)(s-b)(s-c))
return area

x=int(input("enter x"))
y=int(input("enter y"))
z=int(input("enter z"))
r=sarea(x, y, z)
print("Area of Scalene Triangle=",r)

Input/output
enter x5
enter y6
enter z9
Area of Scalene Triangle= 14.142135623730951

➢ program on default argument and keyword argument


import math
def sarea(a, b=7, c=6):
s=(a+b+c)/2
area=math.sqrt(s*(s-a)(s-b)(s-c))
return area

x=int(input("enter x"))
y=int(input("enter y"))
z=int(input("enter z"))
r=sarea(x, y, z)
print("Area of Scalene Triangle=",r)
print("")
print("Area of scalene triangle having default=",sarea(8,11)) # default argument
print("Area of scalene triangle having default=",sarea(8,c=12)) # keyword
argument

Input/output
enter x8
enter y9
enter z11
Area of Scalene Triangle= 35.4964786985977

Area of scalene triangle having default= 23.418742493993992


Area of scalene triangle having default= 26.906086671978144

➢ #program to check whether an inputted number is spy no. or


not
n=int(input("Enter a number"))
m=n
sum=0
product=1
while(n>0):
p=n%10
sum=sum+p
product=product*p
n=n//10
if(sum==product):
print("Spy No.")
else:
print("Not Spy No.")

input/output
>>>Enter a number555
Not Spy No.
>>>Enter a number123
Spy No.
➢ #program for fibonacci series
n=int(input("Enter no of terms"))
a=0
b=1
print("Fibonacci Series is:")
print(a, b, end=' ')
i=2
while(i<n):
c=a+b
print(c,end=" ")
a=b
b=c
i=i+1

input/output
Enter no of terms9
Fibonacci Series is:
0 1 1 2 3 5 8 13 21

➢ #program on datetime module


import datetime

datetime.datetime.now()
Out[65]: datetime.datetime(2024, 3, 1, 11, 33, 39, 467093)

x=datetime.datetime.now()
x.strftime("%x")
Out[67]: '03/01/24'

x.strftime("%X")
Out[68]: '11:34:20'

datetime.datetime.now().strftime("%X")
Out[69]: '11:36:57'

datetime.datetime.now().strftime("%x")
Out[70]: '03/01/24'

datetime.datetime.now().strftime("%a")
Out[71]: 'Fri'

x.strftime("%H")
Out[72]: '11'

x.strftime("%M")
Out[73]: '34'

x.strtime("%S")
Traceback (most recent call last):

Cell In[74], line 1


x.strtime("%S")
AttributeError: 'datetime.datetime' object has no attribute 'strtime'

x.strftime("%S")
Out[75]: '20'

➢ #program on string concepts:


'{0} is a {1} in the university {2}'.format('Dr. C. K. Pandey', 'faculty','BBDU')
'Dr. C. K. Pandey is a faculty in the university BBDU'
st="Ram Ramesh Dinesh Mahesh Suresh"
st.split()
['Ram', 'Ramesh', 'Dinesh', 'Mahesh', 'Suresh']
"bbdu".upper()
'BBDU'
name="ravi kumar saxena"
name.upper()
'RAVI KUMAR SAXENA'
nn=name.upper()
print(nn)
RAVI KUMAR SAXENA
name
'ravi kumar saxena'
nn
'RAVI KUMAR SAXENA'
nn.upper()
'RAVI KUMAR SAXENA'
nn
'RAVI KUMAR SAXENA'
nn.lower()
'ravi kumar saxena'
nn
'RAVI KUMAR SAXENA'
nn=nn.lower()
nn
'ravi kumar saxena'
nn
'ravi kumar saxena'
nn.islower()
True
name
'ravi kumar saxena'
name=name.upper()
name
'RAVI KUMAR SAXENA'
name.islower()
False
name.isupper()
True
t="456"
t.isalpha()
False
t.isdigit()
True
t.isalnum()
True
u=' '
u.isspace()
True
nst="Honesty is the best policy"
nst.split()
['Honesty', 'is', 'the', 'best', 'policy']
l=nst.split()
print(l)
['Honesty', 'is', 'the', 'best', 'policy']
print(l[0])
Honesty
len(l)
5
for i in range(0, len(l)):
print(l[i])

Honesty
is
the
best
policy
= RESTART:
C:/Users/rimds/AppData/Local/Programs/Python/Python310/ckp/stringlinears
earch.py
Enter a sentence from usertoday is tuesday
Enter a search keyis
Element found at position= 2

= RESTART:
C:/Users/rimds/AppData/Local/Programs/Python/Python310/ckp/stringlinears
earch.py
Enter a sentence from userrahul is good boy
Enter a search keysumit
Element not found
mystr="BBDU"
print(mystr.center(20,' '))
BBDU
mystr.center(20,'*')
'*BBDU*'
mystr.center(40,'@')
'@@@@@@@@@@@@@@@@@@BBDU@@@@@@@@@@@@@@@
@@@'
mystr.center(39,'$')
'$$$$$$$$$$$$$$$$$$BBDU$$$$$$$$$$$$$$$$$'
mystr.center(9,'*')
'BBDU*'
n='smartphone were distributed yestereday'
n.capitalize()
'Smartphone were distributed yestereday'

➢ #programs on linear search over string


#program to search a key in a string (Linear search)
str=input("Enter a sentence from user")
nst=str.split()
key=input("Enter a search key")
for i in range(0, len(nst)):
if(key==nst[i]):
print("Element found at position=",i+1)
break
else:
print("Element not found")

input/output
>>>Enter a sentence from userAnimesh
Enter a search keyk
Element not found
>>>Enter a sentence from userShivam
Enter a search keyv
Element not found

➢ #program on string2
print("\\Hello ji")
\Hello ji
print("\'Prime Minister of India is\' \"Narandra Modi\"")
'Prime Minister of India is' "Narandra Modi"
print("Hello \a")
Hello
print("Hello \a")
Hello
print("Hello BBD\f")
Hello BBD
print("Hello BBDU\r")
Hello BBDU
print("Hello BBDU \r We are waiting for you")
Hello BBDU
We are waiting for you
print("Hello BBDU \n We are waiting for you")
Hello BBDU
We are waiting for you
print("Hello BBDU \t We are waiting for you")
Hello BBDU We are waiting for you
print("Hello BBDU \v We are waiting for you")
Hello BBDU
We are waiting for you
print("Hello BBDU \ooo We are waiting for you")
Hello BBDU \ooo We are waiting for you
print("Hello BBDU \0oo We are waiting for you")
Hello BBDU
str="Babu Banarasi Das University"
t=str.find("Das")
print(t)
14
print(str.find("Bhaiya"))
-1
print(str.find("Babu"))
0
print(str.find("babu"))
-1
s=" Mera Bharat Mahan "
s.lstrip()
'Mera Bharat Mahan '
s
' Mera Bharat Mahan '
s.rstrip()
' Mera Bharat Mahan'
s
' Mera Bharat Mahan '
s.istitle()
True
s1="sab padhen sab badhen"
s1.istitle()
False
1.capitalize().istitle()
False
s1
'sab padhen sab badhen'
s2=s1.capitalize()
s2
'Sab padhen sab badhen'
s2.istitle()
False
s2
'Sab padhen sab badhen'
s3="sab"
s3
'sab'
s4=s3.capitalize()
s3
'sab'
s4
'Sab'
s4.istitle()
True
s5="Hello BBDU, we are waiting for you"
s6=s5.partition("we")
s6
('Hello BBDU, ', 'we', ' are waiting for you')
s6=s5.partition(",")
s6
('Hello BBDU', ',', ' we are waiting for you')
s7="I like banana and mango very much."
s7
'I like banana and mango very much.'
s8=s7.replace("banana","papaya")
s8
'I like papaya and mango very much.'
s9="kamal and father of kamal do'nt like friends of kamal"
s9
"kamal and father of kamal do'nt like friends of kamal"
s9.replace("kamal","suhas",2)
"suhas and father of suhas do'nt like friends of kamal"
s10="chandra"
'-'.join(s10)
'c-h-a-n-d-r-a'
srr=" BBDU"
srr
' BBDU'
srr.lstrip()
'BBDU'
srr="BBDU "
srr
'BBDU '
srr.rstrip()
'BBDU'
srr
'BBDU '
srr=srr.rstrip()
srr
'BBDU'
srr
'BBDU'
srr.lower()
'bbdu'
srr
'BBDU'
srr=srr.lower()
srr
'bbdu'
srr=srr.upper()
srr
'BBDU'
srr.islower()
False
srr.isupper()
True
mystr="lucknow"
mystr
'lucknow'
mystr.isalpha()
True
mystr=mystr+"123"
mystr
'lucknow123'
mystr.isalpha()
False
mystr="lucknow 123"
mystr.isalpha()
False
mystr.isdigit()
False
str21="2345"
str21.isdigit()
True
mystr
'lucknow 123'
mystr.isalnum()
False
mystr="lucknow123"
mystr.isalnum()
True
mystr
'lucknow123'
mystr1=' '
mystr1
''
mystr1.isspace()
True
mystr.isspace()
False
s12="we are indians"
s12
'we are indians'
s12.capitalize()
'We are indians'
s12
'we are indians'
s12.istitle()
False
s12=s12.capitalize()
s12
'We are indians'
s12.istitle()
False
s12="Bbdu"
s12.istitle()
True
sent="mother of ankit and father of ankit don't like friends of ankit."
sent
"mother of ankit and father of ankit don't like friends of ankit."
sent.replace("ankit","deepak")
"mother of deepak and father of deepak don't like friends of deepak."
sent.replace("deepak","shivam",2)
"mother of ankit and father of ankit don't like friends of ankit."
sent=sent.replace("ankit","deepak")
sent
"mother of deepak and father of deepak don't like friends of deepak."
sent.replace("deepak","shivam",2)
"mother of shivam and father of shivam don't like friends of deepak."
nst=" All are Indians "
nst
' All are Indians '
nst.lstrip()
'All are Indians '
nst
' All are Indians '
nst=nst.lstrip()
nst
'All are Indians '
nst=nst.rstrip()
nst
'All are Indians'
nst="bbdu"
nst
'bbdu'
nst.istitle()
False
nst=nst.upper()
nst
'BBDU'
nst=nst.lower()
nst=nst.capitalize()
nst
'Bbdu'
nst.istitle()
True
nst="Pure hote wade"
nst
'Pure hote wade'
nst.istitle()
False
st="teachers of bbdu and heads of bbdu are best in all bbdu colleges."
st=st.replace("bbdu","Lucknow University")
st
'teachers of Lucknow University and heads of Lucknow University are best in all
Lucknow University colleges.'
st=st.replace("Lucknow University","LCC",2)
st
'teachers of LCC and heads of LCC are best in all Lucknow University colleges.'
st1=st.partition("in")
st1
('teachers of LCC and heads of LCC are best ', 'in', ' all Lucknow University
colleges.')
st1[0]
'teachers of LCC and heads of LCC are best '
st1[1]
'in'
st1[2]
' all Lucknow University colleges.'
st
'teachers of LCC and heads of LCC are best in all Lucknow University colleges.'
st3="teachers of LCC and heads of LCC are best in \n all Lucknow University
colleges."
st3
'teachers of LCC and heads of LCC are best in \n all Lucknow University
colleges.'
print("teachers of LCC and heads of LCC are best in \n all Lucknow University
colleges.")
teachers of LCC and heads of LCC are best in
all Lucknow University colleges.

You might also like