Relational Operators in PythonIn this tutorial, we will discuss how different relational operators can be used in Python programs. The relational operators are also known as comparison operators, their main function is to return either a true or false based on the value of operands. Following are the relational operators-
Let’s start with the first one- 1. Greater or less than (>, <)The program given below illustrates how it can be done- Output: The sum of a and b is 30 True False False True Explanation- It’s time to have a look at the explanation of the above program-
2. Equals to (==)The equals to ‘==’ operator return a True value if values stored in two variables are the same. The following program illustrates its usage Output: The sum of a and b is 30 The difference of d and b is 20 True False True True Explanation- Let’s understand what happened in the above program-
3. Not Equal to (!=)The not equals to ‘!=’ operator returns a True value if values stored in two variables are different. Consider the program given below- Output: The sum of a and b is 30 The difference of d and b is 20 True True False True Explanation- Let’s have a glance at the explanation of the above program-
Finally, we will discuss the last relational operator which is greater than equal to or less than equal to. 4. Greater than or less than equal to (>=, <=)The greater than equal to returns ‘True’ if the value in any of the two variables is greater than or equal to the second one. The ‘lesser than equal to’ works in a similar way. Let us see how it can be used in a Python program, Output: The sum of a and b is 30 The difference of d and b is 20 True True True True Explanation- The program given below illustrates how it can be done-
ConclusionIn this tutorial, we learned how relational operators can be used in a Python program. |
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/263367.html