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

Python Program

The document contains a series of Python programs that demonstrate various functionalities including temperature conversion, student grade calculation, area calculations for different shapes, prime number identification, factorial calculation, Fibonacci series generation, and more. Each program includes user input, calculations, and outputs results accordingly. The document serves as a collection of practical coding examples for educational purposes.

Uploaded by

s7167304
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Python Program

The document contains a series of Python programs that demonstrate various functionalities including temperature conversion, student grade calculation, area calculations for different shapes, prime number identification, factorial calculation, Fibonacci series generation, and more. Each program includes user input, calculations, and outputs results accordingly. The document serves as a collection of practical coding examples for educational purposes.

Uploaded by

s7167304
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 32

1.

CONVERSION OF TEMPERATURE

PROGRAM

celsius = float(input("Enter temperature in celsius: "))

fahrenheit = (celsius * 9/5) + 32

print('%.2f Celsius is: %0.2f Fahrenheit' %(celsius, fahrenheit))

fahrenheit = float(input("Enter temperature in fahrenheit: "))

celsius = (fahrenheit - 32) * 5/9

print('%.2f Fahrenheit is: %0.2f Celsius' %(fahrenheit, celsius))


OUTPUT

Enter temperature in celsius: 37

37.00 Celsius is: 98.60 Fahrenheit

Enter temperature in fahrenheit: 99

99.00 Fahrenheit is: 37.22 Celsius


2. CALCULATE TOTAL MARKS PERCENTAGE AND GRADE OF A STUDENT

PROGRAM

n=input("Enter the Name:")

no=int(input("Enter the RegNo:"))

print("Enter the Marks of five subjects:")

subject_1 = float (input ())

subject_2 = float (input ())

subject_3 = float (input ())

subject_4 = float (input ())

subject_5 = float (input ())

total, average, percentage, grade = None, None, None, None

total = subject_1 + subject_2 + subject_3 + subject_4 + subject_5

average = total / 5.0

percentage = (total / 500.0) * 100

if percentage >= 80:

grade = 'A'

elif percentage >= 70 and percentage < 80:

grade = 'B'

elif percentage >= 60 and percentage < 70:

grade = 'C'

elif percentage >= 40 and percentage < 60:


grade = 'D'

else:

grade = 'E'

print("\nName: \t", n)

print("\nRegNo: \t", no)

print ("\nTotal marks: \t", total, "/ 500.00")

print ("\nAverage: \t", average)

print ("\nPercentage: \t", percentage, "%")

print ("\nGrade: \t", grade)


OUTPUT

Enter the Name: Arun

Enter the RegNo: 12345

Enter the Marks of five subjects:

98

89

78

88

80

Name: Arun

RegNo: 12345

Total marks: 433.0 / 500.00

Average: 86.6

Percentage: 86.6 %

Grade: A
3. AREA OF RECTANGLE, SQUARE, CIRCLE AND TRIANGLE

PROGRAM

rl=int(input("Enter the length of the rectangle:"))

rb=int(input("Enter the breadth of the rectangle:"))

rarea=float(rl*rb)

si=int(input("Enter the side of the square:"))

sarea=float(si*si)

cr=int(input("Enter the radius of the circle:"))

carea=float((3.14)*cr*cr)

th=int(input("Enter the height of the triangle:"))

tb=int(input("Enter the base of the triangle:"))

tarea=float((0.5)*(tb)*(th))

print("The area of rectangle:",rarea)

print("The area of square:",sarea)

print("The area of circle:",carea)

print("The area of triangle:",tarea)


OUTPUT

Enter the length of the rectangle:6

Enter the breadth of the rectangle:4

Enter the side of the square:3

Enter the radius of the circle:2

Enter the height of the triangle:5

Enter the base of the triangle:4

The area of rectangle: 24.0

The area of square: 9.0

The area of circle: 12.56

The area of triangle: 10.0


4. FIND PRIME NUMBERS IN BETWEEN GIVEN TWO NUMBERS
PROGRAM
lower = 900
upper = 1000

print("Prime numbers between", lower, "and", upper, "are:")

for num in range(lower, upper + 1):


if num > 1:
for i in range(2, num):
if (num % i) == 0:
break
else:
print(num)
OUTPUT
Prime numbers between 900 and 1000 are:
907
911
919
929
937
941
947
953
967
971
977
983
991
997
5. FACTORIAL USING RECURSION

PROGRAM

def recur_factorial(n):

if n == 1:

return n

else:

return n*recur_factorial(n-1)

num = int(input("Enter the number:"))

if num < 0:

print("Sorry, factorial does not exist for negative numbers")

elif num == 0:

print("The factorial of 0 is 1")

else:

print("The factorial of", num, "is", recur_factorial(num))


OUTPUT

Enter the number:5

The factorial of 5 is 120

Enter the number:0

The factorial of 0 is 1

Enter the number:-4

Sorry, factorial does not exist for negative numbers


6. F IBONACCI SERIES

PROGRAM

nterms = int(input("How many terms?: "))

n1, n2 = 0, 1

