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

Python Class9

The document contains a series of Python programming exercises that cover various topics such as calculating simple interest, finding averages, determining the largest number, checking leap years, and manipulating lists. Each exercise includes a function definition, a main program loop, and sample outputs demonstrating the functionality. The exercises aim to enhance programming skills through practical applications.

Uploaded by

harshitshaw2007
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)
12 views

Python Class9

The document contains a series of Python programming exercises that cover various topics such as calculating simple interest, finding averages, determining the largest number, checking leap years, and manipulating lists. Each exercise includes a function definition, a main program loop, and sample outputs demonstrating the functionality. The exercises aim to enhance programming skills through practical applications.

Uploaded by

harshitshaw2007
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/ 15

Q1> WAP to find simple interest

#function for finging si

def si(p,r,t):

si=p*r*t/100

print("principle = ",p,";rate of interest = ",r,";time = ",t)

print("The simple interest(SI) of the principle is",si)

#main program

print("Welcome to my program")

while True:

p=float(input("enter the principle"))

r=float(input("enter the rate of interest"))

t=float(input("enter the time period(in years)"))

si(p,r,t)

Output:
Welcome to my program

enter the principle24

enter the rate of interest6

enter the time period(in years)3

principle = 24.0 ;rate of interest = 6.0 ;time = , 3.0

The simple interest(SI) of the principle is 4.32

enter the principle


Q2> WAP to accept 5 subject marks and calculate its total and average
# Function to calculate the average

def avg(eng, math, hindi, sst, sc):

total = eng + math + hindi + sst + sc

avg = total / 5

print(f"Your marks: English={eng}, Maths={math}, Hindi={hindi}, SST={sst}, Science={sc}")

print(f"Average of your marks is: {avg}")

# Main program loop

while True:

# Get the marks

eng = float(input("Enter the marks of English: "))

math = float(input("Enter the marks of Maths: "))

hindi = float(input("Enter the marks of Hindi: "))

sst = float(input("Enter the marks of SST: "))

sc = float(input("Enter the marks of Science: "))

# Calculate and display the average

avg(eng, math, hindi, sst, sc)

# Ask if the user wants to leave

Output:
Enter the marks of English: 57

Enter the marks of Maths: 60

Enter the marks of Hindi: 58

Enter the marks of SST: 60

Enter the marks of Science: 60

Your marks: English=57.0, Maths=60.0, Hindi=58.0, SST=60.0, Science=60.0

Average of your marks is: 59.0


Q3>WAP to find out largest among 3 numbers

#function to find largest number

def large(a,b,c):

large_num=max(a,b,c)

print(f"largest among the given three numbers is:{large_num}")

#taking input

while True:

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

b=int(input("Enter the second number:"))

c=int(input("Enter the third number:"))

#display the largest number

large(a,b,c)

Output:

Enter the first number:26

Enter the second number:27

Enter the third number:14

largest among the given three numbers is:27


Q4>WAP to find from input :
i)character is alphabet or not ii)character is vowel or not
#main program

print("Welcome to my program")

def check_alphabet(a):

vowel="aeiouAEIOU"

if a in vowel:

print(a[0]," is an vowel")

else:

print(a[0],"is a consonant")

#input

a=input("enter anything of you choice")

#loop for the input is alphabet or not

if a.isdigit():

print("the input is a number")

elif a.isalpha():

print("the input is a alphabet")

if len(a)==1:

check_alphabet(a)

else:

b=a[0]

check_alphabet(b)

else :

print("the input contains both number and alphabet or cotain special characters")

Output:
Welcome to my program

enter anything of you choiceApple

the input is a alphabet

A is an vowel
Q5>WAP to check a given year is leap year or not

print('welcome to my program')

#function to find leap year

def find_leap(year):

if year%4==0:

print(f"{year} is a leap year")

else:

print(f"{year} is a normal year")

#loop to take year

while True:

year=int(input("Enter the year you want:"))

find_leap(year)

Output:

welcome to my program

Enter the year you want:2025

2025 is a normal year

Enter the year you want:2024

2024 is a leap year


Q6> WAP to calculate the mark% and grade
# Function to calculate percentage

def percentage(marks, total_marks):

return (marks / total_marks) * 100

# Function to determine grade based on percentage

def grade(mark_per):

if mark_per > 90:

print("You got Grade: O in the exam, very good! Keep it up!")

elif 76 <= mark_per <= 90:

