Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Ch-3- Advance Python (Answer)

Uploaded by

julirkl123
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)
2 views

Ch-3- Advance Python (Answer)

Uploaded by

julirkl123
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
You are on page 1/ 3

CH – 3: ADVANCED PYTHON

ASSIGNMENT – 1
TITLE - CONTROL STATEMENTS IN PYTHON
-------------------------------------------------------------------------------------------------------------------------

Write Python codes for the following questions along with the output.
Q1. Write a Python program that takes three numbers as input and then calculates and prints both their
sum and average.

Ans.

num1 = float(input("Enter the first number: "))

num2 = float(input("Enter the second number: "))

num3 = float(input("Enter the third number: "))

sum = num1 + num2 + num3

avg = sum / 3

print("The sum of three numbers =", sum)

print("The average =", avg)

OUTPUT

Enter the first number: 6.2

Enter the second number: 7.3

Enter the third number: 8.0

The sum of three numbers = 21.5

The average = 7.166666666666667

Q2. Write a Python program that accepts the length and breadth of a rectangle in centimetres and
calculates and prints both its area and perimeter.

l = float(input("Enter the length of a rectangle: ")) OUTPUT

b = float(input("Enter the breadth of a rectangle: ")) Enter the length of a rectangle: 10

area = l * b Enter the breadth of a rectangle: 8

perimeter = 2 * (l + b) Area of a rectangle= 80.0 sq cm

print("Area of a rectangle=", area,'sq cm') Perimeter of a rectangle is = 36.0 cm

print ("Perimeter of a rectangle is =",perimeter, 'cm')


Q3. WAP to read two lists and concatenate or join them.

list1 = input("Enter 1st list items = ") OUTPUT

list2 = input("Enter 2nd list items = ") Enter 1st list items = [34,67,90]

list3 = list1 + list2 Enter 2nd list items = ['A','B','C']

print("The joined list = ", list3) The joined list = [34,67,90]['A','B','C']

Q4. WAP to display the first three elements of a list. Assuming the list is [56,80,43,7.8,5.3,'M','H','KTS']

my_list = [56,80,43,7.8,5.3,'M','H','KTS'] OUTPUT

print(my_list[0:3]) [56, 80, 43]

Explanation: Get the elements from 0st Index to


2nd index.

That is, we get the elements from the first


position (Index 0) to 3rd position (Index 2).
Index 3 is not included.

Q5. Create a program that takes the cost price and the selling price of an item, checks whether a profit
was gained or a loss was incurred, and then prints the amount of profit or loss. Provide the output for at
least two sample cases.

cp = float(input('Enter cost price = ')) OUTPUT – 1

sp = float(input('Enter selling price = ')) Enter cost price = 40

if (cp > sp): Enter selling price = 35

print('Loss incurred') Loss incurred

loss = cp - sp Amount of loss incurred = Rs. 5.0

print("Amount of loss incurred = ",'Rs.',loss) OUTPUT – 2

else: Enter cost price = 50

print('profit gained') Enter selling price = 80

profit = sp - cp profit gained

print("Amount of profit gained = ",'Rs.',profit) Amount of profit gained = Rs. 30.0


Q6. Create a program (WAP) that checks whether a number entered is negative, positive, or zero, and
provide the output for at least three sample cases.

num = int(input('Enter a number = ')) OUTPUT – 1 OUTPUT – 2 OUTPUT – 3

if num > 0: Enter a number = 9 Enter a number = 0 Enter a number = -5

print('It is a positive number') It is a positive The number is zero It is a negative


number number
elif num == 0:

print('The number is zero')

else:

print('It is a negative number')

Q7. Write a program that calculates and prints the sum of the first ten natural numbers.

sum = 0 OUTPUT

i=1 The sum is: 55

while i <= 10:

sum = sum + i

i = i+1

print("The sum is:", sum)

Q8. Create a program (WAP) that prints each character of a given string in reverse order. Assume the input
string is "INDIA."

x = "INDIA" OUTPUT

for i in reversed(x): # ‘i’ is the Loop counter A

print(i) I

You might also like