SIMATS ENGINEERING
SAVEETHA INSTITUTE OF MEDICAL AND TECHNICAL
SCIENCES
CSA0810 – PYTHON PROGRAMMING
LEVEL – I QUICK REVISION SHEET (CODE ONLY)
■ Factorial of a number
n = 5 fact = 1 for i in range(1, n+1): fact = fact * i print(f"Factorial of {n} is {fact}")
■ Palindrome check
n = 12321 rn = 0 on = n while n > 0: rem = n % 10 rn = rn * 10 + rem n = n // 10 if on == rn:
print(f"{on} is a Palindrome") else: print(f"{on} is not a Palindrome")
■ Fibonacci sequence
n = 10 f1, f2 = 0, 1 for i in range(n): print(f1) f1, f2 = f2, f1 + f2
■ Area of a triangle
base = 5 height = 3 area = (base * height) / 2 print(f"Area is {area}")
■ LCM of two numbers
num1 = 15 num2 = 21 if num1 > num2: greater = num1 else: greater = num2 while True: if greater
% num1 == 0 and greater % num2 == 0: lcm = greater break greater += 1 print(f"LCM of {num1}
and {num2} is {lcm}")
■ GCD of two numbers
num1 = 21 num2 = 15 a, b = num1, num2 while b != 0: a, b = b, a % b print(f"The GCD of {num1}
and {num2} is {a}")
■ Reverse a string
inStr = "Welcome" print(inStr[::-1])
■ Largest of three numbers
num1 = 21 num2 = 15 num3 = 17 if num1 > num2 and num1 > num3: print(f"Largest No is
{num1}") elif num2 > num1 and num2 > num3: print(f"Largest No is {num2}") else:
print(f"Largest No is {num3}")