print("You got Grade: A in the exam, good!")

elif 50 <= mark_per < 76:

print("You got Grade: B in the exam, not bad, try a little harder to improve!")

elif 35 <= mark_per < 50:

print("You got Grade: C in the exam, needs some improvement!")

else:

print("You got Grade: F in the exam, try harder! You need improvement.")

# Main program loop

while True:

name = input("Enter your name: ")

Class = input("Enter your class: ")

roll = input("Enter your roll no: ")

try:

marks = float(input("Enter your marks: "))

total_marks = float(input("Enter the total marks: "))

if marks > total_marks:

print("Marks cannot exceed total marks. Please try again.")

continue

# Display student details

print(f"\nName: {name}",”\nClass: {Class}”,”\nRoll no: {roll}”)

# Calculate percentage and determine grade

mark_per = percentage(marks, total_marks)

print(f"Your marks percentage is: {mark_per}%")

grade(mark_per)
Q7>WAP to display all even numbers between 100 to 200 and their sum
#main program

print("welcome to my python program")

#function for finding even no. between 100-200 and their sum

total = 0

for i in range(100, 201, 2):

total += i # Add the current number to the total

# Print the result

print(f"The sum of even numbers between 100 and 200 is:",total)

Output:
welcome to my python program

The sum of even numbers between 100 and 200 is: 7650

Q8>WAP to find no of digit and the sum of digits


# Function to find the number of digits and the sum of digits

def analyze(number):

num_digits = len(number)

sum_digits = sum(int(digit) for digit in num_str) # Sum the digits

return num_digits, sum_digits

# Program interface

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

num_digits, sum_digits = analyze(number)

# Display the results

print(f"The number of digits in {number} is: {num_digits}")

print(f"The sum of the digits in {number} is: {sum_digits}")

Output:
Enter a number: 26

The number of digits in 26 is: 2

The sum of the digits in 26 is: 8


Q9>WAP to display pattern of 1; 1, 2; 1, 2, 3; 1, 2, 3, 4; 1, 2, 3, 4, 5
# Program to display the pattern

rows = 5

for i in range(1, rows + 1):

for n in range(1, i + 1):

print(n, end=" ")

Output:
1

12

123

1234

12345

Q10>WAP to print multiplication table of a number


# Input: Accept a number from the user

number = int(input("Enter a number to display its multiplication table: "))

# Loop to generate the multiplication table

for i in range(1, 11):

print(f"{number} x {i} = {number * i}")

Output:
Enter a number to display its multiplication table: 12

12 x 1 = 12

12 x 2 = 24

12 x 3 = 36

12 x 4 = 48

12 x 5 = 60

12 x 6 = 72

12 x 7 = 84

12 x 8 = 96

12 x 9 = 108

12 x 10 = 120
Q11>WAP to calculate the electric bill
# Function to calculate the bill

def calculate_bill(units):

if units < 150:

rate = 7

else:

rate = 8

bill = units * rate

return bill

# Input: Accept consumer details

name = input("Enter consumer name: ")

consumer_number = input("Enter consumer number: ")

units_consumed = int(input("Enter total units consumed: "))

# Calculate the bill

bill_amount = calculate_bill(units_consumed)

# Display the electricity bill

print("Electricity Bill")

print(f"Consumer Name: {name}")

