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

Python Programs for Basic Calculations

fhfhsd

Uploaded by

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

Python Programs for Basic Calculations

fhfhsd

Uploaded by

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

# Program to add elements of two lists

# Example lists
list1 = [1, 2, 3, 4, 5]
list2 = [10, 20, 30, 40, 50]

# Check if both lists are of same length


if len(list1) == len(list2):
result = [a + b for a, b in zip(list1, list2)]
print("List after addition:", result)
else:
print("Error: Lists are of different lengths.")

import numpy as np
from scipy import stats

# List of distances travelled in a week (example data)


distances = [120, 150, 160, 130, 150, 170, 180]

# Mean
mean_value = [Link](distances)

# Median
median_value = [Link](distances)

# Mode
mode_value = [Link](distances, keepdims=True)

print("Distances travelled in a week:", distances)


print("Mean :", mean_value)
print("Median:", median_value)
print("Mode :", mode_value.mode[0], " (Count:", mode_value.count[0], ")")

# Program to calculate factorial of a number

# Taking input from user


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

# Factorial calculation
factorial = 1
if n < 0:
print("Factorial does not exist for negative numbers.")
elif n == 0:
print("Factorial of 0 is 1.")
else:
for i in range(1, n + 1):
factorial *= i
print(f"Factorial of {n} is {factorial}")

# Program to count vowels and consonants in a string

# Input from user


text = input("Enter a string: ")

# Convert to lowercase for easy checking


text = [Link]()
# Define vowels
vowels = "aeiou"

# Counters
vowel_count = 0
consonant_count = 0

# Count vowels and consonants


for char in text:
if [Link](): # Only check letters
if char in vowels:
vowel_count += 1
else:
consonant_count += 1

# Display result
print("Number of vowels :", vowel_count)
print("Number of consonants :", consonant_count)

# Program to check validity and type of a triangle

# Input sides
a = float(input("Enter length of first side: "))
b = float(input("Enter length of second side: "))
c = float(input("Enter length of third side: "))

# Check if triangle is possible using Triangle Inequality Theorem


if (a + b > c) and (a + c > b) and (b + c > a):
print("A triangle can be formed.")

# Check type of triangle


if a == b == c:
print("It is an Equilateral triangle.")
elif a == b or b == c or a == c:
print("It is an Isosceles triangle.")
else:
print("It is a Scalene triangle.")
else:
print("A triangle cannot be formed with these sides.")

# Program to check validity and type of a triangle

# Input sides
a = float(input("Enter length of first side: "))
b = float(input("Enter length of second side: "))
c = float(input("Enter length of third side: "))

# Check if triangle is possible using Triangle Inequality Theorem


if (a + b > c) and (a + c > b) and (b + c > a):
print("A triangle can be formed.")

# Check type of triangle


if a == b == c:
print("It is an Equilateral triangle.")
elif a == b or b == c or a == c:
print("It is an Isosceles triangle.")
else:
print("It is a Scalene triangle.")
else:
print("A triangle cannot be formed with these sides.")

# Program to swap two numbers without using third variable

# Input numbers
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))

print(f"Before swapping: a = {a}, b = {b}")

# Swapping using arithmetic operations


a = a + b
b = a - b
a = a - b

print(f"After swapping: a = {a}, b = {b}")

You might also like