12th Apr 2022 9 minutes read How to Write “Greater Than or Equal To” in Python Xavier Rigoulet python Comparison operators are an important part of Python programming. In this article, let’s explore how to use the greater than or equal to comparison in Python. Many programming beginners wonder how to write “greater than or equal to” in Python. Well, to write greater than or equal to in Python, you need to use the >= comparison operator. It will return a Boolean value – either True or False. The "greater than or equal to" operator is known as a comparison operator. These operators compare numbers or strings and return a value of either True or False. Example: Using Greater Than or Equal To in Python Let's answer a series of questions with the help of this Python comparison operator: >>> # Is 9 greater than or equal to 5? >>> 9 >= 5 True >>> # Is 5 greater than or equal to 5? >>> 5 >= 5 True >>> # Is 5 greater than or equal to 9? >>> 5 >= 9 False As you can see, the greater than or equal to operator returns True if the left side of the operator is greater than or equal to the right side. It returns False otherwise. If you’re a beginner in Python, our Python Basics Part 1 Course is the perfect place to start. This course is designed by Python experts and includes 95 interactive exercises to help you learn the programming essentials in just 10 hours. Plus, once you finish the course, you'll be able to show off your new skills on your resume! Greater Than or Equal To And Conditional Statements in Python We can also use the greater than or equal operator with the conditional if-else statement. In other words, we will use the >= operator to decide whether the if statement needs to be executed. Like we did previously, let's translate some instructions we want to give the computer into code. Instead of choosing numerical values arbitrarily as before, we will write a script that will invite the user to input two numbers to be stored in the variables a and b. If a >= b, the computer will print a simple message: # If a is greater or equal than b, print "Hello LearnPython.com" a = input("Enter a numerical value, a = ") b = input("Enter a numerical value, b = ") if a >= b: print("Hello LearnPython.com") Enter a numerical value, a = 9 Enter a numerical value, b = 5 Hello LearnPython.com However, if a is not greater or equal to b, nothing will happen: Enter a numerical value, a = 3 Enter a numerical value, b = 8 This is because I haven’t set any else statements. Let’s fix this! # If a is greater or equal than b, write "Hello LearnPython.com". Otherwise, write “Python is cool!”. a = input("Enter a numerical value, a = ") b = input("Enter a numerical value, b = ") if a >= b: print("Hello LearnPython.com") else: print("Python is cool!") Enter a numerical value, a = 3 Enter a numerical value, b = 8 Python is cool! The if statement will execute the code if the if branch returns True; otherwise, nothing happens. In the case of an if-else statement, first the if branch checks the validity of the branch and executes the code if it returns True. If it returns False, the else branch is executed. With a comparison operator, we compare two values. In the example above, we entered a = 3 and b = 8. In other words, we asked the computer to check the validity of the following: If 3 is greater than or equal to 8, display “Hello LearnPython.com” on the screen. If not, display “Python is cool!”. Because 3 is not greater than or equal to 8, the initial if statement is False; therefore, the Python script executed the else statement. Greater Than Or Equal To In One Line Python does not support a ternary operator (which accepts three operands rather than two); however, by writing an if-else statement as a one-liner conditional expression, we can have an effect of a ternary operator in Python. The syntax is: value_if_true if condition else value_if_false Let’s run some examples: >>> a = 9 >>> output = "Hello LearnPython.com" if a >= 6 else "Python is cool!" >>> print(output) Hello LearnPython.com >>> a = 5 >>> output = "Hello LearnPython.com" if a >= 6 else "Python is cool!" >>> print(output) Python is cool! The expression above evaluates the condition first. If the condition is True, the code evaluates value_if_true and returns its value. If the condition is evaluated as False, the code evaluates value_if_false and returns its value. In our code example, first the script evaluates the condition a >= 6. If a >= 6 is True, then the script returns “Hello LearnPython.com”; otherwise, “Python is cool!” is returned. Greater Than Or Equal To With Numeric Values The operator >= can be used to compare numeric data types as well as lists and tuples. For a list or tuple, the "greater than or equal to" operator iterates over the lists or tuples and evaluates the elements of both the left and right operands. The comparison is conducted in lexicographical order: The first two items are compared, and the comparison outcome is determined based on whether they differ. If the first two items are identical, the following pairs of items are compared sequentially until the exhaustion of either sequence. Feel free to have a look at the official Python documentation for more information. It is important to note that we cannot compare different data types. In other words, we can only use the >= operator to compare a list with another list or a tuple with another tuple. Let's run some examples. >>> [4, 8, 5] >= [45] False The first element of the left operand, 4, is compared with the first element of the right operand, 45. Since 4 is smaller than 45, it returns False. The comparison stops here because there is only one element in the right operand. >>> [12, 8] >= [5] True Contrary to the above example, the output is True because the first element of the left operand, 12, is greater than the first (and only) element of the right operand, 5. >>> [12, 12, 8] >= [12, 12] True Following the rules of comparing the n-th element in the left and right operand, the 1st elements are equal; therefore, the output is True. >>> [4,2,1] >= [3,1,8] True In this example, the first element of the left operand, 4, is compared with the first element of the right operand, 3. Because 4 is greater than 3, the expression is evaluated as True. It works similarly with other data structures, such as tuples. However, let's say you have a tuple consisting of one element and you write the following code: >>> (15, 12) >= (3) TypeError: '>=' not supported between instances of 'tuple' and 'int' You will get an error because the expression in the right operand is not recognized as a tuple. To create a tuple with a single element, you need to add a comma. >>> (15, 12) >= (3,) True The output is True because 15 is greater than 3. Next, let's talk about how the "greater than or equal to" comparison operator works with text values. Greater Than or Equal To With Text Values It is also possible to use the greater than or equal to comparison operator with text values. In Python, text is stored as a string. The >= operator compares strings’ ASCII values. For computers and other telecommunication devices, ASCII codes represent text. Let's run some examples to illustrate this concept. >>> "hello" >= "olleh" False Comparing "hello" with the same word in reverse order returns False. This is because the ASCII value of "h" is 104, while "o" is 111. Since 104 < 111, the output is False. Another example: >>> "python" >= "PYTHON" True The output is True because the ASCII value of lowercase letters is greater than that of uppercase letters. "1" >= "one" False Similarly, numbers have smaller values than letters; this is why the example above returns False. Last but not least, it is essential to note that we cannot compare values of different types. Let's rewrite our previous example to illustrate this: >>> 1 >= "one" TypeError: '>=' not supported between instances of 'int' and 'str' The above code returns an error because I attempted to compare a number with a string. Other Comparison Operators in Python For the sake of completeness, I just want to mention that Python has other comparison operators, such as less than (<), greater than (>), less than or equal to (<=), equal to (==), and not equal to (!=). Let's run a quick example for each: >>> # Less than >>> 4 < 5 True >>> # Greater than >>> 4 > 5 False >>> # Less than or equal to >>> 4 <= 5 True >>> # Equal >>> 4 == 5 False >>> # Not equal >>> 4 != 5 True Finally, it is also important to note that Python’s built-in sorting functions use the language’s built-in comparison operators. Feel free to read my articles on how to sort alphabetically and custom sorting functions in Python to learn how to change the comparison operator used with the sort() method. Closing Thoughts on Greater Than or Equal To in Python In this article, we learned about the greater than or equal to comparison operator in Python. We saw how to use it with a conditional statement and how to write an if-else statement as a one-liner conditional statement to mimic ternary operators in Python. We saw that we could not compare different data types with "greater than or equal to" in Python. We also explored how it’s used to compare the ASCII values of strings. I encourage you to reuse the code snippets above and play with the code to get a better understanding of the concepts explained in this article. This also prompts the following question: Do you want to improve your Python programming skills? With our Python Basics Track, you can discover variables, control flow statements, loops, functions, data structures, and more. This 38-hour track is divided into 3 interactive courses. Each is designed for complete beginners who want a solid foundation in Python 3. We'll walk you through each concept step by step; by the end of the course, you'll be able to write your own code! Sign up today for our Python Basics Track and get started on your programming journey! And do not forget to visit LearnPython.com to keep learning about Python. Tags: python