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

Python Basics: Functions & Calculations

Uploaded by

s1049bhavya12250
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views7 pages

Python Basics: Functions & Calculations

Uploaded by

s1049bhavya12250
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

Print Hello World

print("Hello World!")

Hello World!

[Link] School Address City

school=input("Enter your school name: ")


address=input("Enter your school address: ")
city=input("Enter your City: ")
print(school,"-",address,"-",city)

SPS - Bhopal - Pune 2.

[Link] of two numbers.

num1=1.5

num2=6.3

sum=num1+num2

print("sum of two given numbers is: ",sum)

[Link] to find sum and average of three numbers

a=int(input("Enter the 1st number: "))

b=int(input("Enter the 2nd number: "))

c=int(input("Enter the 3rd number: "))

sum=a+b+c

avg=sum/3

print("Sum of all 3 numbers is: ",sum)

print("Average of all 3 numbers is: ",avg)


5. Program to find the area and perimeter of circle
PI = 3.14

R = float(input("Enter radius of the circle: "))

area = (PI*R*R)

perimeter = (2*PI*R)

print("The area of circle is", area)

print("The perimeter of circle is", perimeter)


6. Python find square and cube of given number

n = input("Enter the value :--> ")

n = int(n)

square = n ** 2

cube = n ** 3

print("\nSquare of the number :-->", square)

print("Cube of the number :-->", cube)


7. Python Program to find area and perimeter of rectangle

l=int(input("Length : "))
w=int(input("Width : "))
area=l*w
perimeter=2*(l+w)
print("Area of Rectangle : ",area)
print("Perimeter of Rectangle : ",perimeter)

8. Python Programs to find total,average and percentage of five subjects

english = float(input("Please enter English Marks: "))

math = float(input("Please enter Math score: "))

computers = float(input("Please enter Computer Marks: "))

physics = float(input("Please enter Physics Marks: "))

chemistry = float(input("Please enter Chemistry Marks: "))

total = english + math + computers + physics + chemistry

average = total / 5

percentage = (total / 500) * 100

print("\nTotal Marks = ",total)

print("Average Marks = ",average)

print("Marks Percentage = ",percentage)


[Link] Programs to find the area of triangle

a = float(input('Enter first side: '))


b = float(input('Enter second side: '))
c = float(input('Enter third side: '))
s = (a + b + c) / 2
area = (s*(s-a)*(s-b)*(s-c)) ** 0.5
print('The area of the triangle is : ',area)
10. Python Program find the largest number among three numbers using if-
else

# Input three numbers

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

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

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

# Compare numbers using if-else

if num1 >= num2 and num1 >= num3:

largest = num1

elif num2 >= num1 and num2 >= num3:

largest = num2

else:

largest = num3

print("The largest number is:", largest)

program to check whether a given number is even or odd:


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

if number % 2 == 0:

print(f"The number {number} is even.")

else:

print(f"The number {number} is odd.")

[Link] program to calculate an electricity bill based on usage with different rate slabs using if-else:
units = float(input("Enter electricity usage in units: "))

if units <= 50:

bill = units * 1.5

elif units <= 150:

bill = 50 * 1.5 + (units - 50) * 2.5

elif units <= 250:

bill = 50 * 1.5 + 100 * 2.5 + (units - 150) * 4

else:

bill = 50 * 1.5 + 100 * 2.5 + 100 * 4 + (units - 250) * 6

# Add fixed charge

bill += 50 # Fixed charge for maintenance

print(f"Electricity bill: ₹{bill:.2f}")

[Link] program to print natural numbers up to a given number nnn using a loop:

n = int(input("Enter a positive integer: "))

print("Natural numbers up to", n, "are:")

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

print(i, end=" ")

[Link] program to calculate the sum of natural numbers up to n using a loop:


n = int(input("Enter a positive integer: "))

sum_n = 0

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

sum_n += i # Add each number to sum_n

print(f"The sum of natural numbers up to {n} is: {sum_n}")

[Link] program to print the multiplication table of a given number


using a loop:
num = int(input("Enter a number to print its multiplication table: "))

print(f"Multiplication table of {num}:")

for i in range(1, 11):

print(f"{num} * {i} = {num * i}")

[Link] program to print a triangle pattern of stars (*)

n = int(input("Enter the number of rows for the triangle: "))

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

