0% found this document useful (0 votes)
15 views21 pages

Computer Practical

This is some important practical of Python for class 11th

Uploaded by

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

Computer Practical

This is some important practical of Python for class 11th

Uploaded by

supersharma101
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

QUE: PROGRAM TO OBTAIN LENGTH AND BREADTH

OF A RECTANGLE AND CALCULATE ITS AREA.

# to input length and breadth of a rectangle and calculate


its area
length = float(input("Enter length of the rectangle:"))
breadth = float(input("Enter breadth of the rectangle:"))

area = length * breadth

print ("Rectangle specifications")


print ("Length = ", length, end = ' ')
print ("Breadth = ", breadth)
print ("Area = ", area)

RESULT:

Enter length of the rectangle: 8.75


Enter breadth of the rectangle: 35.0
Rectangle specifications
Length = 8.75 Breadth = 35.0
Area = 306.25
QUE: PROGRAM TO CALCULATE BMI (BODY MASS
INDEX) OF A PERSON:

# to calculate BMI = kg/m square


weight_in_kg = float(input("Enter weight in kg :"))
height_in_meter = float(input("Enter height in meters :"))

bmi = weight_in_kg / (height_in_meter * height_in_meter)

print(" BMI is :",bmi)

RESULT:

Enter weight in kg : 66
Enter height in meters : 1.6
BMI is : 25.781249999999996
QUE: WRITE A PROGRAM TO INPUT A VALUE IN
TONNES AND CONVERT IT INTO QUINTALS AND
KILOGRAMS
(1-tonne = 10 quintals , 1- tonne = 1000 kgs, 1 quintal = 100
kgs)

tonnes = float (input("Enter tonnes :"))


quintals = tonnes * 10
kgs = quintals * 100

print("Tonnes:",tonnes)
print("Quintals :", quintals)
print("Kilograms:",kgs)

RESULT:
Enter tonnes: 2.5
Tonnes: 2.5
Quintals: 25.0
Kilograms: 2500.0
QUE: WRITE A PROGRAM TO ENTER TWO INTEGERS
AND PERFORM ALL ARITHMETIC OPERATORS ON
THEM.
x = int(input("Enter value1:"))
y = int(input("Enter value2:"))
summ = x + y
diff = x - y
mult = x * y
div = x / y
mod = x % y
print("Numbers are :", x,y)
print("Sum = ", summ)
print("Difference = ", diff)
print("Product = ", mult)
print("Division = ", div)
print("Modulus = ", mod)

RESULT:
Enter value1: 7
Enter value2: 3
Numbers are: 7 3
Sum = 10
Difference = 4
Product = 21
Division = 2.3333333333333335
QUE: WRITE A PROGRAM TO SWAP TWO NUMBERS
USING A THIRD VARIABLE.

x = int(input("Enter value1 :"))


y = int(input("Enter value2 :"))
print("Original Numbers are :", x,y)

t = x
x = y
y = t
print("After Swapping, Numbers are :", x,y)

RESULT:
Enter value1 : 6
Enter value2 : 9
Original Numbers are : 6 9
After Swapping, Numbers are : 9 6
QUE: THE RADIUS OF A SPHERE IS 7.5 METERS.
WRITE PYTHON SCRIPT TO CALCULATE ITS AREA
AND VOLUME.
(Area of a sphere = πr2, Volume of a sphere = 4πr3)

import math
r = 7.5
area = math.pi * r * r
volume = 4 * math.pi * math.pow(r,3)

print("Radius of the sphere:",r, "meters")


print("Area of the square:", area," units square")
print("Volume of the square:", volume, "units cube")

RESULT:

Radius of the sphere : 7.5 meters


Area of the square: 176.71458676442586 units square
Volume of the square: 5301.437602932776 units cube
QUE: WRITE A PROGRAM TO READ BASE / LENGTH(l),
WIDTH (w) AND HEIGHT (h) OF A PARALLELOGRAM
AND CALCULATE ITS AREA AND PERIMETER.

