Python Basic Tutorials

Python is a high level general purpose programming language which has a clear/easy learning curve. Python programming language is massively used in various domains like Artificial Intelligence, Data Science, Web Development, Utilities Tools and Scripts and many more

Python is a Programming language, and the default Python we use is written in C programming language which is also referred to as CPython (Python implementation in C). There are various implementation of Python Programming language i.e Jython(in JAVA), Skulpt(in JS) e.t.c

To make everything easy: We refer CPython as Python

Python if/else Statement

If-else statements are basically the decision-making control flow statements. They are also called conditional statements. Block of the code in different sections is executed based on the satisfaction of the condition.

Type of if-else statements are:

  • python if statements

    if statements contain one conditional expression and block of code in its body. If the condition is satisfied then its body gets executed.

    Syntax
    
    if conditional expression:
        #body
    Example:
    
    name=input("Enter your name: ")
    if name=='harry'
       print("He is handsome")
    Output
    
    Enter your name: harry
    He is handsome.
  • Python if-else statements

    In these conditional statements If statements contain one conditional expression and block of code in their body as well as in the else block. Then if the if-block condition satisfies then the if-block's code is executed otherwise else block's code is executed.

    Syntax
    
    if conditional expression:
        #if block body
    else:
        #else block body
    Example
    
    name=input("Enter your name: ")
    
    if name=='harry':
       print("He is handsome")
    else:
       print("He is not more handsome")
            
    Output
    
    Enter your name: harry
    He is handsome
  • python if-elif-else statements

    In these conditional statements If statements contain more than one conditional expression and more than one block of code in their body. The else block contains the else block code in its body. Then if the if-block condition satisfies then the if-block's code is executed otherwise it checks for all the elif-block's conditions if any elif block satisfies the condition its block's code is executed otherwise else block's code is executed.

    Syntax
    
    if test expression:
        #Body of if
    elif test expression:
        #Body of elif
    else:
        #Body of else
    Example
    
    num=int(input("Enter Number: "))
    if num>0:
        print("Number is positive")
    elif num<0:
        print("Number is Negative")
    else:
        print("Number is zero")
    Output
    
    Enter Number: -3
    Number is Negative
  • Python Nested if-else statements

    This control statement is similar to the above if-else statements, but the difference is there can be multiple if-else statements inside the if-else statement itself.

    Example
                
    #Program to check greatest between 3 numbers
    num1=2
    num2=3
    num3=4
    
    if num1>num2:
        if num2>num3:
            print("num1 is greatest")
        else:
            if num1>num3:
                print("num1 is greatest")
            else:
                print("num3 is greatest")
    elif num2>num3:
        print("num2 is greatest")
    else:
        print("num3 is greatest")
    Output
    num3 is greatest