Conditional Statements in Python

Conditional Statements in Python



What is use of conditional statements: 

Conditional statements are used to compare two or more than two variables and then make decisions. These statements are used in decision making operations.

Following are conditional statements in Python ,

1.    if Statements:

if statement is used in Python for decision making.
Syntax: 
            if test expression:
               statement(s)  
  
here compiler evaluates the expression only if condition becomes true.
if condition becomes false then statements will not get execute.
For Example:
1. num=100 
if num> 101:
    print(num,"Number is positive")

OutPut:
10 Number is positive

2. num=20
if num%2!=0:
    print(num,"Is odd number")

O/P: will not display any thing because condition becomes false.

2.    if-else block

Syntax: 
          if test expression:
        body of if
          else:
          body of else 


 if-else statement evaluates test expression and execute the body of if only when condition becomes true.
 the body of else block will get execute when condition becomes false.

# Program to display number is odd or even
print("Demonstration of if-else statement")
num=11 
if num%2==0:
    print(num,"is even")
else:
        print(num,"is odd")

o/p: Demonstration of if-else statement
11 is odd


# Program to display number is odd or even take user input
print("Demonstration of if-else statement")
num=int(input("Enter any number"))
if num%2==0:
    print (num,"is even")
else:
        print(num,"is odd")


o/p:
Demonstration of if-else statement
Enter any number23
23 is odd



# leap year or not
year=int(input("Enter year"))
if year%4==0:
    print(year,"Year is leap")
else:
    print(year,"Year is not leap")

o/p:
Enter year2020
2020 Year is leap



# Check count of odd and even numbers in list
list1=[2,10,3,4]
even, odd=0,0
for num in list1:
    if num%2==0:
        even+=1 
    
else:
    odd+=1 
    print("lList elemets are", list1)
    print("The list of even numbers in list",even)
    print("The list of odd numbers in list",odd)
    
o/p:
lList elemets are [2, 10, 3, 4]
The list of even numbers in list 3
The list of odd numbers in list 1


3.    if- elif-else

Syntax:
        
          if test condition:
          body of if
           elif test expression:
           body of elif
          else:
          body of else


The elif is short for else-if.
It helps to check multiple expression.
If the condition for if is false then compiler will check elif block for next condition
and so on
If all condition becomes false the body of else block will get executed.
Only one block among several block get executed according to the condition
The if block can have only one else block but can have multiple elif blocks.

# Demonstration of if-elif-else statements
num=int(input("Enter number"))
if num>0:
    print(num," is positive")
elif num==0:
        print(num,"is zero")
else:
    print(num,"Negative number")
o/p:
Enter number1
1 is positive

# number is smaller,bigger or equal
num1=int(input("Enter Number"))
num2=int(input("Enter second number"))
if num1>num2:
    print(num1,"is bigger")
elif num1<num2:
    print(num2,"is bigger")
else:
    print(num1,num2," are equal")
o/p:
Enter Number12
Enter second number12
12 12 are equal

4.    Nested If Statement

Syntax:

         if test expression: 
         statement
         if test expression:
         statement
         else:
         statement
         else:
         statement

You can have if statements inside the if statements.this is called nested if statement.
         


टिप्पणी पोस्ट करा

1 टिप्पण्या

कृपया तुमच्या प्रियजनांना लेख शेअर करा आणि तुमचा अभिप्राय जरूर नोंदवा. 🙏 🙏