if-statements 


Until now, we have performed sequential execution of instruction, in which all the instruction is executed in the given order. We can perform conditional execution of instructions in the python programming language, by using if-statements. For example: we can compare two given numbers to find the largest number among these two. Such types of situation in programming requires conditional execution of the statement. The most basic form of conditional statement is if-statement.

if-statement:

The general form of if-statement is:

if(condition):
   statement1
   statement2
   .
   statement n

in the above syntax:
- condition is any relational or logical expression that evaluates to Boolean result
- in python, the statement beloning to given scope are grouped using the same number of identation / spaces, 
in the above case, 
(statement 1, statement 2...statement n) belongs to the scope of if-statement. 
hence these set of statements are called if-block
- The if-block will be executed only when given condition evaluates to True. 
if result if False, then if-block will not be executed.

python code: Ask user to enter two numbers find the largest number among them.

# python code: Ask user to enter two numbers find the largest number among them.
a = int(input('Enter a number: '))
b = int(input('Enter a number: '))

if(a>b):
  print(f'{a} is largest')

Explanation:

in the above code, after the user input for variable a and b.
if(a>b):
  print(f'{a} is largest')

will be executed,

- first the condition (a>b) is evaluated. let us say: a=2 and b=3. Then (a>b) will result False. so statement of if-block will not be executed
- now assume a=10 and b=5, the (a>b) will evaluates to True, and the if-block will be executed, we will get output as:
10 is largest

python code: Ask user to enter a number and check whether the given number is even.

num = int(input('Enter a number to check:'))

if (num%2==0):
  print(f'{num} is even')

If- else statements 


The if-block of simple if-statement is executed only when the condition evaluates to True, if the condition evaluates to False, then if-block is simply ignored and skipped. Thus, simple if-statement does not handle the False statement. In python, we can execute a separate set of statements when the condition is False using if-else statements. The general form of if-else statement is as:

if(condition):
   statement 1
   statement 2
else:
   statement a
   statement b

Working:
- First condition is evalulated. 
- if condition is True, if-block is executed
- otherwise, if-block is skipped and only else-block is executed.

Note: All the statement of if-block and else-block should have same identation level. 
Always make sure if and else keyword are at same alignment as above.

   

Write a python code: to find largest number among two given numbers:

# python code: Ask user to enter two numbers find the largest number among them.
a = int(input('Enter a number: '))
b = int(input('Enter a number: '))

if(a>b):
  print(f'{a} is largest')
else:
  print(f'{b} is largest')

Write a python code: to find whether the given number is even or odd.

num = int(input('Enter a number to check:'))
if (num%2==0):
  print(f'{num} is even')
else:
  print(f'{num} is odd')

if- elif- else statements 


In a programming problem, most of the time we encounter situations where we need to handle more than one condition. Multiple conditions can be handled in python using if-elif-else statement as follows:

if(condition1):
   statement
   statement
   .
   .
   statement
elif(condition2):
   statement
   statement
   .
   .
   statement
elif(condition3):
   statement
   statement
   .
   .
   statement

...

else:
   statement
   statement
   .
   .
   statement




---
working:
- First condition1 is evaluated, if it is True, the only the if-block is executed other will be simply ignored and skipped.
- if condition1 is False, the condition2 is evaluated, if it is True, only first elif-block is executed and remaining is skipped.
- now if condition2 is false, then condition3 is evaluated in the similar way.
- if all the condition above is False, then by default- else-block will be executed.


Write python code: for the given number whether it is positive, negative or zero

# Write python code: for the given number whether it is positive, negative or zero
num = int(input('Enter a number to check:'))
if(num<0):
  print('Negative')
elif(num>0):
  print('Postive')
else:
  print('you entered zero')

Using  relational and logical operators, complex conditions can be created as follows:

Write python code: find out largest number among three given numbers

# find out largest number among three given numbers
a = int(input('Enter a number to check:'))
b = int(input('Enter a number to check:'))
c = int(input('Enter a number to check:'))

if((a>b) and (a>c)):
  print(f'Largest: {a}')
elif((b>a) and (b>c)):
  print(f'Largest: {b}')
else:
  print(f'Largest: {c}')