Wednesday, 12 November 2025

Python Tutorial: Conditional Statements

Python Tutorial: Conditional Statements

Python is a smart language , compare to other programing language python code is so smart that it makes a compact and meaningful understanding of code with a good coading structure. The syntax of python is easy for both beginner and expert level programmer. Python does not extra opening and closing braket ({}) , the number of line reduce .In this chapter we will discuss about conditional statement in Python . Conditional statement means if,else,elseif. First you will know when if is required. If we have a condition and if the condition satisfy the come code segment will be executed.


1. The if Statement
Run code only if a condition is True . If condition is on executed only the statement is true , here the age is over 18  then they statement will be executed.


age = 18
if age >= 18:
    print("You are an adult!")

Output
You are an adult!


2. The else Statement
In case of else , else statement is executed only if the "if" statement fails or return false.

age = 16
if age >= 18:
    print("You are an adult!")
else:
    print("You are a minor.")

Output

You are a minor.

3. The else if Statement (else if)

"elseif" only executed when the if statement does not and the next condition satisfy "elseif" .  "elseif" does not execute automatically , if statement and previous all condition fail and "elseif" condition satisfy ,  then only "elseif" statement executed. 

score = 85

if score >= 90:
    print("Grade: A")
elif score >= 80:
    print("Grade: B")
elif score >= 70:
    print("Grade: C")
elif score >= 60:
    print("Grade: D")
else:
    print("Grade: F")

Output

Grade: B


4. Nested if Statements

In python , if statement can contain another if statement , that is called nexted if , here is the example of nexted if statement.


age = 20
citizen = True
if age >= 18:
    if citizen:
        print("You can vote!")
    else:
        print("You are old enough but not a citizen.")
else:
    print("You are too young to vote.")


Output:
You can vote!

5. Comparison Operators


Operator   Meaning
==               Equal to
!=                Not equal to
>                 Greater than
<                 Less than
>=              Greater than or equal
<=              Less than or equal

6. Logical Operators

Operator    Meaning                  Example
and            Both true                 x > 5 and x < 15
or              At least one true     x < 5 or x > 15
not            Reverse truth          not(x == y)


Example of Comparison Operators

x = 10   y = 5

if x > y:

    print("x is greater")


Logical Operators

temperature = 25

is_sunny = True

if temperature > 20 and is_sunny:

    print("Perfect day for a picnic!")


Prepared By :Ayan Banerjee




Thursday, 6 November 2025

Python Tutorial : function

Python Tutorial : function

Python is one of the smartest programming language in modern programming world ,  probably Python contain the most advance libraries among the other programming language. Python is very popular for both backend , frontend and embedded software design and artificial intelligence software design. Python on does not contain only inbuild library , many third party provider several library to support Python for more advance work.

Today,  in this tutorial we will learn about function in Python. First we have to learn what is function  ? 

    A function is a block of code that have some specific task and objective , that means when we call a function ,the block code will be executed and it will return some value. The value must may be image, video , interger , string  everything. 

In other language , function starts and close with opening and closing braket( { }) ,but Python is very smart coding language , it handle opening and closing of the function in a smart way so that the function is compact, length is small.

Here is a smaple function  

def HellowWorld():
       print("Hello! Welcome to Python functions.")


def → defines the function
HellowWorld → function name
() → parentheses (for parameters)

How to Call function

Calling function in Python is very easy ,  just put the name with parameter.

HellowWorld()

Sigle Parameter Function

def HellowWorld(name):
    print(f"Hello, {name}! Nice to meet you.")

Calling :HellowWorld("Ayan") 
Output:Sum: Ayan ! Nice to meet you.


Multi Parameter Function

def add(a, b, c):
    result = a + b + c
    print("Sum:", result)

Calling:add(10, 20, 30)
Output:Sum: 60

Function Returns Multiple Values
Python can return multiple return values , this is simple and just like same method calling simple function.First you have to write all return variable and then call the functions .There is the simple example


def calculate(a, b):
    add = a + b
    sub = a - b
    return add, sub

Calling : 

x, y = calculate(10, 5)

print("Sum:", x)
print("Difference:", y)


Output:
Sum: 15
Difference: 5


Functions Can be called Together
Multiple function can be called together , first call the function which need to call first and then on.Here is the example of the same


def get_input():
    a = int(input("Enter first number: "))
    b = int(input("Enter second number: "))
    return a, b


def add(a, b):
    return a + b


def main():
    x, y = get_input()
    result = add(x, y)
    print("Sum is:", result)


Calling :main()


Nested Function
Nested function meaning function under a function .Multiple nested function can be nested under a main function.We do not need to call inner function , we just need to call outer function, inner function will be called automatically.

def outer_function():
    print("Outer function started.")

    def inner_function():
        print("Inner function executed.")

      inner_function()  # call inner function inside outer


Calling :outer_function()

Returning Inner Function

You can return the inner function from the outer function  , this is called a closure.

def outer_function():
    def inner_function():
        print("Hello from inner function!")
    return inner_function  # returning the function, not calling it


Calling :
greet = outer_function()
greet()  

Output:

Hello from inner function!

                 Prepared By: https://www.sonjukta.com/About.html

Python Tutorial: Conditional Statements

Python Tutorial: Conditional Statements Python is a smart language , compare to other programing language python code is so smart that it ma...