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

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.