count = 0

if nterms <= 0:

print("Please enter a positive integer")

elif nterms == 1:

print("Fibonacci series upto",nterms,":")

print(n1)

else:

print("Fibonacci series:")

while count < nterms:

print(n1)

nth = n1 + n2

n1 = n2

n2 = nth

count += 1
OUTPUT

How many terms?: 8

Fibonacci series:

13
7. COUNT THE NUMBER OF EVEN AND ODD NUMBERS

PROGRAM

list1 = [10, 21, 4, 45, 66, 93, 1]

even_count, odd_count = 0, 0

for num in list1:

if num % 2 == 0:

even_count += 1

else:

odd_count += 1

print("Even numbers in the list: ", even_count)

print("Odd numbers in the list: ", odd_count)


OUTPUT

Even numbers in the list: 3

Odd numbers in the list: 4


8. REVERSE A STRING WORD BY WORD

PROGRAM

class py_solution:

def reverse_words(self, s):

return ' '.join(reversed(s.split()))

print(py_solution().reverse_words('Hello World'))
OUTPUT

World Hello
9. TO COUNT THE OCCURRENCES OF ALL ITEMS OF THE LIST IN THE TUPLE

PROGRAM

input_tuple = ('a', 'a', 'c', 'b', 'd')


input_list = ['a', 'b']
count = 0
for item in input_list:
count += input_tuple.count(item)
print("Output:", count)
OUTPUT

Output: 3
10. CREATE A SAVINGS ACCOUNT CLASS (USING INHERITANCE)
PROGRAM
class BankAccount:
def __init__(self, account_number, balance=0.0):
self.account_number = account_number
self.balance = balance

def deposit(self, amount):


if amount > 0:
self.balance += amount
print(f"Deposited: {amount}. New Balance: {self.balance}")
else:
print("Deposit amount must be positive.")

def withdraw(self, amount):


if 0 < amount <= self.balance:
self.balance -= amount
print(f"Withdrew: {amount}. New Balance: {self.balance}")
else:
print("Insufficient funds or invalid withdrawal amount.")

def display_balance(self):
print(f"Account {self.account_number} Balance: {self.balance}")

class SavingsAccount(BankAccount):
def __init__(self, account_number, balance=0.0, interest_rate=0.0):
super().__init__(account_number, balance)
self.interest_rate = interest_rate # Interest rate as a percentage

def apply_interest(self):
interest = self.balance * (self.interest_rate / 100)
self.balance += interest
print(f"Interest applied at {self.interest_rate}%. New Balance: {self.balance}")

savings_account = SavingsAccount(account_number=12345, balance=1000, interest_rate=5)


savings_account.display_balance()
savings_account.deposit(500)
savings_account.withdraw(300)
savings_account.apply_interest()
savings_account.display_balance()
OUTPUT
Account 12345 Balance: 1000
Deposited: 500. New Balance: 1500
Withdrew: 300. New Balance: 1200
Interest applied at 5.0%. New Balance: 1260.0
Account 12345 Balance: 1260.0
11. TO CONSTRUCT THE PATTERN
PROGRAM
rows = 5
for i in range(1, rows + 1):
print(' ' * (rows - i) + '* ' * i)
for i in range(rows - 1, 0, -1):
print(' ' * (rows - i) + '* ' * i)
OUTPUT

*
**
***
****
*****
****
***
**
*
12. MATRIX MULTIPLICATION

PROGRAM

matrix_a = [[1, 2], [3, 4]]


matrix_b = [[5, 6], [7, 8]]

result = [[0, 0], [0, 0]]

for i in range(2):
for j in range(2):
result[i][j] = (matrix_a[i][0] * matrix_b[0][j] +
matrix_a[i][1] * matrix_b[1][j])

for row in result:


print(row)
OUTPUT

[19, 22]
[43, 50]
13. PASCAL'S TRIANGLE
PROGRAM
from math import factorial
n=5
for i in range(n):
for j in range(n-i+1):
print(end=" ")
for j in range(i+1):
print(factorial(i)//(factorial(j)*factorial(i-j)), end=" ")
print()
OUTPUT

1
11
121
1331
14641
14. READ A FILE CONTENT AND COPY ONLY THE CONTENTS AT ODD AND
EVEN LINES INTO A SEPARATE FILE

PROGRAM

f=open("sample.txt","r")

line=f.readlines()

evtxt=[]

odtxt=[]

for i,line in enumerate(line):

if (i+1)%2==0:

evtxt.append(line)

else:

odtxt.append(line)

print("Even line are")

for x in evtxt:

print(x,end="")

print("Odd line are")

for x in odtxt:

print(x,end="")
OUTPUT

Even line are

two

four

six

eight

ten

Odd line are

one

three

five

seven

nine
15.. CREATION OF TURTLE GRAPHICS WINDOW

PROGRAM

import turtle

turtle.screensize(canvwidth=400, canvheight=400, bg="red")

drawing_area=turtle.Screen()

drawing_area.setup(width=550,height=330)
OUTPUT

You might also like