0% found this document useful (0 votes)
8 views19 pages

Extra Programs - Python

The document contains various Python programming exercises, including generating Pascal's triangle, printing prime numbers, converting temperatures, calculating areas of geometric shapes, and finding factorials. It also includes programs for string manipulation, counting even and odd numbers, and creating Turtle graphics. Each section provides code snippets and explanations for different programming tasks.

Uploaded by

kcskncreels
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)
8 views19 pages

Extra Programs - Python

The document contains various Python programming exercises, including generating Pascal's triangle, printing prime numbers, converting temperatures, calculating areas of geometric shapes, and finding factorials. It also includes programs for string manipulation, counting even and odd numbers, and creating Turtle graphics. Each section provides code snippets and explanations for different programming tasks.

Uploaded by

kcskncreels
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

1.

A)Write a Python program to generate


1
11
121
1331
14641
n=5
for i in range(1, n+1):
for j in range(0, n-i+1):
print(' ', end='')
C=1
for j in range(1, i+1):
print(' ', C, sep='', end='')
C = C * (i - j)
print()
(or)
def pascal_triangle(n):
row = [1]
y = [0]
for x in range(n):
print(" " * (n - x - 1), end="")
print(*row, sep=" ")
row = [left + right for left, right in zip(row + y, y + row)]
pascal_triangle(5)
1(b)Write a python program to print prime numbers less than 20
Num = int(input ("Please, Enter the Number "))
print ("The Prime Numbers in the range are: ")
for number in range (0,Num+1):
if number > 1:
for i in range (2, number):
if (number % i) == 0:
break
else:
print (number)
2.A) Program to convert the given temperature from Fahrenheit to
Celsius and vice versa depending upon users choice.
print("Convert Celsius to Farenheit and vice versa")
i=1
while i<=3:
choice=input("Enter your Choice:")
if choice=='1':
c=int(input("Enter the Temperature in Celcius:"))
f = (9/5)*c + 32
print("Temperature in Farenheitis:",f)
elif choice=='2':
f=int(input("Enter the Temperature in Farenheit:"))
c = (5/9)*(f - 32)
print("Temperature in Farenheitis:",c)
elif choice=='3':
break
i=i+1
2.B) Program, to find the area of rectangle, square, circle and triangle
by accepting suitable input parameters from user.
print("Program to find the area of rectangle, square, circle and triangle ")
print("-----------------------------------------------------------------------------")
i=1
while i<=4:
print("1: Area of a Rectangle\n2: Area of a Square\n3: Area of a Circle \n4:
Area of a Triangle ")
option = int(input("Enter your choice: "))
if option==1:
print("AREA OF RECTANGLE")
print("-----------------")
length = int(input("Enter length of Rectangle: "))
breadth = int(input("Enter breadth of Rectangle:"))
area = length*breadth
print("\nArea of Rectangle =",area)
elif option==2:
print("AREA OF SQUARE")
print("--------------")
side = int(input("Enter side length of Square:"))
area_square = side*side
print("\nArea of Square =",area_square)
elif option==3:
print("AREA OF CIRCLE")
print("--------------")
radius = float(input("Enter radius of circle: "))
area = 3.14*radius*radius;
print("\nArea of Circle = ", area)
elif option==4:
print("AREA OF TRIANGLE")
print("----------------")
breadth = float(input("Enter Breadth of Triangle: "))
height = float(input("Enter Height of Triangle: "))
area = (breadth*height)/2
print("\nArea of Triangle = ", area)
else:
print("Invalid Option")
break
i=i+1
3. (a) Program to calculate total marks, percentage and grade of a
student. Marks obtained in each of the five subjects are to be input by
user. Assign grades according to the following criteria: Grade A:
Percentage > = 80 Grade B: Percentage > = 70 and < 80 Grade C:
Percentage >=60 Grade D: Percentage >=40 and
Grade E: Percentage <40
print("Program to calculate Grade of a Student ")
n = int(input("Enter how many Subjects: "))
print("Enter the marks for 5 subjects:")
sum=0.0
mark=[ ]
for i in range(n):
[Link](i,int(input()))
for i in range(n):
sum=sum+mark[i]
print("Total Marks",sum)
percentage = (sum/500)*100;
print("Percentage",percentage)
if(percentage >=80 and percentage <=100):
print("Your Grade is A")
elif(percentage >=70 and percentage <80):
print("Your Grade is B")
elif(percentage >=60 and percentage<70):
print("Your Grade is C")
elif(percentage >=40 and percentage<60):
print("Your Grade is D")
else:
print("Your Grade is E")
(b) Write a python program that create Turtle graphics window with
specific size.
import turtle
[Link](800,600)
window=[Link]()
[Link]('Drawing an A')
the_turtle = [Link]()
the_turtle.hideturtle()
the_turtle.penup()
[Link](-100,0)
the_turtle.pendown()
the_turtle.setposition(0,250)
the_turtle.setposition(100,0)
the_turtle.penup()
the_turtle.setposition(-100,0)
the_turtle.penup()
the_turtle.setposition(-64,90)
the_turtle.pendown()
the_turtle.setposition(64,90)
[Link]()
4. a)Write a program to print the reverse order of the string
“MALAYALAM”.
s = "MALAYALAM"
rev = ''.join(reversed(s))
print(rev)
. b)Write a Python program to find largest of three numbers.
num1 = 10
num2 = 14
num3 = 12
if (num1 >= num2) and (num1 >= num3):
largest = num1
elif (num2 >= num1) and (num2 >= num3):
largest = num2
else:
largest = num3
print("The largest number is", largest)
5. a) Use for Iterative statements in Python program and find the
factorial of a number.
def factorial_iterative(n):
if n < 0:
return "Factorial is not defined for negative numbers."
elif n == 0 or n == 1:
return 1
else:
result = 1
for i in range(1, n + 1):
result *= i
return result
(b) Write a Python program to find sum of first 5 numbers
num = 5
sum = 0
for i in range(num+1):
sum=sum+=i
print(sum)
6. a)Write a Python program to demonstrate the Tower of Hanoi using
recursion.
print("TOWER OF HANOI PROBLEM")
print("----------------------")
def hanoi(disks, source, auxiliary, target):
if disks == 1:
print('Move disk 1 from peg {} to peg {}.'.format(source, target))
return
hanoi(disks - 1, source, target, auxiliary)
print('Move disk {} from peg {} to peg {}.'.format(disks, source, target))
hanoi(disks - 1, auxiliary, source, target)
disks = int(input('Enter number of disks: '))
hanoi(disks, 'A', 'B', 'C')
(b) Write a python program that create Turtle graphics window with
specific size.
import turtle
[Link](800,600)
window=[Link]()
[Link]('Drawing an A')
the_turtle = [Link]()
the_turtle.hideturtle()
the_turtle.penup()
[Link](-100,0)
the_turtle.pendown()
the_turtle.setposition(0,250)
the_turtle.setposition(100,0)
the_turtle.penup()
the_turtle.setposition(-100,0)
the_turtle.penup()
the_turtle.setposition(-64,90)
the_turtle.pendown()
the_turtle.setposition(64,90)
[Link]()
7.(a) Program to convert the given temperature from Fahrenheit to
Celsius and vice versa depending upon user’s choice.
print("Convert Celsius to Farenheit and vice versa")
i=1
while i<=3:
choice=input("Enter your Choice:")
if choice=='1':
c=int(input("Enter the Temperature in Celcius:"))
f = (9/5)*c + 32
print("Temperature in Farenheitis:",f)
elif choice=='2':
f=int(input("Enter the Temperature in Farenheit:"))
c = (5/9)*(f - 32)
print("Temperature in Farenheitis:",c)
elif choice=='3':
break
i=i+1

