➢ Swapping Two Numbers Without a Temporary Variable
a = int(input("Please enter first number: "))
b = int(input("Please enter second number: "))
print(f"Before swapping: a = {a}, b = {b}")
a, b = b, a # Swapping using tuple unpacking
print(f"After swapping: a = {a}, b = {b}")
➢ 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")
==========================================
➢ Checking for Vowel
=========================================
ch = input("Enter any alphabet between A to Z: ").upper()
if ch in ['A', 'E', 'I', 'O', 'U']:
print("It is a vowel")
else:
print("It is not a vowel")
==============================
➢ Roots of a Quadratic Equation
import math
a = int(input("Enter value for a: "))
b = int(input("Enter value for b: "))
c = int(input("Enter value for c: "))
d = b**2 - 4*a*c # Discriminant
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)
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
op = input("Enter an operator (+, -, *, /, %): ")
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 of Three Numbers
===================================
a, b, c = map(int, input("Enter three numbers: ").split())
greatest = max(a, b, c)
print(f"{greatest} is the greatest")
====================================
➢ TypeConversionand Type Casting
=========================================
a = 15
b=2
c = float(a) # Type conversion
print(a // b) # Integer quotient
print(c / b) # Float quotient
print(float(a) / b) # Type casting
================================
➢ Ternary Operator
a, b = 15, 2
print(f"{a} is greater" if a > b else f"{b} is greater")
➢ NESTED IF( GREATEST OF THREE)
=======================≈==================
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)
==========================================
def calculator(op, num1, num2):
operations = {
'+': num1 + num2,
'-': num1 - num2,
'*': num1 * num2,
'/': num1 / num2 if num2 != 0 else "Cannot divide by zero",
'%': num1 % num2
}
return operations.get(op, "Invalid operator")
op = input("Enter an arithmetic operator: ")
num1, num2 = map(int, input("Enter two numbers: ").split())
print(calculator(op, num1, num2))
=====================================
—
➢ While Loop Examples
Printing numbers from 1 to 10
x=1
while x <= 10:
print(x, end="\t")
x += 1
➢ Printing numbers from 10 to 1
==========================================
x = 10
while x >= 1:
print(x, end="\t")
x -= 1
===================================
➢ Odd numbers from 1 to 50
==========================================
x=1
while x <= 50:
print(x, end="\t")
x += 2
==================================
➢ Multiplication Table
==========================================
num = int(input("Enter a number: "))
n=1
while n <= 10:
print(f"{num} X {n} = {num * n}")
n += 1
==================================
➢ Factorial of a Number
===================================
num = int(input("Enter a number: "))
fact = 1
for k in range(1, num + 1):
fact *= k
print(f"The factorial of {num} is {fact}")
==========================================
➢ Reverse a Number
====================================
num = int(input("Enter a number: "))
rev = 0
while num != 0:
rem = num % 10
rev = rev * 10 + rem
num //= 10
print(f"The reverse is {rev}")
===================================
⏪⏪⏪⏪⏪⏪⏪⏪⏪⏪⏪⏪⏪⏪🕙⏪⏪⏪⏪⏪⏪⏪⏪⏪⏪