l = float(input("Enter base/length of the parallelogram :


"))
w = float(input("Enter width of the parallelogram :"))
h = float(input("Enter height of the parallelogram :"))
area = l * h
perimeter = 2*l + 2*w

print("The area of given parallelogram is :", area)


print("The perimeter of given parallelogram is :",
perimeter)

RESULT:

Enter base/length of the parallelogram : 13.5


Enter width of the parallelogram : 7
Enter height of the parallelogram : 5
The area of given parallelogram is : 67.5
The perimeter of given parallelogram is : 41.0
QUE: A TRIANGLE HAS THREE SIDES a,b,c AS 17, 23,
30. CALCULATE AND DISPLAY ITS AREA USING
HERON’S FORMULA AS
s = (a + b + c) / 2 ; Area = √s(s-a)(s-b)(s-c)

import math
a, b, c = 17, 23, 30
s = (a + b + c)/2
area = math.sqrt(s * (s-a) * (s-b) * (s-c) )

print("Sides of triangle :", a,b,c)


print("Area:",area,"units square")

RESULT:

Sides of triangle : 17 23 30
Area: 194.42222095223582 units square
QUE: PROGRAM TO FIND THE MULTIPLES OF A
NUMBER
( the divisor) OUT OF THE GIVEN FIVE NUMBERS.

print("Enter five numbers below")


num1 = float(input("First number :"))
num2 = float(input("Second number :"))
num3 = float(input("Third number :"))
num4 = float(input("Fourth number :"))
num5 = float(input("Fifth number :"))
divisor = float(input("Enter divisor number:"))
count = 0
print("Multiples of",divisor,"are:")
reminder = num1 % divisor
if reminder == 0 :
print(num1,sep=" ")
count +=1
reminder = num2 % divisor
if reminder == 0 :
print(num2,sep=" ")
count +=1
reminder = num3 % divisor
if reminder == 0 :
print(num3,sep=" ")
count +=1
reminder = num4 % divisor
if reminder == 0 :
print(num4,sep=" ")
count +=1
reminder = num5 % divisor
if reminder == 0 :
print(num5,sep=" ")
count +=1
print()
print(count,"multiples of",divisor,"found")

RESULT:

Enter five numbers below


First number : 185
Second number : 3450
Third number : 1235
Fourth number : 1100
Fifth number :905
Enter divisor number: 15
Multiples of 15.0 are:
3450.0

1 multiples of 15.0 found


QUE: PROGRAM TO CALCULATE AND PRINT ROOTS
OF A QUADRATIC EQUATION: ax2 + bx + c = 0 (a≠0).

import math
print("For quadratic equation, ax**2 + bx + c = 0, enter
cofficients below")
a = int(input("Enter a :"))
b = int(input("Enter b :"))
c = int(input("Enter c :"))
if a==0 :
print("Value of",a,'should not be zero')
print("\n Aborting !!!!!!")
else :
delta = b * b-4 * a * c
if delta > 0 :
root1 = (-b + math.sqrt(delta)) / (2*a)
root2 = (-b - math.sqrt(delta)) / (2*a)
print ("Roots are REAL and UNEQUAL")
print ("Root1 =", root1,", Root2=", root2)
elif delta ==0:
root1 =-b / (2*a) ;
print("Roots are REAL and EQUAL")
print ("Root1 =", root1,", Root2=",root1)
else :
print ("Roots are COMPLEX and IMAGINARY”)
RESULT:

For quadratic equation, ax**2 + bx + c = 0, enter


cofficients below
Enter a : 2
Enter b : 4
Enter c : 2
Roots are REAL and EQUAL
Root1 = -1.0 , Root2= -1.0
QUE: WRITE A PROGRAM SCRIPT TO PRINT THE
FIBONACCI SERIES’ FIRST 20 ELEMENTS. SOME
INITIAL ELEMENTS OF A FIBONACCI SERIES ARE:
0112358…

first = 0
second = 1
print (first)
print (second)
for a in range (1, 19) :
third = first + second
print (third)
first, second = second, third
RESULT:
0
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
QUE: NUMBERS IN THE FORM 2n – 1 ARE CALLED
MERSENNE NUMBERS, e.g.,
21 – 1 = 1, 22 – 1 = 3, 23 – 1 = 7.
WRITE A PYTHON SCRIPT THAT DISPLAYS FIRST TEN
MERSENNE NUMBERS.

# program to print mersenne numbers


print("First 10 mersenne numbers are:")
for a in range(1,11) :
mersnum = 2 ** a-1
print(mersnum, end = " ")
print()

RESULT:

First 10 mersenne numbers are:


1 3 7 15 31 63 127 255 511 1023
QUE: WRITE PYTHON SCRIPT TO PRINT THE
FOLLOWING PATTERN :
1
13
135
1357

for a in range(3, 10, 2) :


for b in range(1, a, 2) :
print (b, end = ' ')
print ( )

RESULT:

1
1 3
1 3 5
1 3 5 7
QUE: WRITE A PROGRAM TO PRINT A UNIQUE
PATTERN:

n = 5 # number of lines
s = n * 2 - 1 # for initial spaces
for i in range(1, n + 1) :
for j in range(0, s) :
print(end = " ")
for j in range(i, 0, -1):
print(j,end = " ")
for j in range(2, i+1) :
print(j, end =" ")
s = s - 2
print()

RESULT:

1
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
5 4 3 2 1 2 3 4 5
QUE: WRITE A PROGRAM THAT READS A STRING AND
CHECKS WHETHER IT IS A PALINDROME STRING OR
NOT WITHOUT USING STRING SLICE.

string = input("Enter a string :")


length = len(string)
mid = int(length/2)
rev = -1
for a in range(mid) :
if string[a] == string[rev] :
a +=1
rev -=1
else :
print (string,"is not a palindrome")
break
else :
print (string,"is a palindrome")

RESULT:

Enter a string : mum


mum is a palindrome
QUE: WRITE A PROGRAM THAT DISPLAYS OPTIONS
FOR INSERTING OR DELETING ELEMENTS IN A LIST.
IF THE USER CHOOSES A DELETION OPTION,
DISPLAY A SUBMENU AND ASK IF ELEMENT IS TO BE
DELETED WITH VALUE OR BY USING ITS POSITION
OR A LIST SLICE IS TO BE DELETED.

val = [17, 23, 24, 18]


print("The list is :", val)
while True :
print("Main Menu")
print("1.Insert")
print("2.Delete")
print("3.Exit")
ch = int(input("Enter your choice 1/2/3:"))
if ch == 1 :
item = int(input("Enter item:"))
pos = int(input("Insert at which position?"))
index = pos - 1
val.insert(index,item)
print("Success! List now is :",val)
elif ch == 2 :
print("Deletion Menu")
print("1.Delete using Value")
print("2.Delete using Index")
print("3.Delete a sublist")
dch = int(input("Enter choice (1 or 2 or 3) : "))
if dch == 1 :
item = int(input("Enter item to be deleted:"))
val.remove(index)
print ("List now is :",val)
if dch == 2 :
item = int(input("Enter index of item to be
deleted:"))
val.pop(index)
print ("List now is :",val)
elif dch== 3 :
l = int(input("Enter lower limit of list slice
to be deleted:"))
h = int(input("Enter upper limit of list slice
to be deleted:"))
del val[l:h]
print ("List now is :",val)
else :
print("valid choics are 1/23 only.")
elif ch == 3 :
break;
else :
print("valid choices are 1/2/3 only)
RESULT:

The list is : [17, 23, 24, 18]


Main Menu
1.Insert
2.Delete
3.Exit
Enter your choice 1/2/3: 1
Enter item: 55
Insert at which position? 4
Success! List now is : [17, 23, 24, 55, 18]

You might also like