0% found this document useful (0 votes)
9 views3 pages

IF Statements

if statement notes python

Uploaded by

bruintjiesivhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views3 pages

IF Statements

if statement notes python

Uploaded by

bruintjiesivhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Conditional Statements:

IF Statement:
Python If statement is a conditional statement wherein a set of statements is executed based on the
result of the condition.

If condition is true (if Boolean is true) the program executes a set of statements but if the
condition is false, the program ignores the statement(s) and moves onto the next part of the
program.
The syntax for the IF statement is:

if boolean_expression:
statement(s)

Two things to note:

1) the colon : after boolean expression.


2) The indentation – this is important as all the statements in the indentation are executed by
PYTHON.
Example:

a=2
b=5

if a<b:
print(a, 'is less than', b)

The output would be:

2 is less than 5
Example 2:

a = 24
b = 5

if a<b:
print(a, 'is less than', b)

Output

blank

The condition provided in the if statement evaluates to false, and therefore the statement inside the if
block is not executed.

Example 3:

a = 2
b = 5
c = 4

if a<b and a<c:


print(a, 'is less than', b, 'and', c)

Example 4: Nested If
You can write a Python If statement inside another Python If statement. This is called nesting.

a = 2
if a!=0:
print(a, 'is not zero.')
if a>0:
print(a, 'is positive.')
if a<0:
print(a, 'is negative.'
Example 5:
Find Smallest of Three Numbers using IF

We shall follow these steps to find the smallest number of three.

1. Read first number to a.


2. Read second number to b.
3. Read third number to c.
4. If a is less than b and a is less than c, then a is the smallest of the three numbers.
5. If b is less than a and b is less than c, then b is the smallest of the three numbers.
6. If c is less than a and c is less than b, then c is the smallest of the three numbers.

Python Program

a = int(input('Enter first number : '))


b = int(input('Enter second number : '))
c = int(input('Enter third number : '))

smallest = 0

if a < b and a < c :


smallest = a
if b < a and b < c :
smallest = b
if c < a and c < b :
smallest = c

print(smallest, "is the smallest of three numbers.")

Output

You might also like