Decision making with python

Decision-making using python

Introduction

Decision-making is always considered the first step in learning AI/ML. Decision-making in computers is as important as decision-making by humans. By the end of this article, you will be thorough with decision-making with the help of python.

Types of conditional branching

  • If statement
  • If else statement
  • If elif else statement
  • Nested conditions
  • Shorthand if statement 
  • Shorthand if-else statement
  • Match case

Conditional operators:

Before moving with conditional branching, we should be aware of the conditional operators. Conditional operators always check if the condition is True or False and return the answer in boolean format(True/False).

The following table contains the conditional operators with their explanations,

==a==bReturns True if a and b are equal else returns False.
<a<bReturns True if a is less than b else returns False.
>a>bReturns True if a is greater than b else returns False.
<=a<=bReturns True if a is less than or equal to b else returns False.
>=a>=bReturns True if a is greater than or equal to b else returns False.
!=a!=bReturns True if a is not equal to b else returns False.
Conditional operators

If statement:

If statement is used for executing a line/block of code only upon the condition is satisfied. 

Syntax:

If <condition>:
  statements

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

See also  Dictionary in python

Example:

number=int(input("Enter a number"))
if number ==0:
	print("The number is zero")

In the above code snippet,

  • In line 1, we are getting the input from the user and converting it to an integer.
  • In line 2, we are checking the condition if the number is equal to zero.
  • Line 3 will be executed only if the condition is true, which prints the number is zero.

If else statement:

If else statement is used when you have certain operations to be done for both satisfying and unsatisfying conditions.

Syntax:

if <condition>:
	Statements
else:
	statements

Example:

num=int(input("Enter a number"))
if num>0:
	print("The number is positive")
else:
	print("The number is negative")

In the above code snippet,

  • In line1, we are getting the input from the user and converting it to an integer.
  • In line2, we are checking the condition if the number is greater than zero.
  • Line 3 is executed if the condition is True to print the number is positive
  • If the condition was false, then else statement block is executed to print the number is negative.

If elif else statement:

Whenever there are more conditions to be checked elif plays a vital role. Elif is an abbreviation of else-if which means, “if the previous conditions were not true, then try this condition”.

Syntax:

if <condition>:
    Statements 1
elif <condition>:
    Statements 2
else:
    Statements3

Please note, that there can be more than 1 elif condition, and else is totally optional.

Example:

num=int(input("Enter a number:"))
if num >0 :
    print("positive number")
elif num==0:
	print("The number is zero")
else:
    print("Negative number") 

In the above code snippet,

  • In line1, we are getting the input from the user and converting it to an integer.
  • In line2, we are checking the condition if the number is greater than zero.
  • Line 3 is executed if the condition is True to print the number is positive
  • If the condition was false, then the elif condition is checked, if that condition was True then the elif block will be executed printing “The number is zero”.
  • Else statement is executed only when none of the conditions is satisfied.
See also  Introduction to python

Nested conditions:

Python also allows you to use nested conditions, which means we can start another new block of conditions at any instance inside the blocks of code. But always double-check for correct indentations when using nested conditions.

Shorthand if statement:

If you have only one line of code to execute we can use shorthand if statement.

Syntax:

If <condition> : statement

Example:

n=5
If n>0 : print("positive")

Shorthand if-else condition:

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

Syntax:

<TrueStatement> if <condition> else <FalseStatement>

Example:

n=5
print("positive") if n>0 else print("negative")

Match case:

In python3.10, we have the match case which is similar to the switch case in all the other programming languages,

Syntax:

match <variable>:
	case <value1>:
		Statements
	case <value2>:
		Statements
	case _:
		statements

Example:

VN=input("Veg/non-veg")
match VN:
	case "Veg":
		print("Vegetarian is selected")
	case "non-veg":
		print("Non- vegetarian is selected")
	case _:
        print("Please select correct option")

In the above snippet,

  • We are getting the input from the user.
  • We are making the variable VN match the following cases, If the input was Veg, “Vegetarian is selected” is printed. If the input was Non-Veg, “Non-Vegetarian is selected” is printed.
  • Case_ is used as the default case which means this statement will be executed only if no other cases are satisfied.

Logical operators

Logical operators are used to combining conditional statements and returns True or False.

OperatorExplanation Exampleoutput
andReturns True if both the statement is True else return Falseprint(7<2 and 7==7)False
orReturns True if either of the statement is trueprint(7<2 or 7==7)True
notReverse the result. Returns True if the answer is Fales and returns False if the answer is Trueprint(not 7==7)False
Logical operators

Example

Write a program to check if a student’s age is between 5 to 15 for a junior-level singing competition.

age=int(input("Enter your age"))
if age>=5 and age<=15: #and operator checks if both the condition is True
	print("You are eligible for the context")
else:
    print("You are not eligible")

Write a program to know if the user likes ice cream and write a print correspondingly

do_you=input("Do you like ice cream?")
if do_you =="yes" or do_you =="Yes" or do_you=="YES":
	print("Wow, i too like ice cream")
else:
    print("Its okay i love eating it!!!")

Practise yourself

  • Write a program to calculate the hypotenuse or one of the sides of the right-angled triangle using Pythagoras theorem.
  • Write a program to find the greatest of two numbers.
  • Write a program to check if a user is eligible for voting.
  • Write a program to check if a number is an odd or even number.
  • Write a program for an ATM machine, with the following instructions,
    • A program that has 4 menu options:
      • 1. Deposit
      • 2. Withdraw
      • 3. Balance
      • 4. Quit 
    • At the start of the program set a value for the balance.
    • You must ensure that when withdrawing the amount is not greater than the balance
    • If Deposit or Withdraw options are chosen, remember to display the balance after the actions are performed.
See also  Sets in python

Conclusion:

Congratulations on taking the first step to learning python programming, AI, and ML. By the end of the article, I hope you are now aware of the decision-making by python. “Practice makes a man perfect” so keep coding to master the art of coding.

1 thought on “Decision-making using python”

  1. Pingback: Rock Paper Scissor Game in python -

Leave a Comment

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

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

we provide projects, courses, and other stuff for free. in order for running we use Google ads to make revenue. please disable adblocker to support us.