0% found this document useful (0 votes)
43 views

Python If Conditional Statement

The document discusses Python's if conditional statement. It provides the syntax of an if statement in Python and shows a pictorial representation of its working. An example is given that checks if the product of two numbers is equal to 30. It prints a statement if the condition is true. The document emphasizes the importance of indentation in Python to determine the code block for the if statement. It also shows an IndentationError that occurs if the indentation is incorrect.

Uploaded by

ANTONY
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

Python If Conditional Statement

The document discusses Python's if conditional statement. It provides the syntax of an if statement in Python and shows a pictorial representation of its working. An example is given that checks if the product of two numbers is equal to 30. It prints a statement if the condition is true. The document emphasizes the importance of indentation in Python to determine the code block for the if statement. It also shows an IndentationError that occurs if the indentation is incorrect.

Uploaded by

ANTONY
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Python If Conditional Statement

Python If Conditional Statement

Python If Conditional Statement – Basic idea of an if condition statement is that, when the condition is satisfied,
the statements in the if block are executed, otherwise not.

Syntax – Python IF
if <condition> :
statements

A pictorial representation of the working is given below :

Python If Conditional Statement

Example 1 – Python If
In this example, we will write an if statement. The condition checks if product of the two numbers is 30.

example.py – Python Program


number_1 = 5
number_2 = 6

# observe the indentation for the statements in the if block


if number_1 * number_2 == 30:
print("The condition is true.")
print("Inside if condition block")
print("Statement in if loop")

print("Outside if condition block.")

Output

The condition is true.


Inside if condition block
Statement in if loop
Outside if condition block.

Note : Observe the indentation or alignment of the statements inside the if-block. Unlike programming languages
like java or c where the statements are enclosed in curly braces, python considers the alignment of statements
to consider them in the block.

If the alignment is missed out as shown in the below program, interpreter throws IndentationError.

example.py – Python Program

number_1 = 5
number_2 = 6

# observe the indentation for the statements in the if block


if number_1 * number_2 == 30:
print("The condition is true.")
print("Inside if condition block")
print("statement in if loop")

print("Outside if condition block.")

Output

File "/home/arjun/PycharmProjects/PythonTutorial/conditional/ifCondition.py", line 7


print("Inside if condition block")
^
IndentationError: unexpected indent

Conclusion
In this Python Tutorial, we have seen the syntax of if statement in python, a pictorial representation of working
of if statement, an example and how the indentation of the statements in an if block should be, and
IndentationError if we miss out the already discussed indentation.
Python Programming

⊩ Python Tutorial

⊩ Install Python

⊩ Install Anaconda Python

⊩ Python HelloWorld Program

⊩ Python Variables

⊩ Python Variable Data Type Conversion

⊩ Python Comments

Control Statements

⊩ Python If

⊩ Python If Else

⊩ Python While Loop

⊩ Python For Loop

Python String

⊩ Python String Methods

⊩ Python String Length

⊩ Python String Replace

⊩ Python Split String

⊩ Python Count Occurrences of Sub-String

⊩ Python Sort List of Strings

Functions

⊩ Python Functions

Python Collections

⊩ Python List

⊩ Python Dictionary

Advanced

⊩ Python Multithreading

Useful Resources

⊩ Python Interview Questions

You might also like