×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

Python if Keyword

By IncludeHelp Last updated : December 07, 2024

Description and Usage

The 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

Syntax of if keyword:

if test_condition:
  statement(s)

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

Sample Input/Output

Input:
num = 0

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

Output:
Yes!  0  is equal to 0

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.