Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
4 views7 pages

Assignment - 2

The document is a Python problem set containing various exercises such as calculating the area of a triangle, summing even numbers, identifying valid and invalid identifiers, and creating a simple interest calculator. It includes algorithms, code snippets, and comments explaining the logic behind each exercise. Additionally, it covers user input handling and basic mathematical operations through a calculator program.

Uploaded by

basmah.basheer
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)
4 views7 pages

Assignment - 2

The document is a Python problem set containing various exercises such as calculating the area of a triangle, summing even numbers, identifying valid and invalid identifiers, and creating a simple interest calculator. It includes algorithms, code snippets, and comments explaining the logic behind each exercise. Additionally, it covers user input handling and basic mathematical operations through a calculator program.

Uploaded by

basmah.basheer
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/ 7

Asmitha_Basic_Python_Problem_Set.ipynb - Colab https://colab.research.google.com/drive/1J3cUrR_2AJ2TkP...

Write an algorithm to �nd the area of a triangle.

# Enter the algorithm as comments below


# step 1: Give the base b and hight h.
# step 2: Calculate area using the formula 1/2*b*h
# step 3: Display calculated area.

def area_triangle(b, h):


return 0.5*b*h

area_triangle(b=5, h=8)

20.0

Write an algorithm to �nd the sum of all even number up to given number.

# Enter the algorithm as comments below


# step 1: give an input number as n
# step 2: initialize a variable sum_even to 0 to store the sum of even numbers
# step 3: loop through all numbers from 2 to n and check if the number is even. If it'
# step 4: display the calculated sum

def sum_even(num):

# 1)
retval = sum( range( 0, num+1, 2 ) )

# 2)
# retval = sum([ x for x in range(num+1) if x % 2 == 0 ])

return retval

num = 10
sum_even(num)

30

Which of the following identi�er names are invalid and why?

1. Serial_no.
2. 1st_Room
3. total-Marks
4. _Percentage
5. Total Marks
�. True

# Write the answer as comments

1 of 7 8/1/25, 15:29
Asmitha_Basic_Python_Problem_Set.ipynb - Colab https://colab.research.google.com/drive/1J3cUrR_2AJ2TkP...

# Write the answer as comments


"""
1)invalid due to presence of .
2)invalid (begins with a digit)
3)invalid (- is not used in identifiers)
4)valid
5)invalid (spaces are not allowed)
6)invalid(True is a keyword)
"""

Write the corresponding Python assignment statements:

1. Assign 10 to variable length and 20 to variable breadth.


2. Assign the average of values of variables length and breadth to a variable sum.
3. Assign the strings ‘Mohandas’, ‘Karamchand’, and ‘Gandhi’ to variables �rst, middle and
last.

# Write the code for question 1 below


length=10
breadth=20

# Write the code for question 2 below


sum=(length+breadth)/2

# Write the code for question 3 below


first='Mohandas'
middle='Karamchand'
last='Gandhi'

Which data type will be used to represent the following data values and why?

1. Number of months in a year


2. Resident of Delhi or not
3. Mobile number
4. Perimeter of a square
5. Name of the student
�. Address of the student

# Write the answer as comments


"""
1) integer
2) bool
3) integer
4) float
5) string
6) string
"""

Give the output of the following when num1 = 4, num2 = 3, num3 = 2

2 of 7 8/1/25, 15:29
Asmitha_Basic_Python_Problem_Set.ipynb - Colab https://colab.research.google.com/drive/1J3cUrR_2AJ2TkP...

Give the output of the following when num1 = 4, num2 = 3, num3 = 2

1. num1 += num2 + num3;


print (num1)
2. num1 = num1 ** (num2 + num3)
print (num1)
3. num1 **= num2 + c
print (num1)
4. num1 = 2+9*((3*12)-8)/10
print(num1)
5. num1 = �oat(10)
print (num1)
�. print(10 != 9 and 20 >= 20)
7. print(5 % 10 + 10 < 50 and 29 <= 29)

# Write the answer as comments


"""
1) 9
2) 1024
3) name error
4) 27.2
5) 10.0
6) True
7) True
"""

Write a Python program to calculate the amount payable if money has been lent on
simple interest. Principal or money lent = P, Rate = R% per annum and Time = T years. Then
Simple Interest (SI) = (P x R x T)/ 100. Amount payable = Principal + SI. P, R and T are given
as input to the program.

# Write the code below


# Take input for Principal (P), Rate of interest (R), and Time (T)
P = float(input("Principal amount (P): "))
R = float(input("Rate of interest (R) in percentage: "))
T = float(input("Time (T) in years: "))

# Calculate Simple Interest (SI)


SI = (P * R * T) / 100

