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

Python First Part

The document contains examples of Python code to perform common calculations and operations like finding average, calculating area and perimeter, profit/loss, EMI etc. It also includes examples of working with lists, dictionaries and strings.

Uploaded by

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

Python First Part

The document contains examples of Python code to perform common calculations and operations like finding average, calculating area and perimeter, profit/loss, EMI etc. It also includes examples of working with lists, dictionaries and strings.

Uploaded by

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

1.To find average and grade for given marks.

a= int(input("Enter English marks = "))

b= int(input"Enter Maths marks = "))

c= int(input("Enter Science marks = "))

d= int(input("Enter Social studies marks = ")

h= (a+b+c+d)/4

print ("Average marks =", h)

ifh >=90:

print ("A1"')

elif h >= 80:

print ("A2")

elif. h >= 70:

print ("B1")

elif. h == 60:

print ("B2")

elif. h >= 50:

print ("C1")

elif h >= 40:

print ("C2")

else:

print ("F")

2. To find sale price of an item with given cost and discount (%)
cost = float(input("Cost of the item: $"))
discount = float(input("Discount percentage: "))

sale_price = cost - cost * (discount / 100)

print(f"Sale price after a {discount}% discount: ${sale_price:.2f}")

3.To calculate perimeter/circumference and area of shapes such as triangle, rectangle, square and circle.

import math

# Triangle
a, b, c = 3, 4, 5
triangle_perimeter = a + b + c
s = (a + b + c) / 2
triangle_area = math.sqrt(s * (s - a) * (s - b) * (s - c))

# Rectangle
length, width = 6, 8
rectangle_perimeter = 2 * (length + width)
rectangle_area = length * width

# Square
side = 5
square_perimeter = 4 * side
square_area = side ** 2

# Circle
radius = 2.5
circle_circumference = 2 * math.pi * radius
circle_area = math.pi * radius ** 2

# Output results
print("Triangle Perimeter:", triangle_perimeter)
print("Triangle Area:", triangle_area)
print("Rectangle Perimeter:", rectangle_perimeter)
print("Rectangle Area:", rectangle_area)
print("Square Perimeter:", square_perimeter)
print("Square Area:", square_area)
print("Circle Circumference:", circle_circumference)
print("Circle Area:", circle_area)
4.To calculate Simple and Compound interest.

principal_amount = 1000
interest_rate = 5
time_period = 2

simple_interest = (principal_amount * interest_rate * time_period) / 100

compound_frequency = 1

amount = principal_amount * (1 + interest_rate / (100 * compound_frequency)) ** (compound_frequency * time_period)


compound_interest = amount - principal_amount

print("Simple Interest:", simple_interest)


print("Compound Interest:", compound_interest)
5. To calculate profit-loss for given Cost and Sell Price.

# Input values
cost_price = 800
sell_price = 1000

# Calculate profit or loss


profit_loss = sell_price - cost_price

# Determine if it's a profit or loss


if profit_loss > 0:
print("Profit:", profit_loss)
elif profit_loss < 0:
print("Loss:", abs(profit_loss))
else:
print("No Profit, No Loss")

6.To calculate EMI for Amount, Period and Interest.

loan_amount = float(input("Enter the loan amount: "))


loan_period = int(input("Enter the loan period in months: "))
annual_interest_rate = float(input("Enter the annual interest rate: "))

monthly_interest_rate = (annual_interest_rate / 100) / 12

emi_amount = (loan_amount * monthly_interest_rate) / (1 - (1 + monthly_interest_rate)**-loan_period)

print(f"\nThe Equated Monthly Installment (EMI) is: {emi_amount:.2f}")


7. To calculate tax - GST / Income Tax.

GST calculation:

product_price = float(input("Enter the product price: "))

# (assuming 18% for this example)


gst_rate = 18 / 100

gst_amount = product_price * gst_rate

print(f"\nThe GST amount is: {gst_amount:.2f}")

8.To find the largest and smallest numbers in a list.

numbers = [float(x) for x in input("Enter a list of numbers separated by space: ").split()]

largest_number = max(numbers)
smallest_number = min(numbers)

print(f"Largest number: {largest_number}")


print(f"Smallest number: {smallest_number}")

9.To find the third largest/smallest number in a list.


input_numbers = input("Enter a list of numbers separated by space: ")
numbers = [float(num) for num in input_numbers.split()]
if len(numbers) < 3:
print("Please enter at least three numbers.")
else:
numbers.sort()

third_largest = numbers[-3]
third_smallest = numbers[2]

print(f"The third largest number is: {third_largest}")


print(f"The third smallest number is: {third_smallest}")

10.To find the sum of squares of the first 100 natural numbers.

sum_of_squares = sum(i**2 for i in range(1, 101))

print("The sum of squares of the first 100 natural numbers is:", sum_of_squares)

11.To print the first 'n' multiples of given number.

base_number = int(input("Enter a number to get multiples: "))


num_of_multiples = int(input("How many multiples do you want? "))
multiples = [base_number * i for i in range(1, num_of_multiples + 1)]

print(f"The first {num_of_multiples} multiples of {base_number} are: {multiples}")

12. To count the number of vowels in user entered string.

user_input = input("Enter a string: ")

vowel_count = sum(1 for char in user_input if char.lower() in 'aeiou')

print(f"The number of vowels in the entered string is: {vowel_count}")

13.To print the words starting with a alphabet in a user entered string.

user_input = input("Enter a string: ")

words_starting_with_a = [word for word in user_input.split() if word.lower().startswith('a')]

print(f"Words starting with 'a': {', '.join(words_starting_with_a)}")


14.To print number of occurrences of a given alphabet in each string.

strings = input("Enter strings (comma-separated): ").split(',')


alphabet_to_count = input("Enter alphabet to count: ")

for string in strings:


count = string.lower().count(alphabet_to_count.lower())
print(f"'{alphabet_to_count}' in '{string}': {count}")

15. Create a dictionary to store names of states and their capitals.

state_capitals = {
"Alabama": "Montgomery",
"Alaska": "Juneau",
"Arizona": "Phoenix",
"Arkansas": "Little Rock",
"California": "Sacramento",}

print("Capital of Arizona:", state_capitals["Arizona"])

state_capitals["New York"] = "Albany"

print("\nState Capitals Dictionary:", state_capitals)


16.Create a dictionary of students to store names and marks obtained in 5 subjects.

students_marks = {
"John": [85, 90, 92, 88, 89],
"Alice": [78, 87, 80, 92, 85],
"Bob": [95, 88, 94, 90, 91],

}
print("Marks of John:", students_marks["John"])

students_marks["Eve"] = [92, 94, 89, 88, 95]

print("\nStudents Marks Dictionary:", students_marks)

17.To print the highest and lowest values in the dictionary.


students_marks = {
"John": [85, 90, 92, 88, 89],
"Alice": [78, 87, 80, 92, 85],
"Bob": [95, 88, 94, 90, 91],
}

highest_student = max(students_marks, key=lambda student: sum(students_marks[student]) / len(students_marks[student]))


lowest_student = min(students_marks, key=lambda student: sum(students_marks[student]) / len(students_marks[student]))

print(f"Highest Average Marks: {highest_student} -


{sum(students_marks[highest_student])/len(students_marks[highest_student]):.2f}")
print(f"Lowest Average Marks: {lowest_student} -
{sum(students_marks[lowest_student])/len(students_marks[lowest_student]):.2f}")

You might also like