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

Python Assignment - 0744

The document contains 22 coding assignments completed by Sanjana Kapoor, a B.tech(CSE) second semester student with roll number 17032100744. The assignments cover topics like searching and sorting lists, matrix operations, recursion, loops, and other basic Python programming concepts. For each assignment, the code and output are provided.

Uploaded by

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

Python Assignment - 0744

The document contains 22 coding assignments completed by Sanjana Kapoor, a B.tech(CSE) second semester student with roll number 17032100744. The assignments cover topics like searching and sorting lists, matrix operations, recursion, loops, and other basic Python programming concepts. For each assignment, the code and output are provided.

Uploaded by

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

Sanjana Kapoor

B.tech(CSE) semester 2nd (sec B)


Roll no. 17032100744

Python Assignment
Ans.1 # To search a number from the list
s=True
while(s==True):
print("Lets make a list")
a = int(input("Enter 1st number"))
b = int(input("Enter 2nd number"))
c = int(input("Enter 3rd number"))
d = int(input("Enter 4th number"))
x = int(input("Enter the number you want to search"))
y = [a, b, c, d]
def find(x):
if x in y:
print('Yes, the number is in the given list')
else:
print("The number is not in the list try again")
find(x)
ans=int(input("Enter 1 to continue, 0 to exit"))
if ans==1:
s=True
print("continue")
print(" ")
else:
s=False
print("Exit")

Output:
Lets make a list

Enter 1st number 4

Enter 2nd number 5

Enter 3rd number 6

Enter 4th number 7

Enter the number you want to search 7

Yes, the number is in the given list

Enter 1 to continue, 0 to exit0

Exit

Ans.2 #To sort the list of elements in descending order


'''TO SORT LIST OF ELEMENTS IN DESCENDING ORDER'''
list = [10,30,90,40,20,60,80,70,50,100]

print("The Original list is: ")


print(list)
print()

sorted_list = sorted(list)
print("The Sorted list in ASCENDING ORDER is: ")
print(sorted_list)
print()

reverse_list = sorted(list , reverse = True)


print("The Sorted list in DESCENDING ORDER is: ")
print(reverse_list)

Output:
The Original list is:

[10, 30, 90, 40, 20, 60, 80, 70, 50, 100]

The Sorted list in ASCENDING ORDER is:

[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]

The Sorted list in DESCENDING ORDER is:

[100, 90, 80, 70, 60, 50, 40, 30, 20, 10]

Ans.3 #To concatenate two lists


print("The first list is :")
list1=['1','2','3','4','5','6','7']
print(list1)
print( )
print("The second list is:")
list2=['RM','Jin','Suga','Jhope','Jimin','V','Jungkook']
print(list2)
print( )
print("The concatenation of two lists is:")
print(list1+list2)

Output:
The first list is :

['1', '2', '3', '4', '5', '6', '7']

The second list is:


['RM', 'Jin', 'Suga', 'Jhope', 'Jimin', 'V', 'Jungkook']

The concatenation of two lists is:

['1', '2', '3', '4', '5', '6', '7', 'RM', 'Jin', 'Suga', 'Jhope', 'Jimin', 'V', 'Jungkook']

Ans.4 # Sum of two matrices


M1=[[1,2,3],
[3,4,6],
[7,8,9]]
M2=[[2,0,4],
[0,5,1],
[1,2,3]]
Result=[[0,0,0],
[0,0,0],
[0,0,0]]
for i in range(len(M1)):
for j in range(len(M2)):
Result[i][j]=M1[i][j]+M2[i][j]
print("The first matrix is :")
print(M1)
print("The second matrix is:")
print(M2)
print("The sum of two matrices is:")

for r in Result:
print(r)

Output:
The first matrix is :

[[1, 2, 3], [3, 4, 6], [7, 8, 9]]

The second matrix is:

[[2, 0, 4], [0, 5, 1], [1, 2, 3]]

The sum of two matrices is:

[3, 2, 7]

[3, 9, 7]

[8, 10, 12]

Ans.5 # To multiply two matrices


M1=[[1,2,3],
[3,4,6],
[7,8,9]]
M2=[[2,0,4],
[0,5,1],
[1,2,3]]
Result=[[0,0,0],
[0,0,0],
[0,0,0]]
for i in range(len(M1)):
for j in range(len(M2)):
Result[i][j]=M1[i][j]*M2[i][j]
print("The first matrix is :")
print(M1)
print("The second matrix is:")
print(M2)
print("The product of two matrices is:")

for r in Result:
print(r)

Output:
The first matrix is :

[[1, 2, 3], [3, 4, 6], [7, 8, 9]]

The second matrix is:

[[2, 0, 4], [0, 5, 1], [1, 2, 3]]

The product of two matrices is:

[2, 0, 12]