# Calculate the amount payable (Principal + SI)


amount_payable = P + SI

# Display the results


print(f"The total amount payable is: {amount_payable}")

Principal amount (P): 1000

3 of 7 8/1/25, 15:29
Asmitha_Basic_Python_Problem_Set.ipynb - Colab https://colab.research.google.com/drive/1J3cUrR_2AJ2TkP...

Principal amount (P): 1000


Rate of interest (R) in percentage: 5
Time (T) in years: 5
The total amount payable is: 1250.0

Write a program to repeat the string ‘‘GOOD MORNING” n times. Here n is an integer
entered by the user.

# TakE input for the number of repetitions


n = int(input("Number of times to repeat 'GOOD MORNING': "))

# Repeat the string "GOOD MORNING" n times


for i in range(n):
print("GOOD MORNING")

Number of times to repeat 'GOOD MORNING': 4


GOOD MORNING
GOOD MORNING
GOOD MORNING
GOOD MORNING

Write a program to �nd the average of 3 numbers.

# Take input for three numbers


X = float(input("X: "))
Y = float(input("Y: "))
Z = float(input("Z: "))

# Calculate Average
AV = (X+Y+Z) / 3

# Display the results


print(f"Average is: {AV}")

X: 10
Y: 5
Z: 12
Average is: 9.0

Write a program that asks the user to enter one's name and age. Print out a message
addressed to the user that tells the user the year in which he/she will turn 100 years old.

# TakE input for name and age


name = input("Name: ")
age = int(input("Age: "))

#current year

4 of 7 8/1/25, 15:29
Asmitha_Basic_Python_Problem_Set.ipynb - Colab https://colab.research.google.com/drive/1J3cUrR_2AJ2TkP...

#current year
current_year = int(input("Current year: "))

# Calculate the year when the user will turn 100


year_turn_100 = current_year + (100 - age)

# Print the message


print(f"Hello {name}! You will turn 100 years old in the year {year_turn_100}."

Name: Asmitha
Age: 25
Current year: 2025
Hello Asmitha! You will turn 100 years old in the year 2100.

Write program for a calulater with user di�ned functions for all the mathematical
operations.

# Write the code below

import math

# Define functions for operations


def add(x, y): return x + y
def subtract(x, y): return x - y
def multiply(x, y): return x * y
def divide(x, y): return x / y if y != 0 else "Error! Division by zero."
def modulus(x, y): return x % y if y != 0 else "Error! Modulo by zero."
def power(x, y): return x ** y
def square_root(x): return math.sqrt(x) if x >= 0 else "Error! Cannot take square root

# Map operation codes to functions


operations = {
'1': add,
'2': subtract,
'3': multiply,
'4': divide,
'5': modulus,
'6': power,
'7': square_root
}

# calculation
def calculator():
while True:
print("\nSelect operation:")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")
print("5. Modulus")
print("6. Power")
print("7. Square Root")
print("8. Exit")

5 of 7 8/1/25, 15:29
Asmitha_Basic_Python_Problem_Set.ipynb - Colab https://colab.research.google.com/drive/1J3cUrR_2AJ2TkP...

print("8. Exit")

choice = input("Enter choice (1/2/3/4/5/6/7/8): ")

if choice == '8':
print("Exiting the calculator.")
break

if choice in operations:
try:
if choice == '7':
num1 = float(input("Enter number: "))
print(f"Result: {operations[choice](num1)}")
else:
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
print(f"Result: {operations[choice](num1, num2)}")
except ValueError:
print("Invalid input! Please enter numeric values.")
else:
print("Invalid choice! Please select a valid option.")

# Run the calculator


calculator()

Select operation:
1. Add
2. Subtract
3. Multiply
4. Divide
5. Modulus
6. Power
7. Square Root
8. Exit
Enter choice (1/2/3/4/5/6/7/8): 7
Enter number: 100
Result: 10.0

Select operation:
1. Add
2. Subtract
3. Multiply
4. Divide
5. Modulus
6. Power
7. Square Root
8. Exit
Enter choice (1/2/3/4/5/6/7/8): 6
Enter first number: 5
Enter second number: 7
Result: 78125.0

Select operation:
1. Add
2. Subtract
3. Multiply
4. Divide

6 of 7 8/1/25, 15:29
Asmitha_Basic_Python_Problem_Set.ipynb - Colab https://colab.research.google.com/drive/1J3cUrR_2AJ2TkP...

4. Divide
5. Modulus
6. Power
7. Square Root
8. Exit
Enter choice (1/2/3/4/5/6/7/8): 8
Exiting the calculator.

Start coding or generate with AI.

7 of 7 8/1/25, 15:29

You might also like