(b) Program, to find the area of square by accepting suitable input


parameters from user.
print("AREA OF SQUARE")
side = int(input("Enter side length of Square:"))
area_square = side*side
print("\nArea of Square =",area_square)
8.(a) Write a python program with tuple and list as input to count the
occurrences of all items of the list in the tuple.
def countOccurrence(tup, lst):
count = 0
for item in tup:
print(item)
if item in lst:
print (lst)
count+= 1
return count
# Driver Code
tup = ('a', 'a', 'c', 'b', 'd')
lst = ['a','b']
print("count occurrence of all elements of list in a
tuple:",countOccurrence(tup, lst))
(b) Write a Python program to count the number of even and odd
numbers from array of N numbers.
NumList = []
Even_count = 0
Odd_count = 0
print("To count the number of even and odd numbers from array of N
numbers")
print("-------------------------------------------------------------------")
Number = int(input("Please enter the Total Number of List Elements: "))
for i in range(1, Number + 1):
value = int(input("Please enter the Value of %d Element : " %i))
[Link](value)
for j in range(Number):
if(NumList[j] % 2 == 0):
Even_count = Even_count + 1
else:
Odd_count = Odd_count + 1
print("\nTotal Number of Even Numbers in this List = ", Even_count)
print("Total Number of Odd Numbers in this List = ", Odd_count)
9. (a) Program, to find the area of triangle by accepting suitable input
parameters from user.
print("AREA OF TRIANGLE")
breadth = float(input("Enter Breadth of Triangle: "))
height = float(input("Enter Height of Triangle: "))
area = (breadth*height)/2
print("\nArea of Triangle = ", area)
(b) Program to find factorial of the given number using recursive
function.
print("FACTORIAL OF GIVEN NUMBER :")
def factorial(n):
if(n <= 1):
return 1
else:
return(n*factorial(n-1))
n = int(input("Enter number:"))
print("The factorial of",n,"is",factorial(n))
10.(a) Write a Program to demonstrate list in python.
num=[10,45,20,50,53,98]
for k in num:
print(k)
for ch in 'BCA':
print(ch)
tup=('orange','apple','banana')
for i in tup:
print(i)
(b) Write a Python program to find the sum of the series of n = 10
sum = 0
for i in range(1, 11):
sum=sum+i
print(sum)
11.(a) Program, to find the area of rectangle, by accepting suitable
input parameters from user.
print("AREA OF RECTANGLE")
length = int(input("Enter length of Rectangle: "))
breadth = int(input("Enter breadth of Rectangle:"))
area = length*breadth
print("\nArea of Rectangle =",area)
(b) Write a python program using a tuple.
num=[10,45,20,50,53,98]
for k in num:
print(k)
for ch in 'BCA':
print(ch)
tup=('orange','apple','banana')
for i in tup:
print(i)
12.(a) Program, to find the area of rectangle, circle and triangle by
accepting suitable input parameters from user.
print("Program to find the area of rectangle, square, circle and triangle ")
print("-----------------------------------------------------------------------------")
i=1
while i<=4:
print("1: Area of a Rectangle\n2: Area of a Square\n3: Area of a Circle \n4:
Area of a Triangle ")
option = int(input("Enter your choice: "))
if option==1:
print("AREA OF RECTANGLE")
print("-----------------")
length = int(input("Enter length of Rectangle: "))
breadth = int(input("Enter breadth of Rectangle:"))
area = length*breadth
print("\nArea of Rectangle =",area)
elif option==2:
print("AREA OF SQUARE")
print("--------------")
side = int(input("Enter side length of Square:"))
area_square = side*side
print("\nArea of Square =",area_square)
elif option==3:
print("AREA OF CIRCLE")
print("--------------")
radius = float(input("Enter radius of circle: "))
area = 3.14*radius*radius;
print("\nArea of Circle = ", area)
elif option==4:
print("AREA OF TRIANGLE")
print("----------------")
breadth = float(input("Enter Breadth of Triangle: "))
height = float(input("Enter Height of Triangle: "))
area = (breadth*height)/2
print("\nArea of Triangle = ", area)
else:
print("Invalid Option")
break
i=i+1
(b) Write a program using a for loop that loops over a sequence.
num=[10,45,20,50,53,98]
for k in num:
print(k)
for ch in 'BCA':
print(ch)
tup=('orange','apple','banana')
for i in tup:
print(i)
12. (a) Write a Python program to reverse a string.
original_string = "MALAYALAM"
reversed_string = original_string[::-1]
print(reversed_string)
(b) Write a Python program to count the number of even and odd
numbers from array of N numbers
NumList = []
Even_count = 0
Odd_count = 0
print("To count the number of even and odd numbers from array of N
numbers")
print("-------------------------------------------------------------------")
Number = int(input("Please enter the Total Number of List Elements: "))
for i in range(1, Number + 1):
value = int(input("Please enter the Value of %d Element : " %i))
[Link](value)

for j in range(Number):
if(NumList[j] % 2 == 0):
Even_count = Even_count + 1
else:
Odd_count = Odd_count + 1

print("\nTotal Number of Even Numbers in this List = ", Even_count)


print("Total Number of Odd Numbers in this List = ", Odd_count)
14. (a) Write a Python program to count the number of even and odd
numbers from array of N numbers.
NumList = []
Even_count = 0
Odd_count = 0
print("To count the number of even and odd numbers from array of N
numbers")
print("-------------------------------------------------------------------")
Number = int(input("Please enter the Total Number of List Elements: "))
for i in range(1, Number + 1):
value = int(input("Please enter the Value of %d Element : " %i))
[Link](value)

for j in range(Number):
if(NumList[j] % 2 == 0):
Even_count = Even_count + 1
else:
Odd_count = Odd_count + 1

print("\nTotal Number of Even Numbers in this List = ", Even_count)


print("Total Number of Odd Numbers in this List = ", Odd_count)
(b) Write a Python program to concatenate two strings using string
functions.
var1 = "Hello "
var2 = "Geek"
var3 = var1 + var2
print(var3)
15. (a) Write a Python program to count the number of even and odd
numbers from array of N numbers.
NumList = []
Even_count = 0
Odd_count = 0
print("To count the number of even and odd numbers from array of N
numbers")
print("-------------------------------------------------------------------")
Number = int(input("Please enter the Total Number of List Elements: "))
for i in range(1, Number + 1):
value = int(input("Please enter the Value of %d Element : " %i))
[Link](value)

for j in range(Number):
if(NumList[j] % 2 == 0):
Even_count = Even_count + 1
else:
Odd_count = Odd_count + 1

print("\nTotal Number of Even Numbers in this List = ", Even_count)


print("Total Number of Odd Numbers in this List = ", Odd_count)

b)Write a Python program to find the smallest and largest numbers


among 23,45,67,89,100
l=[23,45,67,89,100]
print("Largest number",max(l))
print("Smallest number",min(l))
16.(a) Write a python script that prints prime numbers less than 10.
Num = int(input ("Please, Enter the Number "))
print ("The Prime Numbers in the range are: ")
for number in range (0,Num+1):
if number > 1:
for i in range (2, number):
if (number % i) == 0:
break
else:
print (number)
(b) Write a python program using a tuple and list.
num=[10,45,20,50,53,98]
for k in num:
print(k)
for ch in 'BCA':
print(ch)
tup=('orange','apple','banana')
for i in tup:
print(i)
17.(a) Write a Python program to count the number of even and odd
numbers from array of N numbers.
Even_count = 0
Odd_count = 0
print("To count the number of even and odd numbers from array of N
numbers")
print("-------------------------------------------------------------------")
Number = int(input("Please enter the Total Number of List Elements: "))
for i in range(1, Number + 1):
value = int(input("Please enter the Value of %d Element : " %i))
[Link](value)

for j in range(Number):
if(NumList[j] % 2 == 0):
Even_count = Even_count + 1
else:
Odd_count = Odd_count + 1
print("\nTotal Number of Even Numbers in this List = ", Even_count)
print("Total Number of Odd Numbers in this List = ", Odd_count)
(b) Use break statement in Python iterative statement.
print("Convert Celsius to Farenheit and vice versa")
i=1
while i<=3:
choice=input("Enter your Choice:")
if choice=='1':
c=int(input("Enter the Temperature in Celcius:"))
f = (9/5)*c + 32
print("Temperature in Farenheitis:",f)
elif choice=='2':
f=int(input("Enter the Temperature in Farenheit:"))
c = (5/9)*(f - 32)
print("Temperature in Farenheitis:",c)
elif choice=='3':
break
i=i+1
18. (a) Program to convert the given temperature from Fahrenheit to
Celsius and vice versa depending upon user’s choice.
print("Convert Celsius to Farenheit and vice versa")
i=1
while i<=3:
choice=input("Enter your Choice:")
if choice=='1':
c=int(input("Enter the Temperature in Celcius:"))
f = (9/5)*c + 32
print("Temperature in Farenheitis:",f)
elif choice=='2':
f=int(input("Enter the Temperature in Farenheit:"))
c = (5/9)*(f - 32)
print("Temperature in Farenheitis:",c)
elif choice=='3':
break
i=i+1

(b) Write a Python program to find the factorial of 20 using function.


print("FACTORIAL OF GIVEN NUMBER :")
def factorial(n):
if(n <= 1):
return 1
else:
return(n*factorial(n-1))
n = 20
print("The factorial of",n,"is",factorial(n))

You might also like