[0, 20, 6]

[7, 16, 27]

Ans.6 # To find the sum of the diagonal elements of the


matrix
a=[[1,2,3],[4,0,7],[5,4,3]]
sum=0
print("Diagonal elements of the matrix are :")
for i in range(len(a)):
for j in range(len(a[i])):
if (i==j):
sum=sum+a[i][j]
print(a[i][j])
print("The sum of diagonal elements are")
print(sum)

Output:
Diagonal elements of the matrix are :

3
The sum of diagonal elements are

Ans.7 # To find the sum of elements of a matrix


a=[[1,2,3],[4,0,7],[5,4,3]]
sum=0
print(" Elements of the matrix are :")
for i in range(len(a)):
for j in range(len(a[i])):
if (i==j or i>j or i<j):
sum=sum+a[i][j]
print(a[i][j])
print("The sum of elements are")
print(sum)

Output:
Elements of the matrix are :

The sum of elements are

29

Ans.8 # To calculate the factorial of a number using recursion


def factorial_recursive(n):
"""

:param n: Integer
:return: n * n-1 * n-2 * n-3*.....1
"""
if n==1:
return 1
else:
return n*factorial_recursive(n-1)

flag=True
while(flag==True):
number = int(input("Enter the number"))
print("Factorial using recursive method", factorial_recursive(number))
ans = int(input("Enter(1 to continue) OR (Enter 0 to exit):"))
if (ans == 1):
flag = True
print("continue")
print("")
else:
flag = False
print("Exit")

Output:
Enter the number 5

Factorial using recursive method 120

Enter(1 to continue) OR (Enter 0 to exit):0

Exit

Ans.9 # To find the minimum of two numbers


a=int(input("Enter first number"))
b=int(input("Enter second number"))
if a>b:
print("Second number is smaller than first number" )

elif b>a:
print("First number is smaller than second number")

else:
print("inavalid input")

Output:
Enter first number 5

Enter second number 4

Second number is smaller than first number

Ans.10 # To find the area of a rectangle


def area(x,y):
return x*y

x=int(input("Enter the length of the rectangle"))


y=int(input("Enter the breadth of the rectangle"))
print("The area of the rectangle is",area(x,y))

Output:
Enter the length of the rectangle 6

Enter the breadth of the rectangle 5


The area rectangle is 30

Ans.11 # To find the square and cube of the number


def square(x):
return x*x
def cube(x):
return x*x*x

x=(int(input("Enter the number you want to find the square and cube")))
print("The square of the number is ",square(x))
print("The cube of the number is",cube(x))

Output:
Enter the number you want to find the square and cube 5

The number is 25

The cube of the number is 125

Ans.12 # To convert the temperature


def temperature(x):
y= x*1.8 + 32
return y
x=float(input("Enter the temperature in celsius"))
print("The temperature in fahrenheit is ",temperature(x))

Output:
Enter the temperature in celsius 50

The temperature in fahrenheit is 122.0

Ans.13 # To check whether the number is divisible by 5 or 10


or 5 and 10
a=int(input("Enter your number"))
if a%10==0 :
print("The number is divisible by both 5 and 10")
elif (a%5==0 and a%10!=0):
print("The number is divisible by 5 but not by 10")
else:
print("The number is neither divisible by 5 nor by 10")

Output:
Enter your number 60

The number is divisible by both 5 and 10


Ans.14 # To find if the first number is smaller or greater than
the other two

num1=int(input("Enter first number"))


num2=int(input("Enter second number"))
num3=int(input("Enter third number"))

if num1>num2 and num1>num3 :


