Conditions and Branching in Python - MyPythonGuru

Jobs Search Portal and Learning point for Python,Data Science,AI,ML, Cloud and latest technologies.

Follow us on Facebook

Post Top Ad

Your Ad Spot

Sunday, July 14, 2019

Conditions and Branching in Python


Comparison Operators


The output of Comparison operator is Boolean Value either True or False. Below are list of comparison operators and there notation which we used in python and other programming language as well..

  • equal: ==
  • not equal: !=
  • greater than: >
  • less than: <
  • greater than or equal to: >=
  • less than or equal to: <=


Equality Operator:
​Output : false 
a==9—( Equality operator where value of a is equal to 9)
a=9---(Assignment operator where 9 is assigned to variable a)



Inequality Operator:

The inequality test uses an exclamation mark before the equal sign, If value of two operands id not equal then result will come as True and if the value of two operand is equal then result will be as false..

Example 1>

Output :True


Example 2>

When i equals 6 the inequality expression produces False.

​Output : false

Comparison Between Strings:

We can apply comparison operator over string also. See the below examples for more clearity..

Example 1:
Output : False


Example 2:

If we use the inequality operator, the output is going to be True as the strings are not equal.
Output:True



Inequality operation is also used to compare the letters/words/symbols according to the ASCII value of letters. The decimal value shown in the following table represents the order of the character:

For example, the ASCII code for ! is 21, while the ASCII code for + is 43. Therefore + is larger than ! as 43 is greater than 21.

Similarly, the value for A is 101, and the value for B is 102 therefore:
Output=True

When there are multiple letters, the first letter takes precedence in ordering:
Output=True


Note: Upper Case Letters have different ASCII code than Lower Case Letters, which means the comparison between the letters in python is case-sensitive.
Reference : Python Doc

No comments:

Post Top Ad

Your Ad Spot