Monday, 23 January 2017

CONDITIONAL STATEMENTS IN PYTHON

Conditional Statements are used to decide whether code has to be execute (or) skip based on the evaluation of the condition. After evaluating the condition, it should return either True or False value.

Elements of conditional statements

In conditional statements we use two elements -

1. Condition - Any expression which returns either True or False value after evaluating that expression is known as "condition"

2. Block - All the statements which are following same space indentation is known as "Block". Block begins with when the indentation increases. Block can contain other blocks. Blocks end when the indentation decreases to zero or to a containing blocks indentation.

Python supports 3 conditional statements. They are -

1. if (or) simple if
Syntax -
if condition : statement
              or
if condition :
        statement1
        statement2

Simple if executes the block if condition returns True. Otherwise, it will skip the execution of the block.

Example 1 -
Name=raw_input('enter name : ')
if Name=='Rakesh':
    print("Hii, Rakesh")
print('Hello, world')

Output 1-
enter name : Rakesh
Hii, Rakesh
Hello, world
Output 2-
enter name : vivek
Hello, world


2. if - else
Syntax -
if condition :
        statement1
        statement2
        ------------
        ------------
else :
        statement1
        -------------

In if-else condition returns True then it will execute if block otherwise it will execute else block.

Example 1 -
Name=raw_input('enter name : ')
if Name=='Rakesh':
    print("Hii, Rakesh")
else :
    print("Hii Stranger")
print('Hello, world')

Output 1 -
enter name : Rakesh
Hii, Rakesh
Hello, world
Output 2 -
enter name : vivek
Hii Stranger
Hello, world


3. elif
Syntax -
if condition :
          statement1
          statement2
          -------------
          -------------
elif condition :
          statement1
          statement2
          ------------
else :
          statement1
          statement2
          ------------

Example 1 -
Name=raw_input('enter name : ')
age=input('enter age : ')
if age<15 :
    print Name,' you are a kid'
elif age<40 :
    print Name,' you are young'
elif age<100 :
    print Name,' you are old'
else :
    print Name,' you are alien'
print 'welcome to codingmakesperfect.blogspot.in'

Output 1 -
enter name : rakesh
enter age : 12
rakesh  you are a kid
welcome to codingmakesperfect.blogspot.in
Output 2 -
enter name : rakesh 
enter age : 26 
rakesh you are young 
welcome to codingmakesperfect.blogspot.in


WRITE A PROGRAM TO FIND BIGGEST NUMBER AMONG 2 NUMBERS
num1=input('enter num1 : ')
num2=input('enter num2 : ')
if num1>num2 :
    print num1,' is big'
else :
    print num2,' is big'

Output 1 -
enter num1 : 15 
enter num2 : 6 
15 is big
Output 2 -
enter num1 : 50 
enter num2 : 70 
70 is big


WRITE A PROGRAM TO FIND BIGGEST NUMBER AMONG 3 NUMBERS
num1=input('enter num1 : ')
num2=input('enter num2 : ')
num3=input('enter num3 : ')
if num1>num2 and num1>num3 :
    print num1,' is big'
elif num2>num3 :
    print num2,' is big'
else :
    print num3,' is big'

Output -
enter num1 : 30 
enter num2 : 20 
enter num3 : 40 
40 is big


WRITE A PROGRAM TO FIND SMALLEST OF 3 NUMBERS
num1=input('enter num1 : ')
num2=input('enter num2 : ')
num3=input('enter num3 : ')
if num1<num2 and num1<num3 :
    print num1,' is small'
elif num2<num3 :
    print num2,' is small'
else :
    print num3,' is small'

Output -
enter num1 : 10 
enter num2 : 20 
enter num3 : 40 
10 is small

No comments:

Post a Comment