python sample programs all my edits
python sample programs all my edits
➢ Datatypes in Python
=========================
a = 56
b = 89.4
c = 'y'
d = 67883542
e = 34.5e10 # Scientific notation
sname = "trees are tall"
print(a, b, c, d, e, sname)
===========================
➢ Compoundintrest calculation
================================
P = float(input("Enter principal amount: "))
r = float(input("Enter rate of interest: "))
n = int(input("Enter number of times interest applied per time period: "))
t = int(input("Enter time in years: "))
CI = P * (1 + r / (100 * n))**(n * t) - P
print(f"The Compound Interest Value is {CI:.6f}"
==============================
➢ Celsius to Fahrenheit Conversion
================================
cel = float(input("Please enter temperature in Celsius: "))
farH = cel * 1.8 + 32
print(f"The temperature in Fahrenheit is {farH:.6f} F")
==========================================
=========================================
ch = input("Enter any alphabet between A to Z: ").upper()
if d == 0:
r1 = r2 = -b / (2 * a)
print(f"The roots are real and equal: {r1:.6f}")
elif d > 0:
r1 = (-b + math.sqrt(d)) / (2 * a)
r2 = (-b - math.sqrt(d)) / (2 * a)
print(f"The roots are: {r1:.6f} and {r2:.6f}")
else:
print("The roots are imaginary")
➢ Math Operators (Calculator)
if op == '+':
print(f"The sum is {a + b}")
elif op == '-':
print(f"The difference is {a - b}")
elif op == '*':
print(f"The product is {a * b}")
elif op == '/':
print(f"The quotient is {a / b}")
elif op == '%':
print(f"The remainder is {a % b}")
else:
print("Invalid operator")
===================================
greatest = max(a, b, c)
====================================
=========================================
a = 15
b=2
c = float(a) # Type conversion
➢ Ternary Operator
a, b = 15, 2
print(f"{a} is greater" if a > b else f"{b} is greater")
=======================≈==================
a, b, c = map(int, input("Enter three numbers: ").split())
if a > b:
if a > c:
print(f"{a} is greatest")
else:
print(f"{c} is greatest")
else:
if b > c:
print(f"{b} is greatest")
else:
print(f"{c} is greatest")
==========================================
➢ Switch Case (Using Dictionary in Python)
==========================================
=====================================
—
x=1
while x <= 10:
print(x, end="\t")
x += 1
➢ Multiplication Table
==========================================
num = int(input("Enter a number: "))
n=1
➢ Factorial of a Number
===================================
num = int(input("Enter a number: "))
fact = 1
==========================================
➢ Reverse a Number
====================================
num = int(input("Enter a number: "))
rev = 0
while num != 0:
rem = num % 10
rev = rev * 10 + rem
num //= 10
===================================
⏪⏪⏪⏪⏪⏪⏪⏪⏪⏪⏪⏪⏪⏪🕙⏪⏪⏪⏪⏪⏪⏪⏪⏪⏪