Python if statement

Python if statement

Decision making is an essential feature of any programming languages. Condition checking is the strength of decision making. Python provides many decision-making statements, such as:

  • If statement
  • If-else statement
  • elif statement

if statement

The if statement is the easiest conditional statement. It is used to test a particular condition. If the condition is true, a block of code (if statement block) will be executed.

Syntax:

Flow chart of if statement: The flowchart of if statement is following

Flow chart of if statement

In the above flowchart, First it checks the condition if the condition is true, then conditional code(if statement) will be executed. If the condition is false, then first set of code after the end of the ‘if’ statement will be executed

Python programming language supposes any non-zero and non-null values as TRUE. If it is zero, then it is assumed as FALSE values.

Indentation in Python

Indentation is used to declare the block of code. All the statements of one block are intended at the same level indentation. Python doesn’t allow the use of curly braces or parentheses for block-level code. Indentation makes programming simple and easy in Python.

Example: There are following the example of if statement

1. Write a program to check number is even

Output:

2. Write a program to check the weight of the passenger’s luggage

Output:

3. Write a program to print the largest number of the three numbers

Output:

4. Write a program to check the given number is positive or negative

Output: