Control statements in Python-Conditional Statement -

Control statements in Python-Conditional Statement

Control Statement

Hey Guys, In this Blog you will learn Control statements in Python. Control statements describe the order in which statements will be execurted at runtime. Normally, python program begins with the first statement and is executed till the end.

There are 2 types of control statement
1) Conditional Statement
2) Looping Statement

1) Conditional Statement

The statement whose execution is depand on a given condition is called is called Conditional Statement.There are following types of conditional

1) if statement
2) if else statement
3) if elif else statement
4) Nested if…else
5) Logical AND Logical OR




1) if statement- If the condition is true then if statement exceuted. If the condition is false then you will see there is no output shown on output screen.

Syntax-

if <condition>:

        statement




1) WAP to check enter number is greater than 10.

num=int(input(“Enter any number”))
if num>10:
print(“Number is greater than 10”)

output

Enter any number18
Number is greater than 10

Note- If i entered less then 10 value then there is no output display on screen.So this is the problem with if statement. To overcome this problem we use if else statement

2) WAP to check whether enter number is positive or negative.

num=int(input(“Enter any number”))
if num>0:
print(num,”is Positive”)
elif num<0:
print(num,”is Negative”)
else:
print(“It is Zero”)

Output

First Run-

Enter any number15
15 is Positive

Second Run-

Enter any number -56
-56 is Negative

Third Run

Enter any number 0
It is Zero



2) if else statement- If the condition is true than if stament executed otherwise if the condition is false then else statement executed.

Syntax-

if condition:
           body of if
else:
           body of else

3) Find Biggest Value of given 2 number from the command line by using if else statement.

num1=int(input(“Enter number1”))
num2=int(input(“Enter number2″))
if num1>num2:
print(num1,” is greater than”, num2)
else:
print(num2,” is greater than”, num1)

Output

Enter number125
Enter number265
65 is greater than 25

For More Program visit on outr youtube channel by click on the below link

Youtube Channel

3) if-elif-else statement- The elif statement allow you to check multiple expression for True and execute a block of code as soon as one of the condition evaluate to True.

Syntax-

if condition:
           body of if
elif condition:
           body of elif
else:
           body of else

4) WAP to enter RUN scored by a cricket player check whether it is Triple Century, Double century, Century and Half.

r=int(input(“Enter Run Scrored by Cricket player”))
if r>=300:
            print(“Triple Century”)
elif r>=200:
           print(“Double Century”)
elif r>=100:
          print(“Century”)
elif r>=50:
         print(“Half Century”)
else:
         print(“Not a Century or Half century”)




4) Nested if…else-We can have ‘if else’ statement inside another ‘if else’ statement. This is called Nested if else statement.

Syntax-

if condition:
                   if condition:
                              statement
                  else:
                             statement
else:
                  if condition:
                             statement
                 else:
                             statement

5) WAP to check greater between 3 numbers.

a=int(input(“Enter first number”))
b=int(input(“Enter Second number”))
c=int(input(“Enter Third number”))
if a>b:
       if a>c:
            print(“a is greater”)
      else:
            print(“c is greater”)
else:
       if b>c:
             print(“b is greater”)
      else:
print(“c is greater”)

Output

Enter first number56
Enter Second number2
Enter Third number41
a is greater

Complex Condition




 

5) Logical AND- 

syntax-

if condition 1 and condition 2 and condition 3 and……:
                                  body of if
else:
                                  body of else




6) WAP to check greater between 3 numbers.

a=int(input(“Enter the value of a”))
b=int(input(“Enter the value of b”))
c=int(input(“Enter the value of c”))
if a>b and a>c:
          print(“a is greater”)
if b>a and b>c:
         print(“b is greater”)
if c>a and c>b:
         print(“c is greater”)

Output

Enter the value of a98
Enter the value of b123
Enter the value of c34
b is greater

6) Logical AND-

Syntax

if condition 1 or condition 2 or condition 3 or……:
                   body of if
else:
                   body of else

7) WAP to check whether enter character is vowel or consonant

ch=input(“Enter any character”)
if ch==’a’ or ch==’A’ or ch==’e’ or ch==’E’ or ch==’i’ or ch==’o’ or ch==’u’:
       print(“It is VOWEL”)
else:
print(“It is Cononant”)

Output

Fisrt Run

Enter any character i
It is VOWEL

Second Run
Enter any character z
It is Cononant

Thanks For Reading the blog also share with your friends. I hope you will understand the topic Control statements in Python. For More Classes subscribe our youtube channel

OlevelGuruji

Python Operator and its types

Top 10 Python Programs

.

Leave a Comment