print("First number is largest of three numbers")
elif num1<num2 and num1<num3 :
print("First number is smallest of three numbers")
elif num1>num2 and num1<num3 :
print("First number is greater than second but smaller than third
number")
elif num1<num2 and num1>num3:
print("First number is greater than third but smaller than second
number")
else:
print("invalid input")

Output:
Enter first number 5

Enter second number 6

Enter third number 7

First number is smallest of three numbers

Ans.15 # To find the name of the day


x=int(input("Enter the day of the week"))
if x==1:
print("The name of the day is Monday")
elif x==2:
print("The name of the day is Tuesday")
elif x==3:
print("The name of the day is Wednesday")
elif x==4:
print("The name of the day is Thursday")
elif x==5:
print("The name of the day is Friday")
elif x==6:
print("The name of the day is Saturday")
elif x==7:
print("The name of the day is Sunday")
else:
print("invalid input")

Output:
Enter the day of the week 5
The name of the day is Friday

Ans.16 # To find the no. of days in a month


#To find the number of days in a month
list1=[1,3,5,7,8,10,12]
list2=[4,6,9,11]
list3=[2]
print("Lets find the number of days in a month")
print("To find use 1 for January,2 for february and so on...")

a=int(input("Enter the number"))


if a in list1:
print("This month contains 31 days")
elif a in list2:
print("This month contains 30 days")
elif a in list3:
print("This month usually has 28 days but in a leap year it has 29")
else: print("Invalid input")

Output:
Lets find the number of days in a month

To find use 1 for January,2 for february and so on...

Enter the number 7

This month contains 31 days

Ans.17 # To print the no.s from 1 to 5 using loop


i=0
while(i<5):
i=i+1
print(i)

Output:
1

Ans.18 # To add 10 consecutive no.s using loop


sum=0
count=0
while(count<11):
sum=sum+count
count=count+1
print(sum)

Output:
55
Ans.19 # To find the sum of digits of a number
n=int(input("Enter the number"))
sum = 0
while(n > 0):
digit = n%10
sum = sum + digit
n =n//10

print(sum)

Output:
Enter the number 234

Ans.20 # To reverse the digits of a number


n=int(input("Enter your number"))
b=str(n)
print(b[::-1])

Output:
Enter your number 456

654

Ans.21 # To find the sum of the no.s divisible by 5 between 1


to 20
i=0
sum = 0

while (i < 21):


sum=sum+i
i=i+5

print(sum)

Output:
50

Ans.22 # To find the factorial of a number using recursion


and loop
def factorial_recursive(n):
"""

:param n: Integer
:return: n * n-1 * n-2 * n-3*.....1
"""
if n==1:
return 1
else:
return n*factorial_recursive(n-1)

flag=True
while(flag==True):
number = int(input("enter the number"))
print("Factorial using recursive method", factorial_recursive(number))
ans = int(input("Enter(1 to continue) OR (Enter 0 to exit):"))
if (ans == 1):
flag = True
print("continue")
print("")
else:
flag = False
print("Exit")

Output:
enter the number 7

Factorial using recursive method 5040

Enter(1 to continue) OR (Enter 0 to exit):0

Exit

Ans.23 # To check whether a number is an Armstrong


number or not
n=int(input("Enter the number\n"))
sum=0
order=len(str(n))
copy_n=n
while(n>0):
digit=n%10
sum += digit**order
n=n//10

if(sum==copy_n):
print(f"{copy_n} is an armstrong number")
else:
print(f"{copy_n} is not an armstrong number")

Output:
Enter the number

153

153 is an armstrong number

Ans.24 # To print the numbers from 1 to 10 in descending


order
y=[10,9,8,7,6,5,4,3,2,1]
for x in y:
print(x)
Output:
10

Ans.25 # To find the square of first five natural numbers


def square(x):
return x*x

i=0
while(i<5):
i=i+1
print(square(i))

Output:
1

16

25

Ans.26 # To print the sum of even no.s from 1 to 10


i=0
sum=0
while(i<11):
sum=sum+i
i=i+2

print(sum)

Output:
30

Ans.27 # To calculate the sum of the numbers from 1 to 20


which are not divisible by 2,3, or 5.
list1=[1,7,11,13,17,19]

sum=0
for i in list1:
sum=sum+i

print(sum)
Output:
68

Ans.28 # To display the pattern of stars given as follows:


*****
****
***
**
*
print("Pattern printing")
num = int(input("Enter how many rows you want to print : \n"))
print("Enter 1 or 0")
bool_val = input("1 for True value or 0 for False: \n")
if bool_val=="1":
for i in range(0,num+1):
print("*"*i)

if bool_val=="0":
for i in range(num,0,-1):
print("*"*i)
Output:
Pattern printing
Enter how many rows you want to print :
5
Enter 1 or 0
1 for True value or 0 for False:
0
*****
****
***
**
*

Ans.29 # To check whether a number entered is prime or


not. (use break)
n = int(input("Enter the number: "))
flag = False
if ( n > 1):
for i in range (2 , n):
if (n % i == 0):
flag = True
break

if (flag):
print("The number", n, "is not a PRIME Number")
else:
print("The number", n, "is a PRIME Number")
Output:
Enter the number: 7

The number 7 is a PRIME Number

Ans.30 # To find the factorial using recursion


def factorial_recursive(n):
"""

:param n: Integer
:return: n * n-1 * n-2 * n-3*.....1
"""
if n==1:
return 1
else:
return n*factorial_recursive(n-1)

flag=True
while(flag==True):
number = int(input("enter the number"))
print("Factorial using recursive method", factorial_recursive(number))
ans = int(input("Enter(1 to continue) OR (Enter 0 to exit):"))
if (ans == 1):
flag = True
print("continue")
print("")
else:
flag = False
print("Exit")

Output:
enter the number 4

Factorial using recursive method 24

Enter(1 to continue) OR (Enter 0 to exit):0

Exit

You might also like