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

PROGRAMs Python Final

The document contains a list of 22 programming tasks with objectives and sample code for each task, covering various programming concepts such as data types, arithmetic operations, control structures, functions, classes, and object-oriented programming in Python. Each program includes a brief description of its purpose and example output. The tasks are designed to help learners practice and understand fundamental programming skills.
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 views23 pages

PROGRAMs Python Final

The document contains a list of 22 programming tasks with objectives and sample code for each task, covering various programming concepts such as data types, arithmetic operations, control structures, functions, classes, and object-oriented programming in Python. Each program includes a brief description of its purpose and example output. The tasks are designed to help learners practice and understand fundamental programming skills.
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

S.

No Remar
. Program’s list Page k
No.
01. Write a program to take user input and display 01
datatype of the input.

02. Write a program to perform arithmetic operations on 02


two numbers.
03. Write a program to convert Temperature celcius to 03
Fahrenheit.
04. Write a program to calculate area and perimeter of 04
rectangle.
Write a program whether a number is even or odd 05
05. using a operator.
Write a program to swap two numbers using a 06
06. function.
07. Write a program to calculate simple interest. 07

08. Write a program to using relational and logical 08


operators.
09. Write a program to demonstrate input and output 09
statements.
10. Write a program to check whether a number is 10
prime.
11. Write a program to print multiplication table using 11
loop.
12. Write a program to find factorial oof a number . 12

13. Write a program to print fibbonacci series. 13

14. Write a function to find maximum amoung three 14


numbers.
15. Write a program to check pallindrome number / 15
string .
16. Write a program to create a class and object in 16
Python.
17. Write a program to demonstrate constructor in 17
python.
18. Write a program to demonstrate inheritance. 18

19. Write a program to demonstrate polymorphism. 19

20. Write a program to demonstrate method overriding. 20

21. Write a program using iterator. 21

22. Write generator function to generate square 22


numbers.
PROGRAM-01

Objective :- Write a program to take user input and display datatype


of the input.

x=input("Enter a value : ")


print("value is ",x," and it's type ",type(x))
y=input("Enter a value : ")
print("value is ",y," and it's type ",type(y))
print("This output is presented by Shanu Singh")

Output:-
PROGRAM-02

Objective :- Write a program to perform arithmetic operations on


two numbers.

x=21
y=2
add=x+y
print("Addition",add)
dif=x-y
print("Subtraction",dif)
prod=x*y
print("Multiplication",prod)
div=x/y
print("Divison",div)
fdiv=x//y
print("Floor divison",fdiv)
rem=x%y
print("Modulus(remainder)",rem)
pow=x**y
print("Exponentiation",pow)
print("\nThis output is presented by Shanu Singh")

Output:-
PROGRAM-03

Objective :- Write a program to convert Temperature celcius to


Fahrenheit.

def celcius_fahrenheit(c):
# Converts a celcius value to fahrenheit
return (c * 1.8) + 32
temp_c=float(input("Enter temperature in Celcius "))
temp_f=celcius_fahrenheit(temp_c)
print("Temperature in fahrenheit :- ",temp_f)
print("\nThis output is presented by Shanu Singh")

Output:-
PROGRAM-04

Objective :- Write a program to calculate area and perimeter of


rectangle.

length=float(input("Enter length of rectangle: "))


width=float(input("Enter width of rectangle: "))
print("Perimeter of rectangle is : ",2*(length+width))
print("Area of rectangle is : ",(length*width))
print("\nThis input is presented by Shanu Singh")

Output:-
PROGRAM-05

Objective :- Write a program whether a number is even or odd using


a operator.

counter=1
while counter <= 5:
num=int(input("Enter a number to check : "))
print("Even") if num % 2 == 0 else print("Odd")
counter += 1
print("\nThis input is presented by Shanu Singh")

Output:-
PROGRAM-06

Objective :- Write a program to swap two numbers using a function.

def swap(a,b):
print("Originsl value of a and b is : ",a,b)
a,b=b,a
print("After swapping value of a and b is : ",a,b)
a=int(input("Enter value 1 : "))
b=int(input("Enter value 2 : "))
swap(a,b)
print("\nThis output is presented by shanu Singh")
PROGRAM-07

Objective :- Write a program to calculate simple interest.

principal=float(input("Enter the principal amount: "))


rate=float(input("Enter the annual interest rate (in % ): "))
time=float(input("Enter time (in years): "))
SI=(principal*rate*time)/100
total_amount=principal+SI
print(f"Simple Interest is : {SI:.2f}")
print(f"Total amount you have to pay is : {total_amount:.2f}")
print("\nThis output is presented by Shanu Singh")

Output:-
PROGRAM-08

Objective :- Write a program to using relational and logical


operators.
var=5
print(var>0)
print(not(var<=0))
var=6
print(var!=0)
print(not(var==0))
p=True
q=True
print(not(p and q)== ((not p) or (not q)))
print(not(p or q)== ((not p) and (not q)))
i=1
j= not i
print(j)
j=not not i
print(j)
print("\nThis output is presented by Shanu Singh")