print("*" * i) # Print i stars in each row

[Link] program to find the sum and average of even and odd numbers from a set of n
numbers:
n = int(input("Enter the number of elements: "))

# Initialize variables for even and odd sums and counts

even_sum = 0

odd_sum = 0

even_count = 0

odd_count = 0
# Input n numbers and classify as even or odd

for i in range(n):

num = int(input(f"Enter number {i+1}: "))

if num % 2 == 0:

even_sum += num

even_count += 1

else:

odd_sum += num

odd_count += 1

# Calculate the average of even and odd numbers

even_avg = even_sum / even_count if even_count > 0 else 0

odd_avg = odd_sum / odd_count if odd_count > 0 else 0

# Display the results

print(f"Sum of even numbers: {even_sum}")

print(f"Average of even numbers: {even_avg:.2f}")

print(f"Sum of odd numbers: {odd_sum}")

print(f"Average of odd numbers: {odd_avg:.2f}")


[Link] to calculate factorial

def factorial(n):

if n == 0 or n == 1: # Base case: factorial of 0 or 1 is 1

return 1

else:

return n * factorial(n - 1) # Recursive call

num = int(input("Enter a number to find its factorial: "))

result = factorial(num)

print(f"The factorial of {num} is {result}.")

Common questions

Powered by AI

A Python program can calculate an electricity bill by applying rates for different usage slabs through a series of if-elif statements. For units up to 50, multiply by 1.5; for units up to 150, add the previous total to (units - 50) * 2.5; for up to 250, add the total to (units - 150) * 4; beyond 250, add the total to (units - 250) * 6. Include a fixed charge at the end for maintenance. This approach allocates costs appropriately based on usage tiers, ensuring accurate billing .

To implement a loop in Python for printing natural numbers up to 'n', use a 'for' loop ranging from 1 to n+1, as it is exclusive of the end number. Print each number during each iteration. This approach uses precise loop control with a known number of iterations equal to 'n' .

To determine the largest number among three given numbers using if-else in Python, compare the numbers by checking conditions sequentially. Start by checking if the first number is greater than or equal to the second and the third numbers. If true, the first number is the largest. If not, check if the second number is greater than or equal to the first and third to decide if it is the largest. Otherwise, by default, the third number is the largest .

To calculate the sum and average of even and odd numbers from a dataset, iterate over the dataset with a loop. For each number, check its parity using the modulus operator (%). If a number is even (% yields 0), add it to the even sum and increment the even counter. Otherwise, add it to the odd sum and increment the odd counter. After processing all numbers, calculate the averages by dividing the sum by counts respectively, handling division by zero by checking counts before division .

To code a program that prints a multiplication table for a given number in Python, use a 'for' loop to iterate over the range 1 to 10. During each iteration, multiply the given number by the iterating variable and print the result in a formatted string. This method efficiently generates the multiplication table by leveraging loop structures for repetitive multiplication operations .

To calculate the factorial of a number in Python, recursive functions can be employed. Define a function that returns 1 for the base case where the input number 'n' is 0 or 1, as the factorial of both is 1. For other cases, return n multiplied by the factorial of (n-1), which recursively calculates the factorial until the base case is reached .

A Python program evaluates if a number is even or odd by using the modulus operator (%). If the number divided by 2 has a remainder of 0, it is even. Otherwise, it is odd. This operation relies on the properties of integer division and conditional statements to classify the number .

To sum natural numbers up to 'n' in Python, initialize a sum variable to 0. Use a 'for' loop to iterate from 1 to 'n', adding each integer to the sum variable. This iterative approach allows tracking cumulative addition to calculate the required sum effectively .

To calculate the area of a triangle with known side lengths, use Heron's formula. First, calculate the semi-perimeter 's' by averaging the sum of all sides: s = (a + b + c) / 2. Then, determine the area using the formula area = sqrt(s * (s - a) * (s - b) * (s - c)). This method leverages the triangle inequality theorem and ensures the area is calculated correctly for any triangle configuration .

To find the area and perimeter of a circle given its radius in Python, use the mathematical constants π (approximately 3.14). Compute the area with the formula πR² and the perimeter with 2πR. This exercise demonstrates understanding of basic geometry concepts and the ability to apply them programmatically using simple arithmetic operations .

You might also like