Thursday , December 26 2024
Python Decision Making - if-else statement

Python Decision Making

Task 1 – Conditional Statements

Python Decision Making

Python supports the usual logical conditions from mathematics:

  • Equals: a == b
  • Not Equals: a != b
  • Less than: a < b
  • Less than or equal to: a <= b
  • Greater than: a > b
  • Greater than or equal to: a >= b

These conditions can be used in several ways, most commonly in “if statements” and loops.
An “if statement” is written by using the if keyword.

Python relies on indentation (whitespace at the beginning of a line) to define scope in the code. Other programming languages often use curly-brackets for this purpose.

Using if Statement

In [1]:
if 10:
    print("Hello")
Hello
In [2]:
if -10:
    print("Hello")
Hello
In [3]:
if 0:
    print("Hello")
else:
    print("Bye")
Bye

using if-else Statement

In [4]:
a=10
b=20
if(a<b):
    print("A is Smaller")
else:
    print("B is Smaller")
A is Smaller
In [5]:
a=int(input("Enter 1st No:"))
b=int(input("Enter 2nd No:"))
if(a<b):
    print(a,"is Smaller")
else:
    print(b,"is Smaller")
Enter 1st No:10
Enter 2nd No:20
10 is Smaller

Multiple if Statement

  • Here all the true conditions will be executed
In [6]:
a=10
b=20
if(a<b):
    print("Hello")
if(b>a):
    print("Hi")
if(a!=b):
    print("Bye")
Hello
Hi
Bye

if-elif Statement

  • Even if all the conditions are true, only first True condition will be executed rest all will not be executed
In [7]:
a=10
b=20
if(a<b):
    print("Hello")
elif(b>a):
    print("Hi")
elif(a!=b):
    print("Bye")
else:
    print("I am optional")
Hello

Nested if-else Statement

In [8]:
a=10
b=20
if(a>b):
    print("A is greater")
    if(a>0):
        print("No is +ve")
    else:
        print("No is -ve")
else:
    print("B is greater")
    if(b>0):
        print("No is +ve")
    else:
        print("No is -ve")   
B is greater
No is +ve

Short Hand If

If you have only one statement to execute, one for if, and one for else, you can put it all on the same line:

In [9]:
a = 20
b = 10
if a > b: print("a is greater than b")
a is greater than b
In [10]:
a = 10
b = 20
print("A is Greater") if a > b else print("B is Greater")
B is Greater

if-else using Logical AND Operator

In [11]:
a = 250
b = 30
c = 500
if a > b and c > a:
    print("Both conditions are True")
Both conditions are True

if-else using Logical OR Operator

In [12]:
a = 250
b = 30
c = 500
if a > b and a > c:
    print("At least one of the conditions is True")

The pass Statement

if statements cannot be empty, but if you for some reason have an if statement with no content, put in the pass statement to avoid getting an error.

In [13]:
a = 10
b = 20

if b > a:
    pass

Task

  • Find No is +ve/-ve
  • Find the No is Even/Odd
  • Find the No is Even/Odd ( Without using any Arithmetic Operator )
  • Find the Greatest Between 2 No’s
  • Find the Greatest Between 3 No’s
  • Find the No is Even/Odd if the number is greater then 6, else find the no is multiple of 3 or not.
  • Calculate Ticket Price:
    • Age : (00-05) –> Free
    • Age : (06-18) –> Rs. 150
    • Age : (19-24) –> Rs. 250
    • Age : (25-30) –> Rs. 25% off on total of age from (6-18 + 19-24)
    • Age : (31-50) –> Rs. 3% off on total age of first 4 condition
    • Age : (51-60) –> Go to chaar dhaam yaatra

Find No is +ve/-ve

In [14]:
# Write your code here
n=int(input("Enter Number"))
if n>0:
    print("No is +Ve")
else:
    print("No is -ve")
Enter Number-45
No is -ve

Find the No is Even/Odd

In [15]:
# Write your code here
n=int(input("Enter Number:"))
if n%2==0:
    print("No is even")
else:
    print("No is odd")
Enter Number:45
No is odd

Find the No is Even/Odd ( Without using any Arithmetic Operator )

In [16]:
# Write your code here
n=int(input("Enter Number:"))
if (n & 1)==0:
    print("No is even")
else:
    print("No is odd")
Enter Number:14
No is even

Find the Greatest Between 2 No’s

In [17]:
# Write your code here
n1=int(input("Enter First Number:"))
n2=int(input("Enter Second Number:"))
if n1>n2:
    print("First No is Greater",n1)
else:
    print("Second No is Greater",n2)
Enter First Number:10
Enter Second Number:20
Second No is Greater 20

Find the Greatest Between 3 No’s

In [18]:
# Write your code here
n1=int(input("Enter First Number:"))
n2=int(input("Enter Second Number:"))
n3=int(input("Enter third Number:"))
if n1>n2 and n1>n3:
    print("First No is Greater")
elif n2>n1 and n2>n3:
    print("Second no is greater")
else:
    print("Third no is greater")
Enter First Number:103
Enter Second Number:55
Enter third Number:24
First No is Greater

Find the No is Even/Odd if the number is greaterthen 6, else find the no is multiple of 3 or not.

In [19]:
# Write your code here
n=int(input("Enter First Number:"))
if n>6:
    if n%2==0:
        print("Even")
    else:
        print("Odd")
else:
    if n%3==0:
        print("Multiple of 3")
    else:
        print("Not Multiple of 3")
Enter First Number:13
Odd

Calculate Ticket Price:

In [20]:
# Write your code here
P1=0
P2=150
P3=250
n=int(input("Enter Age:"))
if n>=0 and n<=5:
    print("Ticket Price:",P1)
elif n>=6 and n<=18:
    print("Ticket Price:",P2)
elif n>=19 and n<=24:
    print("Ticket Price:",P3)
elif n>=25 and n<=30:
    print("Ticket Price:",(P1+P2+P3)*0.75)
elif n>=31 and n<=50:
    print("Ticket Price:",((P1+P2+P3)*0.75)*0.97)
else:
    print("Go to chaar dhaam yaatra")
Enter Age:35
Ticket Price: 291.0

About Machine Learning

Check Also

Python MySQL Insert Into Table

Python MySQL Insert Into Table

4 Python MySQL Insert Into Table Python MySQL Insert Into Table¶To fill a table in …

Leave a Reply

Your email address will not be published. Required fields are marked *