Output:-
PROGRAM-09

Objective :- Write a program to demonstrate input and output


statements.

name=input("enter your name : ") #type is string


print("Name is : ",name)
age=int(input("enter your age: ")) #type is int
print(f"Age {name} is {age} .")
price=float(input("Enter price of phone: ")) #type is float
print("Price of mobile phone is :",price)
evaluate1=eval(input("enter any equation here: "))
print(evaluate1)
print("\nThis output is presented by Shanu Singh")

Output:-
PROGRAM-10

Objective :- Write a program to check whether a number is prime.

num=int(input("enter a number: "))


if num<=1:
print("it is not a prime num")
else :
for i in range(2,num):
if num%2==0:
print("it is not a prime number")
break
else:
print("it is prime")
break
print("\nThis output is presented by Shanu Singh")

Output:-
PROGRAM-11

Objective :- Write a program to print multiplication table using loop.

n=int(input("Enter a number whose table you want to print: "))


i=1
while i <=10:
print(f" {n} * {i} = {n*i}")
i+=1
print("\nThis output is presented by Shanu Singh ")

Output:-
PROGRAM-12

Objective :- Write a program to find factorial oof a number .

def fact(n):
if n<0:
return None
if n<2:
return 1
product=1
for i in range(2,n+1):
product*=i
return product
for i in range(6):
print(i,"->",fact(i))
print("\nThis output is presented by shanu Singh")

Output:-
PROGRAM-13

Objective :- Write a program to print fibbonacci series.

def fib(n):
if n==0:
return 0
if n==1:
return 1

e1,e2=0,1
s=0
for i in range(2,n+1):
s=e1+e2
e1,e2=e2,s
return s
for n in range(10):
print(n,"->",fib(n))
print("\nThis output is presented by shanu Singh")

Output:-
PROGRAM-14

Objective :- Write a function to find maximum amoung three


numbers.

def max_num(a,b,c):
if a>b and a>c:
print(f"{a} is greater")
elif b>a and b>c:
print(f"{b} is greater")
else:
print(f"{c} is greater")
a=int(input("Enter value of num1 "))
b=int(input("Enter value of num2 "))
c=int(input("Enter value of num3 "))
max_num(a,b,c)
print("\nThis output is presented by Shanu Singh")

Output:-
PROGRAM-15

Objective :- Write a program to check pallindrome number / string .

def is_pallindrome():
data=input("enter a number or string ")
original_data=[Link]()
reversed_data=original_data[::-1]
if original_data == reversed_data:
print(f"{data} is a Pallindrome")
else:
print(f"{data} is not a Pallindrome")
is_pallindrome()
print("\nThis output is presented by Shanu Singh")

Output:-
PROGRAM-16

Objective :- Write a program to create a class and object in Python.

class details:
age=21
name="Shanu Singh"
def desc(self):
print("My name is ",[Link]," and I am ",[Link]," yaers old")
obj = details()
[Link]()
print("\nThis output is presented by Shanu Singh")

Output:-
PROGRAM-17

Objective :- Write a program to demonstrate constructor in python.

class Student:
def __init__(self,n,m):
[Link]=n
[Link]=m
def display(self):
print(f"Student name is {[Link]} and marks are {[Link]}")
def add_bonus(self,bonus):
[Link]+=bonus
s1=Student("Shanu",555)
[Link]()
s1.add_bonus(2)
[Link]()
print("\nThis output is presented by Shanu Singh")

Output:-
PROGRAM-18

Objective :- Write a program to demonstrate inheritance.

class A:
def dis(self):
print("welcome to")
class B(A):
def disB(self):
print("ws code")
obj=B()
[Link]()
[Link]()
print("\nThis output is presented by Shanu Singh")

Output:-
PROGRAM-19

Objective :- Write a program to demonstrate polymorphism.

class Area():
def findarea(self,l=None,b=None):
if l!=None and b!=None:
print(l*b)
elif l!=None:
print(l*l)
else:
print("nothing")
area=Area()
[Link](2,4)
[Link](2)
[Link]()
print("\nThis output is presented by Shanu Singh")

Output:-
PROGRAM-20

Objective :- Write a program to demonstrate method overriding.

class A:
def show(self):
print("I am from class A")
class B(A):
def show(self):
super().show()
print("I am from class B")
ob=B()
[Link]()
print("\nThis output is presented by Shanu Singh")

Output:-
PROGRAM-21

Objective :- Write a program using iterator.

nums=[10,20,30,40]
it=iter(nums)
print(next(it))
print(next(it))
print(next(it))
print(next(it))
print("\nThis output is presented by Shanu Singh")

Output:-
PROGRAM-22

Objective :- Write generator function to generate square numbers.

def square_geneator(n):
for i in range(1,n+1):
yield i * i
gen=square_geneator(5)
print(next(gen))
print(next(gen))
print(next(gen))
print("\nThis output is presented by Shanu Singh")

Output:-

You might also like