This page topics close

If statements is a statement which is used to test any condition and if the condition is True then executes True block.

  • Syntax of using if statement

    (if expression/condition) :
                   //statements to execute when condition become true 
    

    Syntax of using if statement
    Example)
    drag_handle
     
    x=10
    if x==10:
    	print("value of x is 10")
    Output:
    value of x is 10

    Example of if

    
    # taking input from user
    # input()-function is used to take input 
    var=input("enter your name ")
    if var=="ram":
    	print("Hello , Welcome")
    

    Output:
    enter your name ram
    Hello , Welcome

    input()- This function is used to take input from user.


    Example : expression in if

    
    # we can use expression directly inside the if condition
    # example :
    x=3
    y=5
    z=60
    if(x+y+z):
    	print("Yes true block")

    Output:
    Yes true block

    Example: string in if statement

    
    # we can pass string into if condition 
    x="hey , this is python"
    if(x):
    	print("This is true block ...")

    Output:
    This is true block ...

    Example: integer and float type variable in if statement

    
    # we can also pass integer and float values in if
    x=34
    y=45.54
    if(x):
    	print("integer type variable in if")
    
    
    if(y):
    	print("float type variable in if")

    Output:
    integer type variable in if
    float type variable in if

What next?

The next topic is Python If else Statements

Share this page