Tuesday, June 1, 2021

Python - Conditional Expressions or Decision making statements

Python has some decision making conditional statements on which we can take some further steps. If the condition is TRUE then it perform some specific steps ELSE or FALSE it executes other specific statements.

Types of Condition statements:

  • if Statement
  • If else 
  • if elif ...and else 

If else and elif statements are a multiway decision taken by our program due to certain conditions in our code.

Syntax:

if (condition1): // if condition 1 is true

print(“yes”)

elif (condition2): // if condition 2 is true

print(“No”)

else: // otherwise

print(“May be”)

Code example:

a = 22

if (a>9):

    print(“Greater”)

else:

    print(“lesser”)


Relational Operators:

Relational operators are used to evaluate conditions inside if statements. Some examples of relational operators are:

= = -> equals

>=  -> greater than/equal to

<=, etc.

Logical Operators:

In python, logical operators operate on conditional statements. Example:

and -> true if both operands are true else false

or -> true if at least one operand is true else false

not -> inverts true to false and false to true


elif clause

elif in python means [else if]. if statement can be chained together with a lot of these elif statements followed by an else statement.

if (condition1):

    #code

elif (condition 2):

    #code

elif (condition 2):

    #code

….

else:

    #code

Above ladder will stop once a condition in an if or elif is met.

Important Notes:

  • There can be any number of elif statements.
  • Last else is executed only if all the conditions inside elifs fail. 

Practice:

Write a program to print yes when the age entered by the user is greater than or equal to 18.

age=int(input("Enter your age:\n"))
if age >=18:
    print("Yes")
else:
    print("No")

Write a program to find the greatest of four numbers entered by the user.

n1=int(input("Enter number 1:"))
n2=int(input("Enter number 2:"))
n3=int(input("Enter number 3:"))
n4=int(input("Enter number 4:"))

if n1>n2:
    g1=n1
else:
    g1=n2

if n3>n4:
    g2=n3
else:
    g2=n4

if g1>g2:
    greatest=g1
else:
    greatest=g2

print("Greatest number is: "greatest)


Write a program to find out whether a student is pass or fail if it requires a total of 40% and at least 33% in each subject to pass. Assume 3 subjects and take marks as an input from the user.

m1=int(input("Enter mark 1:"))
m2=int(input("Enter mark 2:"))
m3=int(input("Enter mark 3:"))


if (m1 <33 or m2<33 or m3<33):
    print("the Student failed as less than 33 in one of the subject")
elif ((m1+m2+m3)/3) <40:
    print("the Student failed as total is less than 40 parcentile")
else:
    print("The student has passed with ",(m1+m2+m3)/3)

A spam comment is defined as a text containing the following keywords:

“make a lot of money”, “buy now”, “subscribe this”, “click this”. Write a program to detect these spams.

text = input("Enter the text\n")

if("make a lot of money" in text):
    spam = True
elif("buy now" in text):
    spam = True
elif("click this" in text):
    spam = True
elif("subscribe this" in text):
    spam = True
else:
    spam = False

if(spam):
    print("This text is spam")
else:
    print("This text is not spam")

Write a program to find whether a given username contains less than 10 characters or not.

username=input("Enter your name:\n")
if len(username)<10:
    print("Username contains less than 10 chars")
else:
     print("Username contains more than or equal to 10 chars")

Write a program that finds out whether a given name is present in a list or not.

myList=['sri','ananda','joy','lilu']

name =input("Enter a name:\n")
if name in myList:
    print("Yes")
else:
    print("No")

Write a program to calculate the grade of a student from his marks from the following scheme:

90 – 100 -> Ex

80 - 90 -> A

70 – 80 -> B

60 – 70 -> C

50 – 60 -> D

<50 – F

mark=int(input("Enter your mark:"))
if mark >=90:
    print("Ex")
elif mark >=80:
    print("A")
elif mark >=70:
    print("B")
elif mark >=60:
    print("C")
elif mark >=50:
    print("D")
else:
    print("F")

No comments:

Post a Comment

Featured Post

11g to 12c OSB projects migration points

1. Export 11g OSB code and import in 12c Jdeveloper. Steps to import OSB project in Jdeveloper:   File⇾Import⇾Service Bus Resources⇾ Se...