print(f”Consumer Number: {consumer_number}")

print(f"Total Units Consumed: {units_consumed}")

print(f"Total Bill Amount: Rs.{bill_amount}")

Output:
Enter consumer name: Ram

Enter consumer number: 9330054347

Enter total units consumed: 350

Electricity Bill

Consumer Name: Ram

Consumer Number: 9330054347

Total Units Consumed: 350

Total Bill Amount: Rs.2800


Q12>WAP calculate employee bonus
# Get employee details

employee_name = input("Enter the Employee Name: ")

salary = float(input("Enter the Salary: "))

service_year = int(input("Enter the Years of Service: "))

# Calculate bonus

if service_year > 5:

bonus = salary * 0.05

else:

bonus = 0

# Display employee details

print("Employee Details:")

print(f"Name: {employee_name}")

print(f"Salary: {salary}")

print(f"Years of Service: {service_year }")

print(f"Bonus: {bonus}”)

Output:
Enter the Employee Name: Harish

Enter the Salary: 35000

Enter the Years of Service: 4

Employee Details:

Name: Harish

Salary: 35000.00

Years of Service: 5

Bonus: 0.00
Q13>WAP to check if a number is prime, even or odd and positive or negative
print("Welcome to my python program")

#function to ckect if a number is prime

def prime(number):

if number > 1:

for i in range(2, int(number ** 0.5) + 1):

if number % i == 0:

return f"{number} is not a Prime Number."

return f"{number} is a Prime Number."

return f"{number} is not a Prime Number."

# Function to check if a number is even or odd

def even_odd(number):

if number % 2 == 0:

return f"{number} is Even."

else:

return f"{number} is Odd."

# Function to check if a number is positive or negative

def positive_negative(number):

if number > 0:

return f"{number} is Positive."

elif number < 0:

return f"{number} is Negative."

else:

return f"The number is Zero."

# Main program

while True:

print("Choose an Option:")

print("1. Check if Prime")

print("2. Check if Even or Odd")

print("3. Check if Positive or Negative")

option = int(input("Enter your option (1/2/3): "))


number = int(input("Enter a Number: "))

if option == 1:

result = prime(number)

elif option == 2:

result = even_odd(number)

elif option == 3:

result = positive_negative(number)

else:

result = "Invalid Option!"

# Display the result

print(result)

a=input("Did you want to leave the program (y/n):")

if a=="y":

print("thankyou! for using this program")

break

elif a=="n":

continue

else:

print('Invaild option please select "y" or "n"')

Output:
Welcome to my python program

Choose an Option:

1. Check if Prime

2. Check if Even or Odd

3. Check if Positive or Negative

Enter your option (1/2/3): 1

Enter a Number: 67

67 is a Prime Number.

Did you want to leave the program (y/n):y

thankyou! for using this program


Q14>WAP to create list of 7 cities and i)append a new city, ii)arrange in
alphabetical order, iii)remove a city
#Create a list of 7 cities

cities = ['Lucknow', 'Patna', 'Kolkata', 'Mumbai', 'New Delhi', 'Trivantanampuram', 'Jaipur']

#Display all cities

print("All cities:")

print(cities)

#Append a new city and display the list

cities.append('Hydrabad')

print("After appending a new city:")

print(cities)

#Arrange the cities in alphabetical order and display them

cities.sort()

print("Cities in alphabetical order:")

print(cities)

#Remove a city from the list and display the final list

cities.remove()

print("After removing a city:")

print(cities)

Output:

All cities:

['Lucknow', 'Kolkata', 'Mumbai', 'New Delhi', 'Chennai', 'Jaipur']

After appending a new city:

['Lucknow', 'Kolkata', 'Mumbai', 'New Delhi', 'Chennai', 'Jaipur', 'Hydrabad']

Cities in alphabetical order:

['Chennai', 'Hydrabad', 'Jaipur', 'Kolkata', 'Lucknow', 'Mumbai', 'New Delhi']

After removing a city:

['Chennai', 'Hydrabad', 'Jaipur', 'Kolkata', 'Lucknow', 'Mumbai']


Q15>WAP to create a list 7 numbers and i) sort the list in ascending order, ii)
remove any element from the list, iii) insert a value, iv) calculate the sum,v)
display the highest value
#Create a list with 7 numeric values

my_list = [12, 7, 25, 19, 3, 15, 9]

#Append one more value to the list

my_list.append(20)

print("After appending a value:", my_list)

#Sort the list in ascending order

my_list.sort()

print("After sorting in ascending order:", my_list)

#Remove any element from the list

removed_element = my_list.pop(2)

print(f"After removing the element at index 2 ({removed_element}):", my_list)

#Insert a value at the 3rd location (index 2)

my_list.insert(2, 18)

print("After inserting 18 at the 3rd location:", my_list)

#Calculate the sum of values in the list

sum_of_values = sum(my_list)

print("Sum of values in the list:", sum_of_values)

#Find and display the highest value from the list

highest_value = max(my_list)

print("Highest value in the list:", highest_value)

Output:
After appending a value: [12, 7, 25, 19, 3, 15, 9, 20]

After sorting in ascending order: [3, 7, 9, 12, 15, 19, 20, 25]

After removing the element at index 2 (9): [3, 7, 12, 15, 19, 20, 25]

After inserting 18 at the 3rd location: [3, 7, 18, 12, 15, 19, 20, 25]

Sum of values in the list: 119

Highest value in the list: 25

You might also like