First of all we will discuss the Python if statement

Python If statement is an expression or a conditional statement that is used to check or validate conditions, with if statements we can run a block of statements on the basis of some condition. i.e if the statement is true, a block of one or more than one statements will run.

How to use python if statement in code

  • If a student gets marks mroe than 90%, he will get the grade A

	if numbers > 90:
		print("You get grade A in this subject"

  • If a person has the age 18 or more than 18, he can apply for national identity card

	if age >= 18:
		print("You can apply for national identity card")

  • If a number is divisible by 2 or in other words if a number has remainder 0 while dividing by 2, it is an even number

	if number % 2 == 0:
		print("This is an even number")


Second turn is of Python if else statement

As you know that we can define a block of statements to execute on getting a condition true by if statement, Further more we can also define a block of statements to run if the condition gets false. i.e if the statement is true, a block of one or more than one statements will run. Otherwise another block of statements will be executed.

how to use python if else statement in code

  • If a student gets marks more than 90%, he will get the grade A. Otherwise lower than A

	if numbers > 90:
		print("You will get grade A in this subject")
	else :
		print("You will get grade less than A")

  • If a person has the age 18 or more than 18, he can apply for national identity card. Otherwise not.

	if age >= 18:
		print("You can apply for national identity card")
	else:
		print("You are not eligible to apply for national identity card")

  • If a number is divisible by 2 or in other words if a number has remainder 0 while dividing by 2, it is an Even number. Otherwise an Odd number.

	if number % 2 == 0:
		print("This is an Even number")
	else:
		print("This is an Odd number")


Last but not least turn is of Python else if(elif) statement

While writing code for any application almost every time you got scenerios in which true and false are not enough to cover the whole scenerio. e.g

We can consider our previous ‘Grading system’ scenario, which remains incomplete with if and else only because we can handle only two options. So here we will discuss its solution. 

There must be something which is for the gap between true and false so that we can check for all the grades, So that python if statement is optionally follow up by ‘python else if’ statement. More specific to Python’s terminology, statement syntax is ‘elif’. Lets get to the code so that you can understand it better.

  • If a student gets marks mroe than 90%, he will get the grade A.
  • If a student gets marks mroe than 80% but less than 91, he will get the grade B.
  • If a student gets marks mroe than 70% but less than 81, he will get the grade C.
  • If a student gets marks mroe than 60% but less than 71, he will get the grade D.
  • If a student gets marks mroe than 50% but less than 61, he will get the grade E.
  • Otherwise he will get grade F.

	if numbers > 90:
		print("You will get grade A in this subject")
	elif numbers > 80 and numbers < 91:
		print("You will get grade B in this subject")
	elif numbers > 70 and numbers < 81:
		print("You will get grade C in this subject")
	elif numbers > 60 and numbers < 71:
		print("You will get grade D in this subject")
	elif numbers > 50 and numbers < 61:
		print("You will get grade E in this subject")
	else :
		print("You will get grade F")

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.