Tricentis Tosca 16.0 Released on Feb-2023 ----- UFT has been upgraded from UFT 15.0.1 to UFT One 15.0.2, Beginning at November 2020.

Thursday 25 March 2021

Conditional Statements in Python

Conditional statements

In python, decision making is performed by the following statements.

If-statement - The if statement is used to test a specific condition. If the condition is true, a block of code(If-block) will be executed.

IF-else Statement - The if-else statement is similar to if statement except the fact that, if also provides the block of the code for the false case of the condition to be checked. If the condition provided in the if statement is false, then the else statement will be executed.

Nested if statement - Nested if statements enable us to use if ? else statement inside an outer if statement. 

If Statement 

The if statement is used to test a particular condition and if the condition is true, it executes a block of code known as If-block. The condition of if statement can be any valid logical expression which can be either evaluated to true of false.

Syntax

If expression:
   statement

Example 1:

num=int(input("enter the number"))
if num%2==0
   print("Number is even")

Output:

enter the number 10
Number is even

Example 2:

a= int(input("Enter a value"))
b= int(input("Enter b value"))
c= int(input("Enter c value"))

if a>b and a>c:
   print(" a is largest");
if b>a and b>c:
   print(" b is largest");
if c>a and c>b:
   print("c is largest");

Output:

Enter a value 1000
Enter b value 2000
Enter c value 3000
c is largest

If-Else Statement :

The If -else statement provides an else block combined with the if statement which is executed in the false case of the condition. If the condition is true, then the if-block is executed, otherwise, the else-block is executed.

Syntax -

If condition:
   #block of statements
else
   #another block of statements(else-block)

Example - Program to check whether a person is eligible to vote or not.

age=int(input("Enter your age"))

if age>=18:

   print("you are eligible to vote")

else

   print("sorry you have to wait")

Output :

Enter your age 20
you are eligible to vote

Example : Program to check whether a number is even or not.

num= int(input("enter the number"))
if num%2==0
   print("Number is even..")
else
   print("Number is odd..")

Output -

enter the number 20
Number is even

elif Statement :

The elif statement enables us to check multiple conditions and execute the specific block of statements depending upon the true condition among them. We can have any number of elif statements in out program depending upon our need. However, using elif is optional.

The elif statement works like an if-else-if ladder statement in C. It must be succeeded by an if statement.

Syntax :

If expression 1:
   #block of statements
elif expression 2:
   #block of statements
elif expression 3:
   #block of statements
else:
   #block of statements

Example 1:

number = int(input("Enter the number"))
if number == 10:
   print("number is equals to 10")
elif number == 50
   print("number is equal to 50")
elif number == 100
   print("number is equal to 100")
else
   print("number is not equal to 10, 50 or 100")

Output:

Enter the number 20
number is not equal to 10, 50 or 100

Example 2:

marks = int(input("Enter the marks"))
if marks > 85 and marks <= 100:
   print("scored grade A")
elif marks > 60 and marks <= 85:
   print("Score grade B+")
elif marks > 40 and marks <= 60: 
   print("Scored grade B")
elif marks >30 and marks <=40:
   print("scored grade C")
else:
  print("fail")

No comments:

Post a Comment

Note: only a member of this blog may post a comment.