Conditional Statements

M3-R5.1 · Chapter 4: Operators, Expressions and Python Statements · 5 min read

Conditional Statements 

Conditional statements allow you to make decisions in your Python programs — like saying "if this happens, do that; otherwise, do something else."

OR

Conditional statements in Python allow the execution of different blocks of code based on specific conditions.

OR

Decision making is about deciding the order of execution of statements based on certain conditions.

Decision structures evaluate multiple expressions which produce TRUE or FALSE as outcome.

   Types of Conditional Statements:

There are basically 4 types of decision-making statements. Here is the list of all those four decisions making statements:

  1. if Statement
  2. if...else Statement
  3. elif OR if...elif...else Statement
  4. Nested if Statements

1. If Statement :- 

it Executes (should do something) a block of code if a specified condition is true. It First checks a condition — if it's True, it runs a block of code and If it's False, it skips that block.

Basic Syntax:

if condition:

    # code block that runs if condition is True

NOTE:

  • Always end the if line with a colon :.
  •  Always indent the code inside (usually 4 spaces).

Part

Meaning

if

Checks if a condition is True

: colon

Tells Python that a block starts

Indentation

Shows Python what code to run

Example:1

x = 10

if x > 5:

    print("x is greater than 5")

age = 18

if age >= 18:

    print("You are an adult")

Example 1: A shop will give discount of 10% if the cost of purchased Items is more than 1000. Accept purchase quantity of an item and cost of one item by keyboard so print total cost for user.

quan = int(input('Enter first number: ') )

rate = int(input('Enter second number: ') )

pamt=quan*rate

if(pamt>1000):

    dis=pamt*0.1

    total=pamt-dis

    print("Discount: ", discount)

    print("Total: ", total)

2. If - else Statements :-

The if-else statement in Python allows a program to make a decision based on a condition.

It executes one block of code if the condition is true and another if the condition is false.

Syntax - 

if condition:

    # runs if True

else:

    # runs if False

Example - 

marks = 45
if marks >= 50:
    print("Pass")
else:
    print("Fail") 

Example 1: Write a program to Check a number is positive or negative

x = 10

if x > 0:

    print("x is positive")

else:

    print("x is zero or negative")

Example 2: Write a program to get age and check he is adult or not

age = int(input(“plz enter the age of person”))

if age >= 18:

    print("You are an adult.")

else:

    print("You are a minor.")

Example 3: Write a program to accept price and quantity of item from Keyboard if purchase amount is greater than 1000 than give 10% discount  and print the payable amount otherwise print the purchased amount .

price=int(input("Enter Price: "))

qty=int(input("Enter Quantity: "))

amt=price*qty

if amt>1000:

    print ("10% discount is applicable")

    discount=amt*10/100

    amt=amt-discount

    print ("Amount payable: ",amt)

else:

    print (" Your are not eligible for 10% discount ")

    print ("Amount payable: ",amt)

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

num = int(input("enter the number:"))        

if (num%2 == 0):     

    print("The Given number is an even number")   

else:   

    print("The Given Number is an odd number")

Example 5: Program to check whether a person is eligible to vote or not

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

if (age>=18):   

    print("Eligible to vote !!");   

else:   

    print("Not Eligible to vote !!");  

 Example 6: Program to the larger number in two numbers.

num1 = int(input("plz enter the first no"))

num2 = int(input("plz enter the Second no"))

if (num1>num2):

    print("\n First Number is Largest Number =", num1)

else:

    print("\n Second Number is Largest Number =", num2)

Example 7: Program to check first number  is divisible by second number.

num1 = int(input("Enter first number : "))

num2 = int(input("Enter second number : "))

rem = num1%num2

if rem == 0:

    print ("First number is divisible by second number")

else:

    print ("First number is not divisible by second number")