Python Assiginemt (On) - 1
Python Assiginemt (On) - 1
UNIVERSITY OF CALCUTTA
SESSION -2022-2023
(PYTHON PROGRAMMING ASSIGNMET-2023)
~1~
INDEX
SL Program List Date Page no Teacher’s
No. signature
1. Write a program in python to calculate the average of five numbers. 04/01/23 4-5
2.
Write a program in python to create a basic calculator using switch 04/01/23 6-9
case.
3.
Write a program in python to check whether the given number is odd or 04/01/23 10-11
even.
4.
Write a program in python to check whether the given year is leap year 04/01/23 12-13
or not.
6.
Write a program in python to find the multiplication table of a given 04/01/23 16-17
number.
7.
Write a Python Program to read a number n and print and compute the 04/01/23 18-19
series “1+2+…+n=”.
8.
Write a Python Program to read a number n and print and compute the 04/01/23 20-21
series “1+4+7+10…+n=”.
9.
Write a Python Program to read a number n and print and compute the 04/01/23 22-23
series “5+10+15+…+n=”.
11.
Write a program in python to check whether a given number is prime 04/01/23 26-27
or composite.
12.
Write a program in python to calculate the Prime factor of a given 04/01/23 28-29
number.
13.
Write a program in python to check whether a given number is perfect 04/01/23 30-31
number or not.
14.
Write a program in python to find the reverse of a given number and 04/01/23 32-33
also check the number is palindrome or not.
15.
Write a program in python to calculate the Prime number between 04/01/23 34-35
given range.
16.
Write a program that takes two numbers and prints the LCM of two 04/01/23 36-37
numbers.
17.
Write a program that takes two numbers and prints the GCD of two 04/01/23 38-39
numbers.
21.
Write a program in python to calculate the perimeter in circle using 04/01/23 46-47
math.pi
Write a program in python to input three positive integers a, b, and c , using 04/01/23
22. Pythagorean triples formula ( a2+ b2 =c2.) to generate all Pythagorean triples
48-49
in a certain range.
~2~
23. Write a program in python to check the number is Krishnamurthy or not.
04/01/23 50-51
Write a program in python to print the pattern of given below: 04/01/23
24. 12345
52-53
1234
123
12
1
Write a program in python to print the pattern of given below: 04/01/23
25. *****
54-55
****
***
**
*
Write a program in python to print the pattern of given below: 04/01/23
26. 54321
56-57
4321
321
21
1
29.
Write a program to take a string as input and counts the number of 04/01/23 62-63
vowels in a string.
31.
Write a program in python to accept three digits and print all possible 04/01/23 66-67
combination
32.
Write a program in python to check whether a number is Pythagorean 04/01/23 68-69
triplets or not.
33. Write a program in python to take three inputs from the user and print 04/01/23
the 1st one in reverse and the 2nd string converted to upper case and 70-71
the 3rd string converted to lower case.
34.
Write a program in python to ask user for two string and concatenate 04/01/23 72
them.
35.
Write a program in python to take two strings as input from the user 04/01/23 73-74
and print the 1st one twice and the other one thrice
38.
Write a program in python to Use list comprehension to find all the odd 04/01/23 79-80
numbers and numbers divisible by 3 from a list of numbers.
~3~
ASSIGNMENT: 1. DATE:04/01/23
ALGORITHM:
Step 1: Initialize TOTAL and COUNTER
Step 6: End-while
Step 9: STOP
SOURCE CODE:
a=int(input("Enter the 1st number"))
total=(a+b+c+d+e)
average=(total)/5
OUTPUT:2
Enter the 1st number25
DISCUSSION:
The average value in a set of numbers is the middle value, calculated by dividing the total of
all the values by the number of values. When we need to find the average of a set of data,
we add up all the values and then divide this total by the number of values.
__________________
TEACHER’S SIGNATURE
~5~
ASSIGNMENT: 2. DATE:
PROGRAM STATEMENT: Write a program in python to
create a basic calculator using switch case.
ALGORITHM :
Step 1: Set different values for different operations to be executed.
Step 2: Now ask from user to choose the value corresponding to the operation
to be performed
SOURCECODE:
print("Calculator")
print("1.Add")
print("2.Substract")
print("3.Multiply")
print("4.Divide")
if ch==1:
a=int(input("Enter A:"))
b=int(input("Enter B:"))
c=a+b
print("Sum = ",c)
elif ch==2:
~6~
a=int(input("Enter A:"))
b=int(input("Enter B:"))
c=a-b
print("Difference = ",c)
elif ch==3:
a=int(input("Enter A:"))
b=int(input("Enter B:"))
c=a*b
print("Product = ",c)
elif ch==4:
a=int(input("Enter A:"))
b=int(input("Enter B:"))
c=a/b
print("Quotient = ",c)
else:
print("Invalid Choice")
OUTPUT:1
Calculator
1.Add
2.Substract
3.Multiply
4.Divide
Enter Choice(1-4): 1
Enter A:10
~7~
Enter B:20
Sum = 30
OUTPUT:2
Calculator
1.Add
2.Substract
3.Multiply
4.Divide
Enter Choice(1-4): 2
Enter A:20
Enter B:5
Difference = 15
OUTPUT:3
Calculator
1.Add
2.Substract
3.Multiply
4.Divide
Enter Choice(1-4): 3
Enter A:15
Enter B:10
Product = 150
~8~
OUTPUT:4
Calculator
1.Add
2.Substract
3.Multiply
4.Divide
Enter Choice(1-4): 4
Enter A:20
Enter B:5
Quotient = 4.0
OUTPUT:5
Calculator
1.Add
2.Substract
3.Multiply
4.Divide
Enter Choice(1-4): 5
Invalid Choice
DISCUSSION : This is a basic calculator, which asks from user to perform which
operation and then operate it.
__________________
TEACHER’S SIGNATURE
~9~
ASSIGNMENT:3. DATE:
ALGORITHM :
Step 1: START
Step 7: STOP
SOURCE CODE:
a=int(input("Please enter any number"))
n=a%2
if(n==0):
else:
OUTPUT:1
Please enter any number10
~ 10 ~
OUTPUT:2
Please enter any number3
DISCUSSION:
An even number is a number that can be divided into two equal groups. An odd number is a
number that cannot be divided into two equal groups.
__________________
~ 11 ~
TEACHER’SSIGNATURE
~ 12 ~
ASSIGNMENT: 4. DATE:
PROGRAM STATEMENT: Write a program in python to check
whether the given year is leap year or not.
ALGORITHM :
Step 1: Enter a year
SOURCE CODE:
a=int(input("Please enter any year"))
if(a%400==0)or(a%100!=0)and(a%4==0):
else:
OUTPUT:1
Please enter any year2004
~ 13 ~
OUTPUT:2
Plese enter any year1900
DISCUSSION : Source code to check whether a year entered by user is leap year or
not in Python programme. Python if else Statement.
A leap year is exactly divisible by 4 except for century years (years ending with 00). The
century year is a leap year only if it is perfectly divisible by 400. For example:- 2016 is a leap
year , 1900 is a not leap year , 2000 is a leap year.
__________________
TEACHER’SSIGNATURE
~ 14 ~
ASSIGNMENT: 5. DATE:
PROGRAM STATEMENT: Write a program in python to find
the sum of digit of a given number.
ALGORITHM :
Step1: Take the value of the integer and store in a variable.
Step2. Using a while loop, get each digit of the number and add the digits to a
variable.
Step3. Print the sum of the digits of the number.
Step4. Exit..
SOURCE CODE:
n=int(input("Enter a number:"))
total=0
while(n>0):
dig=n%10
total=total+dig
n=n//10
OUTPUT:1
Enter a number:12
OUTPUT:2
Enter a number:2154
~ 15 ~
DISCUSSION :
User must first enter the value and store it in a variable.The while loop is used and the last
digit of the number is obtained by using the modulus operator.The digit is added to another
variable each time the loop is executed.This loop terminates when the value of the number
is 0.The total sum of the number is then printed.
__________________
TEACHER’SSIGNATURE
~ 16 ~
ASSIGNMENT: 6. DATE:
PROGRAM STATEMENT: Write a program in python to find
the multiplication table of a given number.
ALGORITHM:
Step 1: Enter any number
SOURCE CODE:
num = int(input("Enter the number: "))
print(num,"X",i,"=",num * i)
OUTPUT:1
Enter the number: 5
Multiplication Table of 5
5X1=5
5 X 2 = 10
5 X 3 = 15
5 X 4 = 20
5 X 5 = 25
5 X 6 = 30
~ 17 ~
5 X 7 = 35
5 X 8 = 40
5 X 9 = 45
5 X 10 = 50
OUTPUT:2
Enter the number: 16
Multiplication Table of 16
16 X 1 = 16
16 X 2 = 32
16 X 3 = 48
16 X 4 = 64
16 X 5 = 80
16 X 6 = 96
16 X 7 = 112
16 X 8 = 128
16 X 9 = 144
16 X 10 = 160
DISCUSSION: In the program, user is asked to enter the number and the program
prints the multiplication table of the input number using for loop. The loops run from 1 to 10
and the input number is multiplied by the loop counter in each step to display the steps of
multiplication table.
__________________
TEACHER’S SIGNATURE
~ 18 ~
ASSIGNMENT: 7. DATE:
ALGORITHM:
Step1. Take a value from the user and store it in a variable n.
Step 2. Use a for loop where the value of i ranges between the values of 1 and
n.
Step 3. Print the value of i and ‘+’ operator while appending the value of i to a
list.
Step4. Then find the sum of elements in the list.
Step5. Print ‘=’ followed by the total sum.
Step6. Exit.
SOURCE CODE:
n=int(input("Please enter the lowerlimit"))
a=[]
for i in range(1,1+n):
print(i,sep="",end="")
if(1<n):
print("+",sep="",end="")
a.append(i)
print("=",sum(a))
print()
OUTPUT:1
Please enter the lowerlimit5
1+2+3+4+5= 15
~ 19 ~
OUTPUT:2
Please enter the lowerlimit10
1+2+3+4+5+6+7+8+9+10= 55
DISCUSSION: User must first enter the value and store it in a variable.The
while loop is used and the last digit of the number is obtained by using the modulus
operator.
The digit is added to another variable each time the loop is executed.This loop
terminates when the value of the number is 0. The total sum of the number is then
printed.
__________________
TEACHER’S SIGNATURE
~ 20 ~
~ 21 ~
ASSIGNMENT: 8 . DATE:
ALGORITHM:
Step1. Take a value from the user and store it in a variable n.
Step 2. Use a for loop where the value of i ranges between the values of 1 and
n.
Step 3. Print the value of i and ‘+’ operator while appending the value of i to a
list.
Step4. Then find the sum of elements in the list.
Step5. Print ‘=’ followed by the total sum.
Step6. Exit.
SOURCE CODE:
n=int(input("Please enter the lowerlimit"))
a=[]
for i in range(1,n+1,+3):
print(i,sep="",end="")
if(1<n):
print("+",sep="",end="")
a.append(i)
print("=",sum(a))
print()
OUTPUT:1
Please enter the lowerlimit15
1+4+7+10+13= 35
~ 22 ~
OUTPUT:2
Please enter the lowerlimit21
1+4+7+10+13+16+19= 70
DISCUSSION: User must first enter the value and store it in a variable.The
while loop is used and the last digit of the number is obtained by using the modulus
operator.
The digit is added to another variable each time the loop is executed.This loop
terminates when the value of the number is 0. The total sum of the number is then
printed.
__________________
TEACHER’S SIGNATURE
~ 23 ~
ASSIGNMENT: 9. DATE:
PROGRAM STATEMENT: Write a Python Program to read a
number n and print and compute the series “5+10+15+…+n=”.
ALGORITHM:
Step1. Take a value from the user and store it in a variable n.
Step 2. Use a for loop where the value of i ranges between the values of 1 and
n.
Step 3. Print the value of i and ‘+’ operator while appending the value of i to a
list.
Step4. Then find the sum of elements in the list.
Step5. Print ‘=’ followed by the total sum.
Step6. Exit.
SOURCE CODE:
n=int(input("Please enter the lowerlimit"))
a=[]
for i in range(5,n+1,+5):
print(i,sep="",end="")
if(1<n):
print("+",sep="",end="")
a.append(i)
print("=",sum(a))
print()
OUTPUT:1
Please enter the lowerlimit25
5+10+15+20+25+= 75
~ 24 ~
OUTPUT:2
Please enter the lowerlimit48
5+10+15+20+25+30+35+40+45+= 225
DISCUSSION: User must first enter the value and store it in a variable.The
while loop is used and the last digit of the number is obtained by using the
modulus operator.
The digit is added to another variable each time the loop is executed.This loop
terminates when the value of the number is 0. The total sum of the number is
then printed.
__________________
TEACHER’S SIGNATURE
~ 25 ~
ASSIGNMENT: 10 . DATE:
PROGRAM STATEMENT: Write a program in python to
calculate the factor of a given number.
ALGORITHM:
Step-1: Start.
Step-4: End.
SOURCE CODE:
num=int(input("Enter a number: "))
factorial = 1
if num < 0:
elif num == 0:
else:
factorial = factorial*i
~ 26 ~
OUTPUT:1
Enter a number: 7
OUTPUT:1
Enter a number: 9
DISCUSSION:
The numbers that are completely divisible by the given value (it means the remainder should
be 0) called as factors of a given number.
_________________________
TEACHER’SSIGNATURE
~ 27 ~
ASSIGNMENT: 11. DATE:
PROGRAMSTATEMENT: Write a program in python to check
whether a given number is prime or composite.
ALGORITHM:
Step-1: Start
Step-8: Loop
Step-10: End.
SOURCE CODE:
num=int(input("Enter any number : "))
if num>1:
if (num%i)==0:
break
else:
~ 28 ~
print(num,"is a PRIME number")
elif num==0 or 1:
else:
OUTPUT:1
Enter any number : 4
DISCUSSION:
A positive integer greater than 1 which has no other factors except 1 and the number itself is
called a prime number. In this program, variable num is checked if it’s prime or not in a range
between (50-100). Numbers less than or equal to 1 are not prime numbers.
___________________
TEACHER’S SIGNATURE
~ 29 ~
ASSIGNMENT: 12. DATE:
PROGRAMSTATEMENT: Write a program in python to calculate
the Prime factor of a given number.
ALGORITHM:
Step 1: Take the value of the integer and store in a variable.
Step 2: Using a while loop, first obtain the factors of the number.
Step 3: Using another while loop within the previous one, compute if the
factors are prime or not.
SOURCE CODE:
import math
def primefactors(n):
while n%2==0:
print (2),
n=n/2
for i in range(3,int(math.sqrt(n))+1,2):
while(n%i==0):
print(i)
n = n/i
if n>2:
print(n)
primefactors(n)
~ 30 ~
OUTPUT:1
Enter the number for calculating the prime factors =16
8.0
4.0
OUTPUT:2
Enter the number for calculating the prime factors =650
13
DISCUSSION: Prime factors are factors of a number that are, themselves, prime
numbers.. This program takes input from the user and computes the prime factors of the
integer.
__________________
TEACHER’S SIGNATURE
~ 31 ~
ASSIGNMENT: 13. DATE:
PROGRAM STATEMENT: Write a program in python to
check whether a given number is perfect number or not.
ALGORITHM :
Step 1: Take in an integer and store it in a variable.
Step 2:. Initialize a variable to count the sum of the proper divisors to 0.
Step 3: Use a for loop and an if statement to add the proper divisors of the
integer to the sum variable.
Step4: Check if the sum of the proper divisors of the number is equal to the
variable.
sum1 = 0
if(n % i == 0):
sum1 = sum1 + i
if (sum1 == n):
else:
~ 32 ~
OUTPUT:1
Enter any number: 6
OUTPUT:2
Enter any number: 15
DISCUSSION:
A perfect number n is defined as any positive integer where the sum of its divisors minus the
number itself equals the number. This program takes input from the user and checks
whether the number is perfect or not.
__________________
TEACHER’S SIGNATURE
~ 33 ~
ASSIGNMENT: 14. DATE:
PROGRAMSTATEMENT: Write a program in python to find
the reverse of a given number and also check the number is
palindrome or not.
ALGORITHM:
Step-1: Start
Step-2: Declare and initialize another variable reversed with 0, where reversed
an integer variable.
Step-3: Get the last digit of the given number by performing the modulo
division (%) and store the value in last_digit variable, likey last_digit= number %
10.
Step-6: Repeat the steps 3 to 5 till numbered is not equal to (or greater than)
zero.
Step-8: Stop
SOURCE CODE:
n=int(input("Please enter the number"))
t=n
rev=0
while(n>0):
d=n%10
~ 34 ~
rev=(rev*10)+d
n=n//10
if(t==rev):
else:
OUTPUT:1
Please enter the number121
OUTPUT:2
please enter the number145
not palindrome
DISCUSSION:
A palindromic number (also known as a numeral
palindrome or a numeric palindrome) is a number
(such as 16461) that remains the same when its digits
are reversed.
_________________
TEACHER'S SIGNATURE
~ 35 ~
ASSIGNMENT: 15. DATE:
PROGRAMSTATEMENT: Write a program in python to
calculate the Prime number between given range.
ALGORITHM:
Step-1: Start
Step-8: Loop
Step-10: End.
SOURCE CODE:
a=int(input("Please entr the lower limit"))
if num > 1:
if (num%i)==0:
~ 36 ~
break
else:
print(num)
OUTPUT:1
Prime numbers between 900 and 1000 are:
907
911
919
929
937
941
947
953
967
971
977
983
991
997
DISCUSSION:
A prime number is a natural number greater than 1 that is not a product of two smaller
natural numbers. To print the prime number between the two range was fully depended on
the first for loop for printing the range.
___________________
TEACHER’S SIGNATURE
~ 37 ~
ASSIGNMENT: 16. DATE:
PROGRAMSTATEMENT:Write a program that takes two
numbers and prints the LCM of two numbers.
ALGORITHM :
Step 1: Initially, Get 2 Integer Inputs from the user using int(input()).
Step 2: Find the greater number by using an If condition and assign it to the
variable 'max'
Step 3: Within the while loop, Use an If condition to check whether the
remainder of (max% a) and (max% b) equals to zero or not.
SOURCE CODE:
num1 = int(input("Enter first number:"))
max= num1
else:
max= num2
while(True):
break;
max= max+ 1
~ 38 ~
OUTPUT:
Enter first number: 54
DISCUSSION :
L.C.M of two numbers is the lowest number which is divisible by both the numbers. This
number takes two values from user and gives its L.C.M.
__________________
TEACHER’S SIGNATURE
~ 39 ~
ASSIGNMENT:17. DATE:
PROGRAM STATEMENT: Write a program that takes two
numbers and prints the GCD of two numbers.
ALGORITHM :
Step 1: Enter two numbers a and b
Step 2: a and b must be greater than 0 and not equal to each other
SOURCECODE:
a=int(input("Enter first number: "))
if (a == 0):
return b
if (b == 0):
return a
if (a == b):
return a
if (a > b):
return gcd(a-b, b)
if(gcd(a, b)):
~ 40 ~
print('GCD of', a, 'and', b, 'is', gcd(a, b))
else:
print('not found')
OUTPUT:
Enter first number: 98
GCD of 98 and 56 is 14
DISCUSSION: G.C.D is the Greatest Common Divisor of two numbers, i.e. the largest
number, which divides both of the given numbers. This program takes two numbers from
user and calculates its G.C.D.
__________________
TEACHER’S SIGNATURE
~ 41 ~
ASSIGNMENT: 18. DATE:
PROGRAMSTATEMENT: Write a program in python to find
the factorial of a given number.
ALGORITHM :
Step-1: Start.
Step-4: End.
SOURCE CODE:
num=int(input("Enter a number: "))
factorial = 1
if num < 0:
elif num == 0:
else:
factorial = factorial*i
~ 42 ~
OUTPUT:
Enter a number: 5
DISCUSSION:
The numbers that are completely divisible by the given
value (it means the remainder should be 0) called as
factors of a given number.
_________________________
TEACHER’SSIGNATURE
~ 43 ~
ASSIGNMENT: 19 . DATE:
ALGORITHM:
Step 1: Input the 'n' value until which the Fibonacci series has to be
generated
Step 7: sum = a + b
SOURCE CODE:
n = int(input("Enter the value of 'n': "))
a=0
b=1
sum = 0
count = 1
~ 44 ~
print("Fibonacci Series: ", end = " ")
count += 1
a=b
b = sum
sum = a + b
OUTPUT:
Enter the value of 'n': 5
Fibonacci Series: 0 1 1 2 3
DISCUSSION:
Fibonacci series is a series in which each number is the sum of the preceding two numbers.
By default, the first two numbers of a Fibonacci series are 0 and 1. This program takes input
from user and gives a single line of output containing the Fibonacci series.
________________
TEACHER'S SIGNATURE
~ 45 ~
ASSIGNMENT: 20. DATE:
PROGRAM STATEMENT: Write a program in python to check
the number is amstrong or not.
ALGORITHM:
Step 1: Start
•n=n/10
Step 7: Stop.
SOURCE CODE:
num = int(input("Enter a number: "))
sum = 0
temp = num
digit = temp % 10
sum += digit ** 3
temp //= 10
if num == sum:
~ 46 ~
print(num,"is an Armstrong number")
else:
__________________
TEACHER’S SIGNATURE
~ 47 ~
ASSIGNMENT: 21. DATE:
PROGRAM STATEMENT: Write a program in python to
calculate the perimeter in circle using math.pi
ALGORITHM:
Area of Circle: It is the area occupied by the circle. Given by the formula,
Area = π*R*R
Perimeter = 2*π*R
We will use these formulas for finding the area and perimeter of the circle with a given
radius. The value of π can either be used directly or can be extracted using the pi value in
the math library in Python.
SOURCE CODE:
PI = 3.14
area = (PI*R*R)
perimeter = (2*PI*R)
OUTPUT:
Enter radius of the circle:4
~ 48 ~
DISCUSSION
In the above code, we have initialized the value of PI with 3.14. Then asked the user for an
input for the value of R. Using the formulas, we calculated the value of area and perimeter
and printed it.
We can also get the value of PI using the built-in value in Python's math library.
__________________
TEACHER’S SIGNATURE
~ 49 ~
ASSIGNMENT: 22. DATE:
PROGRAM STATEMENT: Write a program in python to input
three positive integers a, b, and c , using Pythagorean triples formula (
a2+ b2 =c2.) to generate all Pythagorean triples in a certain range.
ALGORITHM:
Step 1. Take three numbers and store it in three variable.
triplets or not.
Step 3. If the numbers are Pythagorean triplets then print the numbers are
Pythagorean triplets.
Step 4. If the numbers are not Pythagorean triplets then print the numbers
SOURCE CODE:
a=int(input("enter 1st side:"))
else:
OUTPUT:1
enter 1st side:3
DISCUSSION:
In this program we check three numbers if they are Pythagorean
triplets or not .If the sum of the square of any two number is equal to
___________________
TEACHER’S SIGNATURE
~ 51 ~
ASSIGNMENT: 23. DATE:
PROGRAM STATEMENT: Write a program in python to check
the number is Krishnamurthy or not.
ALGORITHM:
Step 1. Start
5.4.1: f = f * i
Step8. Stop
SOURCE CODE:
a=int(input("Enter any number: "))
g=a
fact=1
~ 52 ~
sum=0
while(a>0):
r=a%10
fact=1
for i in range(1,r+1):
fact=fact*i
sum=sum+fact
a=a//10
if(sum==g):
else:
OUTPUT:1
Enter a Number145
OUTPUT:2
Enter a Number435
DISCUSSION
Krishnamurthy number is another special number in Java. A number is said to be
Krishnamurthy if the factorial sum of all its digits is equal to that number. Krishnamurthy
number is also referred to as a Strong number.
__________________
TEACHER’S SIGNATURE
~ 53 ~
ASSIGNMENT: 24. DATE:
PROGRAM STATEMENT: Write a program in python to print the
pattern of given below:
12345
1234
123
12
ALGORITHM:
Step 1: Take the number of rows(N) of mirrored right
Step 2: In any row, the sum of spaces and numbers are equal
To N. …
Step 3: In any row R, we will first print N-R-1 space l Characters then R+1
star characters.
Step 4: end.
SOURCE CODE:
n=int(input("enter the limit"))
for j in range(1,i-1):
print(j)
print( )
~ 54 ~
OUTPUT:
Enter the limit:6
12345
1234
123
12
DISCUSSION:
Star patterns are a series of * or any other character used to create some pattern or any
geometrical shape.
__________________
TEACHER’S SIGNATURE
~ 55 ~
ASSIGNMENT: 25. DATE:
PROGRAM STATEMENT: Write a program in python to print
the pattern of given below:
*****
****
***
**
*
ALGORITHM :
Step 1: Take the number of rows(N) of inverted right
Step 4: End.
SOURCE CODE:
n=int(input("enter the limit"))
for j in range(1,i-1):
print(j)
print( )
~ 56 ~
OUTPUT:
Enter the limit:5
*****
****
***
**
DISCUSSION:
Star patterns are a series of * or any other character used to create some pattern or any
geometrical shape.
__________________
TEACHER’S SIGNATUR
~ 57 ~
ASSIGNMENT: 26. DATE:
PROGRAM STATEMENT: Write a program in python to print
the pattern of given below:
54321
4321
321
21
1
SOURCECODE:
n = int(input('Enter number: '))
for i in range(n):
print(''.join(map(str,range(n-i,0,-1))))
OUTPUT:
Enter number: 5
54321
4321
321
21
DISCUSSION:
~ 58 ~
Pattern programs are patterns/designs/symbols consisting of
numbers, alphabets or symbols in a particular form. Pattern program
is the best way to check the your skill in loops.
_________________
TEACHER'S SIGNATURE
~ 59 ~
ASSIGNMENT: 27. DATE:
PROGRAM STATEMENT: Write a program in python to print
the floyed triangle.
ALGORITHM:
Step-1: Start.
Step-2: Create variables that hold rows and column values as i and j. Take a
number to display the rows as num and set the variable k to 1as its initial value.
Step-5:Increment k by 1 or k = k + 1.
Step-6: Jump to newline after each iteration of the inner for loop.
Step-7: Stop.
SOURCE CODE:
row=int(input("please Enter the total Number of row:"))
number=1
print("Floyd's Triangle")
for i in range(1,row+1):
for j in range(1,i+1):
print(number,end=' ')
number=number+1
print( )
~ 60 ~
OUTPUT:
please Enter the total Number of row:5
Floyd's Triangle
23
456
7 8 9 10
11 12 13 14 15
DISCUSSION:
The Floyd’s triangle is a right-angled triangle that contains consecutive natural numbers. In
Floyd’s triangle, the number starts with 1 in the top left corner, and then it consecutive filling
the defined rows through the numbers.
_________________
TEACHER'SSIGNATURE
~ 61 ~
ASSIGNMENT: 28. DATE:
PROGRAM STATEMENT: Write a program in python to print
the Pascal triangle.
ALGORITHM:
Step-1: Start.
Step-2:Declare variables i, j, n, c, z, s.
Step-4:Insert the first loop from i=0 to n,then a z loop to print the space .
Step-8: Stop.
SOURCE CODE:
n=int(input("Enter number of rows: "))
a=[]
for i in range(n):
a.append([])
a[i].append(1)
for j in range(1,i):
a[i].append(a[i-1][j-1]+a[i-1][j])
if(n!=0):
a[i].append(1)
~ 62 ~
for i in range(n):
for j in range(0,i+1):
print()
OUTPUT:
Input number of rows: 6
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
DISCUSSION:
All values outside the triangle are considered zero (0). The first row is 0 1 0 whereas only 1
acquire a space in pascal’s triangle, 0s are invisible. Second row is acquired by adding (0+1)
and (1+0). The output is sandwiched between two zeroes. The process continues till the
required level is achieved.
_________________
TEACHER'S SIGNATURE
~ 63 ~
ASSIGNMENT: 29. DATE:
PROGRAM STATEMENT: Write a program to take a string as
input and counts the number of vowels in a string.
ALGORITHM:
Step 1: Take a string from the user and store it in a variable.
Step 3: Use a for loop to traverse through the characters in the string.
SOURCE CODE:
str=input("Enter the string: ")
vowels=0
for i in str:
vowels=vowels+1
OUTPUT:
Enter the string: Computer Science
~ 64 ~
DISCUSSION:
In this program counts the number of vowels (not necessary different) in a given string.
_________________
TEACHER'S SIGNATURE
~ 65 ~
ASSIGNMENT: 30. DATE:
PROGRAM STATEMENT: Write a program to check whether
a string is a palindrome or not.
ALGORITHM:
Step-1: Start
Step-2: Declare and initialize another variable reversed with 0, where reversed an integer
variable.
Step-3: Get the last digit of the given number by performing the modulo division (%) and
store the value in last_digit variable, likey last_digit= number % 10.
Step-6: Repeat the steps 3 to 5 till numbered is not equal to (or greater than) zero.
Step-7: If the reversed number is equal to the original number then it is a palindrome
number otherwise not a palindrome number.
Step-8: Stop
SOURCE CODE:
num=int(input("Enter a number: "))
temp=num
rev=0
while (num>0):
dig=num%10
rev=rev*10+dig
num=num//10
if (temp==rev):
~ 66 ~
print ("The number is palindrome.")
else :
if (string==string[ : : -1 ]):
else :
OUTPUT:1
Enter an integer:454
Reversed number=454
OUTPUT:2
Enter an integer:607
Reversed number=706
DISCUSSION:
A palindromic number (also known as a numeral palindrome or a numeric palindrome) is a
number (such as 16461) that remains the same when its digits are reversed.
_________________
TEACHER'S SIGNATURE
~ 67 ~
ASSIGNMENT: 31. DATE:
PROGRAM STATEMENT: Write a program in python to
accept three digits and print all possible combination.
ALGORITHM:
Step- 1. Take in the first, second and third number and store it in separate
variables.
Step- 2. Then append all the three numbers to the list.
Step-3. Use three for loops and print the digits in the list if none of their
indexes are equal to each other.
Step- 4. Exit.
SOURCECODE:
a=int(input("Enter first number:"))
d=[]
d.append(a)
d.append(b)
d.append(c)
for i in range(0,3):
for j in range(0,3):
for k in range(0,3):
if(i!=j&j!=k&k!=i):
print(d[i],d[j],d[k])
~ 68 ~
OUTPUT:
Enter first number:12
12 45 10
12 10 45
45 12 10
45 10 12
10 12 45
10 45 12
DISCUSSION:
User must enter the first, second and third number. All the elements are appending into a
list for the ease of comparison. The for loops range from 0-2 which are basically the indexes
of the three elements in the list. If none of the indexes are equal to each other, the element
associated with the particular element in the list is printed.
_________________
TEACHER'SSIGNATURE
~ 69 ~
ASSIGNMENT: 32. DATE:
PROGRAM STATEMENT: Write a program in python to check
whether a number is Pythagorean triplets or not.
ALGORITHM:
Step 1. Take three numbers and store it in three variable.
triplets or not.
Step 3. If the numbers are Pythagorean triplets then print the numbers are
Pythagorean triplets.
Step 4. If the numbers are not Pythagorean triplets then print the numbers
SOURCE CODE:
a=int(input("enter 1st side:"))
else:
OUTPUT:1
enter 1st side:3
~ 70 ~
They are Pythagorean triplets
OUTPUT:2
enter 1st side:6
DISCUSSION:
In this program we check three numbers if they are Pythagorean
triplets or not .If the sum of the square of any two number is equal to
__________________
TEACHER’S SIGNATURE
~ 71 ~
ASSIGNMENT: 33. DATE:
PROGRAM STATEMENT: Write a program in python to take
three inputs from the user and print the 1st one in reverse and
the 2nd string converted to upper case and the 3rd string
converted to lower case.
ALGORITHM :
Step 1: Take three inputs from the user.
Step 3: 1st obtain the reverse of the 1st string using slice statement.
Step 4: Then convert the 2nd string into uppercase by taking string
variable.upper()
Step 5: Similarly convert the 3rd string to print string variable. Lower().
SOURCECODE:
a=input("Enter the 1st string: ")
d=a[::-1]
e=b.upper()
f=c.lower()
~ 72 ~
OUTPUT:
Enter the 1st string: Computer
DISCUSSION:
In this program we print three string into three different form in one program. We get the
String reverse using slice statement and converted string into uppercase and lower case.
__________________
TEACHER’S SIGNATURE
~ 73 ~
ASSIGNMENT: 34. DATE:
PROGRAM STATEMENT: Write a program in python to ask
user for two string and concatenate them.
ALGORITHM :
Step 1: Take two string from user and store them in two different variable.
SOURCECODE:
var1=input("enter the 1st string: ")
var3=var1+" "+var2
OUTPUT:
enter the 1st string: Hello
DISCUSSION:
In this program we concatenate or add two strings, given by the user.
__________________
TEACHER’S SIGNATURE
~ 74 ~
ASSIGNMENT: 35. DATE:
PROGRAM STATEMENT: Write a program in python to take
two strings as input from the user and print the 1st one twice
and the other one thrice.
ALGORITHM :
Step 1.Take two string from the user as input and store them to two
different variable.
Step 2. Then multiply the 1st variable with 2 and store it in a different
variable.
SOURCECODE:
var1=input("enter the 1st string: ")
var3=var1*2
var4=var2*3
print(var3)
print(var4)
OUTPUT:
enter the 1st string: Mango
MangoMango
~ 75 ~
BananaBananaBanana
DISCUSSION:
This program takes two string from user and print the first one twice and second one thrice.
__________________
TEACHER’S SIGNATURE
~ 76 ~
ASSIGNMENT: 36. DATE:
PROGRAM STATEMENT: Write a program in python to sort a
list in Bubble sort method.
ALGORITHM:
Step-1: Start
Step-3: compare the element with the next one (a[0] & a[1] (a is the name of
the array), and swap if a[0] > a[1]. ...
Step-4: Repeat step one but process array elements [0, n-2] because the last
one, i.e., a[n-1], is present at its correct position. ...
Step-5: Repeat this process n-1 times.
Step-6: Stop.
SOURCE CODE:
def bubbleSort(arr):
n = len(arr)
for i in range(n-1):
bubbleSort(arr)
for i in range(len(arr)):
~ 77 ~
OUTPUT:
enter the no of elements:
5
enter the element in array:
12
14
8
71
5
19
sorted array are:
5
8
12
14
71
19
DISCUSSION:
Bubble sort, sometimes referred to as sinking sort, is a
simple sorting algorithm that repeatedly steps through
the list, compares adjacent elements and swaps them if
they are in the wrong order. The pass through the list is
repeated until the list is sorted.
___________________
TEACHER’S SIGNATURE
~ 78 ~
ASSIGNMENT: 37. DATE:
PROGRAM STATEMENT: Write a program in python to sort a
list in Selection sort method.
ALGORITHM:
Step-1: Start
SOURCE CODE:
import sys
for i in range(len(A)):
min_idx = i
min_idx = j
for i in range(len(A)):
print("%d" %A[i]),
~ 79 ~
OUTPUT:
Enter number of elements in array5
12
48
14
78
Sorted Array:
5 12 14 48 78
DISCUSSION: The Selection sort algorithm is based on the idea of finding the
minimum or maximum element in an unsorted array and then putting it in its correct
position in a sorted array. Assume that the array A = [7, 5, 4, 2] needs to be sorted in
ascending order.
___________________
TEACHER’S SIGNATURE
~ 80 ~
ASSIGNMENT: 38. DATE:
PROGRAM STATEMENT: Write a program in python to Use
list comprehension to find all the odd numbers and numbers divisible
by 3 from a list of numbers.
ALGORITHM:
Step 1: Take two inputs of lower limit and upper limit.
Step 2: Use for loop to execute from lower limit to upper limit+1.
or not.
Step 5: Again use if statement and check whether the numbers are
divisible by 3 or not.
Step 6: If the remainder became zero then the numbers are divisible by 3.
SOURCE CODE:
lower=int(input("Enter the lower limit for the range:"))
for i in range(lower,upper+1):
if(i%2!=0):
print(i)
if(i%3==0):
print(i)
~ 81 ~
OUTPUT:
Enter the lower limit for the range:1
DISCUSSION:
This program determines the list of all odd numbers in a given range which are divisible by 3.
___________________
TEACHER’S SIGNATURE
~ 82 ~