Home » Python

if keyword with example in Python

Python if keyword: Here, we are going to learn about the if keyword with example.
Submitted by IncludeHelp, on April 14, 2019

Python if keyword

if is a keyword (case-sensitive) in python, it is used to define an "if statement" block that executes, if a given condition is true. if keyword is a part of conditional statements in python programming languages and it is used with the various type of if-else conditional statements.

Syntax of if keyword

    if test_condition:
	    statement(s)

Here, if test_condition is True, then statement(s) will be executed.

Example:

    Input:
    num = 0

    # condition
    if num==0:
        print("Yes! ", num, " is equal to 0")

    Output:
    Yes!  0  is equal to 0

Python examples of if keyword

Example 1: Check whether a given number is equal to 0.

# python code to demonstrate example of 
# if keyword 

# Check whether a given number is equal to 0

# number 
num = 0

if num==0:
    print("Yes! ", num, " is equal to 0")

Output

Yes!  0  is equal to 0

Example 2: Check whether a given number is positive, negative or zero (using if keyword only).

# python code to demonstrate example of 
# if keyword 

# Check whether a given number is
# positive, negative or zero

# number 
num = 20

if num>0:
    print(num, " is positive")

if num<0:    
        print(num, " is negavtive")

if num==0:
    print(num, " is zero")

Output

20  is positive	


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.