0% found this document useful (0 votes)
4 views14 pages

Python Programs For Practical File

The document contains a series of programming tasks and their corresponding solutions in Python. The tasks include calculating areas and perimeters, finding substrings, checking for prime numbers, generating patterns, and working with data structures like lists and data frames. Additionally, it covers graphical representation using matplotlib and various string manipulations.

Uploaded by

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

Python Programs For Practical File

The document contains a series of programming tasks and their corresponding solutions in Python. The tasks include calculating areas and perimeters, finding substrings, checking for prime numbers, generating patterns, and working with data structures like lists and data frames. Additionally, it covers graphical representation using matplotlib and various string manipulations.

Uploaded by

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

Q.

1 WRITE A PROGRAM THAT CALCULATES THE FOLLOWING:


. AREA OF A CIRCLE
.CIRCUMFERENCE OF A CIRCLE
.AREA OF RECTANGLE
.PERIMETER OF A RECTANGLE

PROGRAM:
while True:
print('1. for area of circle')
print('2. for area of
rectangle')
print('3. for circumference of
circle') print('4. for area of
square')
ch=int(input('enter your choice:enter 0
to exit')) if ch==1:
r=int(input('enter the radius of the
circle:')) a=3.14*r*r
print('area of
circle:',a) elif ch==2:
l=int(input('enter the
length:')) b=int(input('enter
the breath:')) a=l*b
print('area of
rectangle:',a) elif ch==3:
r=int(input('enter the radius of the
circle:')) c=2*(int(3.14*r))
print('circumference of
circle:',c) elif ch==4:
l=int(input('enter the side of
square:')) a=l*l
print('area of
square:',a) elif ch==0:
brea
k else:
print('invalid option')

OUTPUT:
Q.2 WRITE A PROGRAM THAT READS A STRING AND DISPLAYS
THE LONGEST SUBSTRING OF THE GIVEN STRING.

PROGRAM:
str1=input('enter the
string:')
word=[Link]()
maxlength=0
maxword='
' for i in
word:
x=len(i)
if x>maxlength and
[Link]()==True: print(i)
maxlength=x
maxword=i
print('substring with maximum length is:',maxword)

OUTPUT:

Q.3 WRITE A PROGRAM TO FIND THE OCCURENCES OF EACH


LETTER PRESENT IN THE GIVEN STRING.

PROGRAM:
st1=input('please enter your own
string:') char=input('please enter your
own chracter') c=0
for i in range(len(st1)):
if(st1[i]==char):
c=c+1
print('total occurence of ',char,'is',c)
OUTPUT:

Q4 Print this pattern using loop


*
**
***
** **

Q5 Check whether a number is prime or not

Q6 Check whether the sum of cubes of the digits of number is equal to the
number (ARMSTRONG NO.)
Q7 Input three no and print the greatest of them

Q8 Take input 10 times and print its sum

Q9 Input a number and print it’s factorial


Q10 Write a program to find out the sum of digits of any entered number.

Q11. Write a program to check whether the given character is an uppercase


letter or lowercase letter or a digit or a special character.

#Input the character to check

ch=input("Enter Any Character:")

'''Checking whether it is upperletter or lowerletter or digit or a special


character'''

if [Link]():
print(ch, " is an upper case
letter") elif [Link]():
print(ch, " is a lower case
letter") elif [Link]():
print(ch, " is a
digit") elif
[Link]():
print(ch, " is a space")
else:
print(ch," is a special character")

Q12. Write a program to find the maximum number out of the given three
numbers.

#Take input or three number to compare

n1=int(input("Enter the
Number1:"))
n2=int(input("Enter the
Number2:"))
n3=int(input("Enter the
Number3:")) if n1>n2 and
n1>n3:
print(n1, " - Number 1 is
greater") elif n1>n2 and
n1>n3:
print(n2, " - Number 2 is
greater") elif n3>n1 and
n3>n2:
print(n3, " - Number 3 is
greater") else:
print("All are same")
Q13. Write a program to print a multiplication table of the entered number.

#Take input to accept a nmuber for printing Multiplication table

n=int(input("Enter number to print multiplication table:"))

#Take for loop for multiple

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

Q14. Write a program to generate the following pattern:

2 3

4 5 6

7 8 9 10

11 12 13 14 15

#Take input for n lines

n=int(input("Enter n:")) #Generating


Pattern k=1
for i in range(1,n+1):

for j in range(1,i+1):
print(k,end=" ")
k=k+1
print()
Q15. Write a program to create a list of students' marks with user-defined
values and find the maximum.

#Take input for n lines

n=int(input("Enter no. of subjects:"))

#Creating empty list

l=[]

#Accepting marks and appending marks into the list

for i in range(n):
m=int(input("Enter marks:"))

[Link](m)

print("Maximum marks scored:",max(l)

Q16. Write a program to create a 2D array using NumPy.

#import numpy package

import numpy as np

#Creating array using arange() function

arr=[Link](5,45,5)

#reshaping array for 2D

arr=[Link](2,4)

#printing array

print(arr)

Q17. Write a program to develop a matrix of 3x3 with values from 11 to 28.

#import numpy

package import
numpy as np
#Creating array using arrange()

function arr=[Link](11,28,2)

#reshaping array for

2D

arr=[Link](3,3)

#printing array

print(arr)

Q18. Write a program to create a data frame to store data of candidates who
appeared in interviews. The dataframe columns are name, score, attempts,
and qualify (Yes/No). Assign rowindex as C001,C002, and so on.

#import pandas package

import pandas as pd

#Creating Dictionary to store data

d={'Name':['Anil','Bhavna','Chirag','Dhara','Giri'], 'Score':
[25,20,22,23,21],
'Attempts':[1,2,2,1,1], 'Qualified':

['Yes','No','Yes','Yes','No']} #Creating a dataframe

df=[Link](d,index=['C001','C002','C003','C004','C00

5'])

#Printing a dataframe

print(df)
Q19. Write a program to represent the data on the ratings of mobile
games on bar chart. The sample data is given as: Pubg, FreeFire,
MineCraft, GTA-V, Call of duty, FIFA 22. The rating for each game is as:
4.5,4.8,4.7,4.6,4.1,4.3.

#Import package for Matplot

Library import [Link]

as plt #Creating lists for data

games=['Pubg', 'FreeFire', 'MineCraft', 'GTA-V','Call of duty', 'FIFA


22'] rating=[4.5,4.8,4.7,4.6,4.1,4.3]

#Creating bar graph with different bar colours

[Link](games,rating,color=['black', 'red', 'green', 'blue', 'yellow'])

#Customizing the bar graph [Link]("Games Rating 2022")

[Link]('Games')
[Link]('Rating')
[Link]();
[Link]()
Q20. Write a program to find whether the input no. is Palindrome or not.

n=int(input("Enter
number:")) temp=n
rev=0
while(n>0):
dig=n%10
rev=rev*10+dig
n=n//10
if(temp==rev):
print("The number is a
palindrome!") else:
print("The number isn't a palindrome!")

Q21. Write a program to Count the no. of vowels in a string

Q22. Write a program to Swap the numbers without any new variable.

You might also like