python programs-1
python programs-1
ith
ad
ig
ita
l
1.Basic Operations in Python: Add,
Subtract, Multiply, and Divide Two
l
Numbers
ita
Introduction
ig
step on how to take two numbers as input and perform these operations.
ad
Step-by-Step Theory
Step 1: Understanding Arithmetic Operators
ith
● + → Addition
● - → Subtraction
● * → Multiplication
an
● / → Division
l
ita
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
ig
division = num1 / num2 # Handles decimal division
print("\nResults:")
ad
print(f"Addition: {num1} + {num2} = {addition}")
print(f"Subtraction: {num1} - {num2} = {subtraction}")
print(f"Multiplication: {num1} × {num2} = {multiplication}")
print(f"Division: {num1} ÷ {num2} = {division}")
ith
l
ita
Input:
Output:
Results: ig
ad
Addition: 10.0 + 5.0 = 15.0
Subtraction: 10.0 - 5.0 = 5.0
Multiplication: 10.0 × 5.0 = 50.0
Division: 10.0 ÷ 5.0 = 2.0
ith
an
l
ita
ig
ad
ith
an
Key Takeaways
l
ita
Introduction
In Python, we can determine if a number is even or odd using the modulus operator (%). This
ig
tutorial explains how to take a number as input and check whether it is even or odd.
ad
Step-by-Step Theory
Step 1: Understanding Even and Odd Numbers
l
num = int(input("Enter a number: ")
ita
if num % 2 == 0:
else:
l
ita
Key Takeaways
ig
ad
✅ The modulus operator (%) helps check divisibility.
✅ Even numbers have num % 2 == 0.
✅ Odd numbers have num % 2 != 0.
✅ if-else conditions help make decisions in Python.
ith
an
l
Step-by-Step Theory
ita
Step 1: Understanding Leap Year Rules
ig
2. Except when it is also divisible by 100 → year % 100 == 0 (not a leap year)
3. Unless it is also divisible by 400 → year % 400 == 0 (leap year)
ad
Step 2: Taking User Input
l
○ year % 400 == 0: If divisible by 400, it is a leap year.
ita
3. Displaying the Result
○ print(f"{year} is a Leap Year.") prints the result.
Example Outputs ig
ad
ith
an
Key Takeaways
✅ A leap year must be divisible by 4.
✅ It must not be divisible by 100, unless also divisible by 400.
✅ The modulus operator (%) helps check divisibility.
✅ if-elif-else statements help in decision-making.
4.Factorial: Python Program to Find the
Factorial of a Number
l
ita
Introduction
The factorial of a number (n!) is the product of all positive integers from 1 to n.
● Example:
○ 5! = 5 × 4 × 3 × 2 × 1 = 120
○ 4! = 4 × 3 × 2 × 1 = 24 ig
ad
○ 1! = 1
○ 0! = 1 (By definition)
ith
Step-by-Step Theory
Step 1: Understanding Factorial Calculation
For example:
● 3! = 3 × 2 × 1 = 6
● 6! = 6 × 5 × 4 × 3 × 2 × 1 = 720
l
● Use a for loop to multiply numbers from 1 to n.
ita
num = int(input("Enter a number: "))
factorial = 1
if num < 0:
ig
print("Factorial is not defined for negative numbers.")
ad
elif num == 0 or num == 1:
print(f"The factorial of {num} is 1")
else:
for i in range(1, num + 1):
factorial *= i
print(f"The factorial of {num} is {factorial}")
ith
l
ita
ig
ad
ith
Key Takeaways
✅ Factorial (n!) is the product of all numbers from 1 to n.
an
✅ 0! = 1 by definition.
✅ Factorial is not defined for negative numbers.
✅ Loop and recursion can be used to compute factorial.
✅ Using a loop is more memory-efficient than recursion.
5.Palindrome: Python Program to Check if
a String is a Palindrome
l
Introduction
ita
A palindrome is a word, phrase, number, or sequence of characters that reads the same
forward and backward.
Examples of Palindromes
✅ "madam" → Palindrome
✅ "racecar" → Palindrome
✅ "level" → Palindrome
❌ "hello" → Not a palindrome
ig
ad
❌ "Python" → Not a palindrome
ith
Step-by-Step Theory
Step 1: Understanding String Reversal
an
l
ita
Step 5: Comparing the Strings
ig
text = input("Enter a string: ").lower() # Convert to lowercase
reversed_text = text[::-1]
ad
if text == reversed_text:
print(f'"{text}" is a Palindrome.')
else:
ith
Example Outputs
l
ita
Key Takeaways
ig
✅ A palindrome reads the same forward and backward.
ad
✅ Use string[::-1] for quick string reversal.
✅ Convert to lowercase to ignore case sensitivity.
✅ A loop-based method can also be used for checking.
ith
an
an
ith
ad
ig
ita
l