Min menu

Pages

Comparison Operators in Python



comparison operator <  (Less Than Operator)

The operator  < is used to execute a specific code if the value of the first variable  is less than  the value of the second variable.

When we put it in the condition, it means is the value of the first variable  less than  the value of the second variable?

  • If it is smaller than it will be the answer  True and therefore the code will run.

  • If it is not smaller than it will be the answer  False and therefore the code will not execute.


Example

Test.py
          a = 10
		b = 5
	
		# The print command b will be executed less than the value of the variable a The following condition means that if the value of the variable
		if a < b:
		print('a < b')
	
		# The print command a will be executed less than the value of the variable b The following condition means that if the value of the variable is
		if b < a:
		print('b < a')
  

We will get the following result when running.

b < a

We note that he executed the printing command set in the second condition because the answer to the condition was  True. And the printing order placed in the first condition was not executed because the answer to the condition was  False.



comparison operator >=  (Greater Than or Equal To Operator)

The operator  >= is used to execute a specific code if the value of the first variable is  greater than or equal to  the value of the second variable.

When we put it in the condition, it means is the value of the first variable  greater or equal to  the value of the second variable?

  • If it is greater than or equal to it, the answer will be  True , and therefore the code will be executed.

  • If it is not greater than or equal to it, the answer will be  False , and therefore the code will not be executed.


Example

Test.py
          a = 5
		b = 5
		c = 10
	
		# The print command b will be executed greater or equal to the value of the variable a The following condition means that if the value of the variable
		if a >= b:
		print('a >= b')
	
		# The print command c will be executed greater than or equal to the value of the variable a The following condition means that if the value of the variable
		if a >= c:
		print('a >= c')
	
		# The print command a is greater than or equal to the value of the variable c will be executed The following condition means that if the value of the variable is
		if c >= a:
		print('c >= a')
  

We will get the following result when running.

a >= b
c >= a

We note that the printing order placed in the first and third conditions was executed because the answer to the condition was  True. And the printing order placed in the second condition was not executed because the answer to the condition was  False.



comparison operator <=  (Less Than or Equal To Operator)

The operator  <= is used to execute certain code if the value of the first variable  is less than or equal to  the value of the second variable.

When we put it in the condition, it means is the value of the first variable  less than or equal to  the value of the second variable?

  • If it is less than or equal to it, the answer will be  True , and therefore the code will be executed.

  • If it is not less than or equal to it, the answer will be  False , and therefore the code will not be executed.


Example

Test.py
          a = 5
		b = 5
		c = 10
	
		# The print command b will be executed less than or equal to the value of the variable a The following condition means that if the value of the variable
		if a <= b:
		print('a <= b')
	
		# The print command c will be executed less than or equal to the value of the variable a The following condition means that if the value of the variable
		if a <= c:
		print('a <= c')
	
		# The print command a will be executed less than or equal to the value of the variable c The following condition means that if the value of the variable is
		if c <= a:
		print('c <= a')
  

We will get the following result when running.

a <= b
a <= c

We note that the printing command set in the first and second conditions was executed because the answer to the condition was  True. And the printing order placed in the third condition was not executed because the answer to the condition was  False.