Tag: Programming Languages

  • 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!
  • Importance of Machine Learning

    What is Machine Learning?
    Machine learning is a type of technology that enables the computer to learn from the data and make a decision or predict without explicitly being programmed for it. Simple words, teaching a computer to learn from examples and experiences.

    Why Learn Machine Learning?
    An Enticing Technology: Machine learning stands at the core of most cool technologies in use today, such as movie recommendations on Netflix and voice assistants like Siri.

    Future Jobs: Most of the future jobs will be based on machine learning. So, you can create a career in technology, health, and finance by learning now.

    Solving Problems: Machine learning resolves complicated problems by analyzing large data. It will find out the patterns and make decisions much faster and more accurately than human beings.

    Innovation: The art of being part of new technologies and improvements in old ones, which really make a difference in the world.

    Understanding of Modern Tech: Knowing the inner machinery of machine learning gives insight into how modern technology works and makes a living.

    Put simply; this is teaching a computer to learn from data. In this way, it opens many interesting opportunities and innovations that will be realized in the future.

    Examples :

    ConceptExample
    RecommendationNetflix
    Speech RecognitionSiri
    Spam FilteringGmail
    Image RecognitionGoogle Photos
    Predictive TextSmartphone
    Self-DrivingTesla
    Medical ImagingX-rays
    Fraud DetectionBanking
    PersonalizationAmazon
    Weather ForecastForecasting