Author: Saravana Kumar

  • Python – Functions Examples

    Example 1 :

    # Define a function
    def greet():
        print("Hello, World!")
    
    # Call the function
    greet()

    Output :

    Hello, World!

    Example 2 :

    # Define a function with parameters
    def greet(name):
        print(f"Hello, {name}!")
    
    # Call the function with an argument
    greet("Alice")

    Output :

    Hello, Alice!

    Example 3 :

    # Define a function with two parameters
    def add(a, b):
        return a + b
    
    # Call the function with arguments
    result = add(3, 4)
    print(result)

    Output :

    7

    Example 4 :

    # Define a function that returns a value
    def square(x):
        return x * x
    
    # Call the function and store the result
    result = square(5)
    print(result)

    Output :

    25

    Example 5 :

    # Define a function that returns multiple values
    def get_name_and_age():
        name = "Alice"
        age = 30
        return name, age
    
    # Call the function and unpack the result
    name, age = get_name_and_age()
    print(name, age)

    Output :

    Alice 30

    Example 6 : (Area of Circle using Functions)

    def areaofcircle(r):
        answer=3.14*r
        return answer
    
    radius=int(input("Enter Radius Value"))
    ans=areaofcircle(radius)
    print ("Answer",ans)

    Output :

    Enter Radius Value 1
    Answer 3.14

    Example 7 :

    def welcome(myname):
        msg="Hi " +myname + " Welcome to My Centre"
        return msg;
    
        
    m=input("Enter Your Name : ")
    print(welcome(m))
    

    Output :

    Enter Your Name : Raj
    Hi Raj Welcome to My Centre
  • Python – Syntax of If, For, While, Break, Continue

    1. If Statement

    if condition:
        # Code to execute if the condition is True
    
    if 5 > 3:
        print("5 is greater than 3")

    2. if-else Statement

    if condition:
        # Code to execute if the condition is True
    else:
        # Code to execute if the condition is False
    if 3 > 5:
        print("3 is greater than 5")
    else:
        print("3 is not greater than 5")

    3. if-elif-else Statement

    if condition1:
        # Code to execute if condition1 is True
    elif condition2:
        # Code to execute if condition2 is True
    else:
        # Code to execute if both conditions are False
    x = 10
    if x > 10:
        print("x is greater than 10")
    elif x == 10:
        print("x is equal to 10")
    else:
        print("x is less than 10")

    4.for Loop

    for variable in sequence:
        # Code to execute in each iteration
    for i in range(5):
        print(i)

    5.while Loop

    while condition:
    # Code to execute as long as the condition is True
    i = 0
    while i < 5:
        print(i)
        i += 1

    6.break Statement

    for/while variable in sequence/condition:
        if condition:
            break  # Exit the loop
    for i in range(10):
        if i == 5:
            break
        print(i)

    7. continue Statement

    for/while variable in sequence/condition:
        if condition:
            continue  # Skip the rest of the code in this iteration
        # Code to execute if the condition is not met
    for i in range(5):
        if i == 2:
            continue
        print(i)

    8. else with Loops

    for/while variable in sequence/condition:
        # Code to execute in each iteration
    else:
        # Code to execute after the loop finishes
    for i in range(3):
        print(i)
    else:
        print("Loop finished!")
  • Python – For Loop Examples

    Example 1
    mycity=["Kalugumalai","Chennai","Madurai"]
    
    for citynames in mycity:
            print (citynames)
            print ("hi")
    Output:
    ======
    Kalugumalai
    hi
    Chennai
    hi
    Madurai
    hi
    Example 2
    mynumbers=[1,2,3,4,5]
    
    for n in mynumbers:
            print (n)
    Output:
    ======
    1
    2
    3
    4
    5
    Example.3
    mynumbers=[2,4,6,8,10]
    
    for n in mynumbers:
            print (n-1)
    output:
    1
    3
    5
    7
    9
    Example 4: To Prints the numbers from 1 to 5
    for i in range(1, 6):
        print(i)

    Output :

    1
    2
    3
    4
    5
    Example 4 : To Prints charactors
    # Define a string
    word = "Hello"
    
    # Start the for loop
    for char in word:
        print(char)

    Output :

    H
    e
    l
    l
    o
    Example 5 : Printing the Elements of a List
    colors = ["red", "green", "blue"]
    
    for color in colors:
        print(color)

    Output :

    red
    green
    blue
    Example 6 : To Sum the Numbers in a List
    numbers = [1, 2, 3, 4, 5]
    
    total = 0
    
    for number in numbers:
        total = total + number 
    
    print("Total sum:", total)
    

    Output :

    Total sum: 15
    Example 7:to Print Multiplication Table
    # Define the number for which to print the multiplication table
    number = 7
    
    # Start the for loop
    for i in range(1, 11):
        print(f"{number} x {i} = {number * i}")
    

    Output :

    7 x 1 = 7
    7 x 2 = 14
    7 x 3 = 21
    ...
    7 x 10 = 70
    Example 8: To Print Odd Numbers Only
    mynumbers=[1,2,3,4,5,6,7,8,9,10]
    
    for n in mynumbers:
        if(n%2=1)
            print (n)
    Output : 
    1
    3
    5
    7
    9
    Example 9 : To Print Even Numbers from 1 to 100
    for n in range(1,101):
        if(n%2=0)
            print (n)
    Output :
    2
    4
    6
    .
    .
    .100
    Example 10 :
    mytable=2
    for i in range(1,10):
         print(f"{i}  x  {mytable}  = {mytable* i}")
    Output : 
    1  x  2  = 2
    2  x  2  = 4
    3  x  2  = 6
    4  x  2  = 8
    5  x  2  = 10
    6  x  2  = 12
    7  x  2  = 14
    8  x  2  = 16
    9  x  2  = 18
  • Python – While Loop Examples

    Example 1 : Counting from 1 to 5

    count = 1
    
    while count <= 5:
        print(count)
        count = count+1

    Output :

    1
    2
    3
    4
    5

    Example 2 : Sum of Numbers from 1 to 10

    
    
    count = 1
    total_sum = 0

    while count <= 10:
    total_sum = total_sum + count
    count =count + 1

    print("The sum of numbers from 1 to 10 is:", total_sum)

    Output :

    The sum of numbers from 1 to 10 is: 55

    Example 3: Asking for a Password

    correct_password = "root1234"
    
    while True:
        password = input("Enter the password: ")
        if password == correct_password:
            print("Access granted!")
            break                                          
        else:
            print("Incorrect password, try again.")

    Output :

    Enter the password: abc
    Incorrect password, try again.
    
    Enter the password: root1234
    Access granted!

    Example 4: To Print Even Numbers Between 1 and 10

    number = 2
    
    while number <= 10:
        print(number)
        number = number + 2

    Output :

    2
    4
    6
    8
    10

    Example 5: ATM Withdrawal

    balance = 1000
    
    
    while True:
        withdrawal = int(input("Enter amount to withdraw: "))
        
        if withdrawal > balance:
            print("Insufficient funds. Your balance is:", balance)
        else:
            balance -= withdrawal
            print("Withdrawal successful. New balance is:", balance)
            break 

    Output :

    Enter amount to withdraw: 1500
    Insufficient funds. Your balance is: 1000
    Enter amount to withdraw: 500
    Withdrawal successful. New balance is: 500

    Example 6: Password Retry Limits

    correct_password = "securePass"
    attempts_left = 3
    
    
    while attempts_left > 0:
        password = input("Enter the password: ")
        
        if password == correct_password:
            print("Access granted!")
            break 
        else:
            attempts_left -= 1
            print("Incorrect password. Attempts left:", attempts_left)
    
    if attempts_left == 0:
        print("Too many failed attempts. Access denied.")

    Output :

    Enter the password: wrong1
    Incorrect password. Attempts left: 2
    Enter the password: wrong2
    Incorrect password. Attempts left: 1
    Enter the password: securePass
    Access granted!
  • Learn Quantitative Aptitude – Divisibility Rules

    Divisibility Rules

    1. Divisibility by 2:
      • A number is divisible by 2 if the last digit is even (0, 2, 4, 6, or 8).
      • Example: 128 is divisible by 2 because the last digit is 8.
    2. Divisibility by 3:
      • A number is divisible by 3 if the sum of its digits is divisible by 3.
      • Example: 123 is divisible by 3 because 1+2+3=61 + 2 + 3 = 61+2+3=6, and 6 is divisible by 3.
    3. Divisibility by 5:
      • A number is divisible by 5 if the last digit is either 0 or 5.
      • Example: 145 is divisible by 5 because the last digit is 5.
    4. Divisibility by 9:
      • A number is divisible by 9 if the sum of its digits is divisible by 9.
      • Example: 729 is divisible by 9 because 7+2+9=187 + 2 + 9 = 187+2+9=18, and 18 is divisible by 9.
    5. Divisibility by 11:
      • A number is divisible by 11 if the difference between the sum of the digits in odd positions and the sum of the digits in even positions is either 0 or divisible by 11.
      • Example: 121 is divisible by 11 because (1+1)−2=0(1 + 1) – 2 = 0(1+1)−2=0, and 0 is divisible by 11.

    Examples :

    Divisibility by 2:

    • a) 452 – Yes (last digit 2)
    • b) 379 – No (last digit 9)
    • c) 860 – Yes (last digit 0)
    • d) 1349 – No (last digit 9)

    Divisibility by 3:

    • a) 321 – Yes (3+2+1=63 + 2 + 1 = 63+2+1=6, 6 is divisible by 3)
    • b) 745 – No (7+4+5=167 + 4 + 5 = 167+4+5=16, 16 is not divisible by 3)
    • c) 912 – Yes (9+1+2=129 + 1 + 2 = 129+1+2=12, 12 is divisible by 3)
    • d) 484 – No (4+8+4=164 + 8 + 4 = 164+8+4=16, 16 is not divisible by 3)

    Divisibility by 5:

    • a) 135 – Yes (last digit 5)
    • b) 222 – No (last digit 2)
    • c) 405 – Yes (last digit 5)
    • d) 6780 – Yes (last digit 0)

    Divisibility by 9:

    • a) 243 – Yes (2+4+3=92 + 4 + 3 = 92+4+3=9, 9 is divisible by 9)
    • b) 546 – Yes (5+4+6=155 + 4 + 6 = 155+4+6=15, 15 is divisible by 9)
    • c) 801 – No (8+0+1=98 + 0 + 1 = 98+0+1=9, 9 is divisible by 9)
    • d) 3762 – Yes (3+7+6+2=183 + 7 + 6 + 2 = 183+7+6+2=18, 18 is divisible by 9)

    Divisibility by 11:

    • a) 253 – No ((2+3)−5=0(2 + 3) – 5 = 0(2+3)−5=0, 0 is divisible by 11)
    • b) 1331 – Yes ((1+3)−(3+1)=0(1 + 3) – (3 + 1) = 0(1+3)−(3+1)=0, 0 is divisible by 11)
    • c) 2024 – Yes ((2+4)−0=6(2 + 4) – 0 = 6(2+4)−0=6, 6 is not divisible by 11)
    • d) 341 – No ((3+1)−4=0(3 + 1) – 4 = 0(3+1)−4=0, 0 is divisible by 11)

    Divisibility by 4:

    • Rule: A number is divisible by 4 if the last two digits of the number form a number divisible by 4.
    • Example: 7328 is divisible by 4 because 28 (the last two digits) is divisible by 4.

    Divisibility by 6:

    • Rule: A number is divisible by 6 if it is divisible by both 2 and 3.
    • Example: 432 is divisible by 6 because it is divisible by 2 (last digit is 2) and by 3 (sum of digits 4+3+2=94 + 3 + 2 = 94+3+2=9 is divisible by 3).

    Divisibility by 7:

    • Rule: A bit more complex, but one method is to double the last digit, subtract it from the rest of the number, and if the result is divisible by 7, then the original number is also divisible by 7.
    • Example: 483. Double the last digit (3 ×\times× 2 = 6), subtract from the rest of the number (48 – 6 = 42). Since 42 is divisible by 7, 483 is also divisible by 7.

    Divisibility by 8:

    • Rule: A number is divisible by 8 if the last three digits of the number form a number divisible by 8.
    • Example: 5616 is divisible by 8 because 616 (the last three digits) is divisible by 8.

    Divisibility by 10:

    • Rule: A number is divisible by 10 if its last digit is 0.
    • Example: 12340 is divisible by 10 because the last digit is 0.

    Divisibility by 12:

    • Rule: A number is divisible by 12 if it is divisible by both 3 and 4.
    • Example: 144 is divisible by 12 because it is divisible by 3 (sum of digits 1+4+4=91 + 4 + 4 = 91+4+4=9 is divisible by 3) and by 4 (last two digits 44 are divisible by 4).

    Divisibility by 13:

    • Rule: A method is to remove the last digit, multiply it by 9, and subtract from the rest of the number. If the result is divisible by 13, then the original number is divisible by 13.
    • Example: For 273, remove the last digit (3), multiply by 9 (3 ×\times× 9 = 27), and subtract from the rest of the number (27 – 27 = 0). Since 0 is divisible by 13, 273 is divisible by 13.

    Divisibility by 15:

    • Rule: A number is divisible by 15 if it is divisible by both 3 and 5.
    • Example: 390 is divisible by 15 because it is divisible by 3 (sum of digits 3+9+0=123 + 9 + 0 = 123+9+0=12 is divisible by 3) and by 5 (last digit is 0).

    Divisibility by 17:

    • Rule: Remove the last digit, multiply it by 5, and subtract from the rest of the number. If the result is divisible by 17, then the original number is divisible by 17.
    • Example: For 289, remove the last digit (9), multiply by 5 (9 ×\times× 5 = 45), and subtract from the rest of the number (28 – 45 = -17). Since -17 is divisible by 17, 289 is divisible by 17.

    Divisibility by 18:

    • Rule: A number is divisible by 18 if it is divisible by both 2 and 9.
    • Example: 324 is divisible by 18 because it is divisible by 2 (last digit is 4) and by 9 (sum of digits 3+2+4=93 + 2 + 4 = 93+2+4=9 is divisible by 9).

    Divisibility by 25:

    • Rule: A number is divisible by 25 if the last two digits are 00, 25, 50, or 75.
    • Example: 4250 is divisible by 25 because the last two digits are 50.

    Divisibility by 100:

    • Rule: A number is divisible by 100 if the last two digits are 00.
    • Example: 3400 is divisible by 100 because the last two digits are 00.

    Examples :

    1. Determine if the following numbers are divisible by 4:

    • a) 132
      • Last two digits: 32
      • 32 ÷ 4 = 8 (exact division, no remainder)
      • Result: Yes, 132 is divisible by 4.
    • b) 576
      • Last two digits: 76
      • 76 ÷ 4 = 19 (exact division, no remainder)
      • Result: Yes, 576 is divisible by 4.
    • c) 891
      • Last two digits: 91
      • 91 ÷ 4 = 22.75 (not an exact division)
      • Result: No, 891 is not divisible by 4.

    2. Check divisibility by 6 for the following numbers:

    • a) 234
      • Check divisibility by 2: Last digit is 4 (even)
      • Check divisibility by 3: 2+3+4=92 + 3 + 4 = 92+3+4=9 (9 is divisible by 3)
      • Result: Yes, 234 is divisible by 6.
    • b) 455
      • Check divisibility by 2: Last digit is 5 (odd, not divisible by 2)
      • Result: No, 455 is not divisible by 6.
    • c) 672
      • Check divisibility by 2: Last digit is 2 (even)
      • Check divisibility by 3: 6+7+2=156 + 7 + 2 = 156+7+2=15 (15 is divisible by 3)
      • Result: Yes, 672 is divisible by 6.

    3. Test divisibility by 7 for these numbers:

    • a) 329
      • Last digit: 9
      • Remove the last digit and subtract twice its value from the rest: 32−(9×2)=32−18=1432 – (9 \times 2) = 32 – 18 = 1432−(9×2)=32−18=14
      • 14 is divisible by 7.
      • Result: Yes, 329 is divisible by 7.
    • b) 672
      • Last digit: 2
      • Remove the last digit and subtract twice its value from the rest: 67−(2×2)=67−4=6367 – (2 \times 2) = 67 – 4 = 6367−(2×2)=67−4=63
      • 63 is divisible by 7.
      • Result: Yes, 672 is divisible by 7.
    • c) 889
      • Last digit: 9
      • Remove the last digit and subtract twice its value from the rest: 88−(9×2)=88−18=7088 – (9 \times 2) = 88 – 18 = 7088−(9×2)=88−18=70
      • 70 is divisible by 7.
      • Result: Yes, 889 is divisible by 7.

    4. Identify if the following numbers are divisible by 8:

    • a) 2048
      • Last three digits: 048
      • 048 ÷ 8 = 6 (exact division, no remainder)
      • Result: Yes, 2048 is divisible by 8.
    • b) 4096
      • Last three digits: 096
      • 096 ÷ 8 = 12 (exact division, no remainder)
      • Result: Yes, 4096 is divisible by 8.
    • c) 1256
      • Last three digits: 256
      • 256 ÷ 8 = 32 (exact division, no remainder)
      • Result: Yes, 1256 is divisible by 8.

    5. Determine if the following numbers are divisible by 12:

    • a) 276
      • Check divisibility by 3: 2+7+6=152 + 7 + 6 = 152+7+6=15 (15 is divisible by 3)
      • Check divisibility by 4: Last two digits 76, 76 ÷ 4 = 19 (exact division)
      • Result: Yes, 276 is divisible by 12.
    • b) 432
      • Check divisibility by 3: 4+3+2=94 + 3 + 2 = 94+3+2=9 (9 is divisible by 3)
      • Check divisibility by 4: Last two digits 32, 32 ÷ 4 = 8 (exact division)
      • Result: Yes, 432 is divisible by 12.
    • c) 589
      • Check divisibility by 3: 5+8+9=225 + 8 + 9 = 225+8+9=22 (22 is not divisible by 3)
      • Result: No, 589 is not divisible by 12.

    6. Check divisibility by 25 for these numbers:

    • a) 675
      • Last two digits: 75
      • Result: Yes, 675 is divisible by 25.
    • b) 3200
      • Last two digits: 00
      • Result: Yes, 3200 is divisible by 25.
    • c) 18050
      • Last two digits: 50
      • Result: Yes, 18050 is divisible by 25.

    Short Forms

    Divisibility by 2:

    • Rule: Last digit is 0, 2, 4, 6, or 8.
    • Example: 48 (last digit 8).

    Divisibility by 3:

    • Rule: Sum of digits is divisible by 3.
    • Example: 123 (1+2+3=61 + 2 + 3 = 61+2+3=6).

    Divisibility by 4:

    • Rule: Last two digits form a number divisible by 4.
    • Example: 132 (last two digits 32).

    Divisibility by 5:

    • Rule: Last digit is 0 or 5.
    • Example: 145 (last digit 5).

    Divisibility by 6:

    • Rule: Divisible by both 2 and 3.
    • Example: 234 (even last digit and sum 2+3+4=92 + 3 + 4 = 92+3+4=9).

    Divisibility by 7:

    • Rule: Double last digit, subtract from the rest of the number, result is divisible by 7.
    • Example: 329 (32−2×9=1432 – 2 \times 9 = 1432−2×9=14).

    Divisibility by 8:

    • Rule: Last three digits form a number divisible by 8.
    • Example: 2048 (last three digits 048).

    Divisibility by 9:

    • Rule: Sum of digits is divisible by 9.
    • Example: 729 (7+2+9=187 + 2 + 9 = 187+2+9=18).

    Divisibility by 10:

    • Rule: Last digit is 0.
    • Example: 470 (last digit 0).

    Divisibility by 11:

    • Rule: Difference between sum of digits in odd positions and even positions is divisible by 11.
    • Example: 121 ((1+1)−2=0(1 + 1) – 2 = 0(1+1)−2=0).

    Divisibility by 12:

    • Rule: Divisible by both 3 and 4.
    • Example: 432 (sum 4+3+2=94 + 3 + 2 = 94+3+2=9 and last two digits 32).

    Divisibility by 13:

    • Rule: Remove last digit, multiply by 9, subtract from the rest; if divisible by 13, original is too.
    • Example: 273 (27−9×3=027 – 9 \times 3 = 027−9×3=0).

    Divisibility by 15:

    • Rule: Divisible by both 3 and 5.
    • Example: 390 (sum 3+9+0=123 + 9 + 0 = 123+9+0=12 and last digit 0).

    Divisibility by 17:

    • Rule: Remove last digit, multiply by 5, subtract from the rest; if divisible by 17, original is too.
    • Example: 289 (28−9×5=−1728 – 9 \times 5 = -1728−9×5=−17).

    Divisibility by 18:

    • Rule: Divisible by both 2 and 9.
    • Example: 324 (even last digit and sum 3+2+4=93 + 2 + 4 = 93+2+4=9).

    Divisibility by 25:

    • Rule: Last two digits are 00, 25, 50, or 75.
    • Example: 3200 (last two digits 00).

    Divisibility by 100:

    • Rule: Last two digits are 00.
    • Example: 4500 (last two digits 00).