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

Assignment 3 Solution

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

Assignment 3 Solution

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

Assignment 3 Solution

Enter 3 numbers.
Calculate and display sum, product and average of all numbers.

num1 = int(input('Enter 1st number: '))


num2 = int(input('Enter 2nd number: '))
num3 = int(input('Enter 3rd number: '))

sumOfNumbers = num1 + num2 + num3


productOfNumbers = num1 * num2 * num3
averageOfNumbers = (num1 + num2 + num3)/3

print('3 numbers are: ', num1, num2, num3)


print('Sum: ', sumOfNumbers)
print('Product: ', productOfNumbers)
print('Average: ', averageOfNumbers)

Output:

Enter 1st number: 20


Enter 2nd number: 5
Enter 3rd number: 3
3 numbers are: 20 5 3
Sum: 28
Product: 300
Average: 9.333333333333334

Enter values of a,b and c.


Calulate the given expression:- abc + 2a - 2b * 3c

a = int(input('Enter 1st number: '))


b = int(input('Enter 2nd number: '))
c = int(input('Enter 3rd number: '))

expression = (a*b*c) + (2*a) - (2*b) * (3*c)

print('3 entered numbers are: ', a,b,c)


print('Expression: ', expression)

Output:

Enter 1st number: 7


Enter 2nd number: 2
Enter 3rd number: 9
3 entered numbers are: 7 2 9
Expression: 32

Enter values of a,b and c. (Use:- map(),input(),split() functions)


Add 5 into a.
Subtract 2 from b.
Divide 20 by c.
Use augmented assignment operator.

a, b, c = map(int, input('Enter 3 numbers: ').split())

print('3 entered numbers are: ', a,b,c)


a += 5
b -= 2
c /= 20
print('Modified values of variables: ', a,b,c)

Output:

Enter 3 numbers: 13 9 30
3 entered numbers are: 13 9 30
Modified values of variables: 18 7 1.5

Enter 2 numbers. (Use:- map(),input(),split() functions)

Calculate and display the quotient and reminder of the division.


Use appropriate operators.

a, b = map(int, input('Enter 2 numbers: ').split())

Quotient = a/b
Reminder = a%b
print('Quotient: ', Quotient)
print('Reminder: ', Reminder)

Output:

Enter 2 numbers: 17 4
Quotient: 4.25
Reminder: 1

Display the values and datatype of the above variables quotient and reminder.

print(f'Value: {Quotient} Datatype: {type(Quotient)}')


print(f'Value: {Reminder} Datatype: {type(Reminder)}')

Output:

Value: 4.25 Datatype: <class 'float'>


Value: 1 Datatype: <class 'int'>

Enter a number.
Display cube of the enterend number.
Divide the entered number by 5 and display the quotient as an integer.

import math

num = int(input('Enter a number: '))


cube = pow(num, 3)
quotient = num//5 # quotient as integer

print(f'Cube = {cube} quotient = {quotient}')

Output:

Enter a number: 4
Cube = 64 quotient = 0

Create a list having multiple items.


Display 1st, 2nd item from the list.
Display 2nd last and last item from the list.

list1 = ['apple','blueberry','mango','orange','lemon']

print('1st item: ', list1[0])


print('2nd item: ', list1[1])
print('2nd last item: ', list1[-2])
print('last item: ', list1[-1])

Output:

1st item: apple


2nd item: blueberry
2nd last item: orange
last item: lemon

Do the given operations on the above list:-


Find if 57 is not present in the list
Find if 40 is present in the list

print(57 not in list1)


print(40 in list1)

Output:

True
False

Create two variables


Find if the address of both variables are same

a = 10
b = 18

id(a) == id(b)

Output:

False
Enter two number in a and b. (Use:- map(),input(),split() functions)
Find factorial of a and b.
Also display addition of factorial of a and b.

import math

n1, n2 = map(int, input('Enter 2 numbers: ').split())

fact1 = math.factorial(n1)
fact2 = math.factorial(n2)
add = fact1 + fact2

print(f'1st Number: {n1} Factorial: {fact1}')


print(f'2nd Number: {n2} Factorial: {fact2}')
print(f'Addition : {add}')

Output:

Enter 2 numbers: 4 2
1st Number: 4 Factorial: 24
2nd Number: 2 Factorial: 2
Addition : 26

Write statements for the following:-


Find a random number between 45 and 60
Find a random number between 0 and 1
Find a random number between 70 and 150

import random

print(random.randint(45, 60))
print(random.random())
print(random.randint(70, 150))

Output:

53
0.5586154456049517
137

Create a tuple having names of planets.


Display any random planet from the tuple.

import random

planetTuple = ('Mercury', 'Venus', 'Earth','Mars','Jupiter', 'Saturn', 'Uranus',


'Neptune')

print(random.choice(planetTuple))

Output:
Neptune

Create a dictionary having student names and favourite subjects.


Display datatype of the dictionary variable.

studentDict = {'Ramesh' : 'Math',


'Mili' : 'Coding',
'John' : 'Reading'}

print(type(studentDict))

Output:

<class 'dict'>

Delete the above dictionary

del studentDict

Output:

Write statements for the following:-


Create a tuple having one item
Create a list having multiple elements
Convert the above list into a set and display
Create an empty set and display it's datatype

mytuple = ('apple',)
mylist = [10, 32, 30, 77, 9, 15]
myset1 = set(mylist)
print(myset1)
myset2 = set()
print(type(myset2))

Output:

{32, 9, 10, 77, 15, 30}


<class 'set'>

You might also like