True or False. 7 > 4
7 < 4
| Algebraic operator | Python operator | Sample condition | Meaning |
|---|---|---|---|
| > | > |
x > y |
x is greater than y |
| < | < |
x < y |
x is less than y |
| ≥ | >= |
x >= y |
x is greater than or equal to y |
| ≤ | <= |
x <= y |
x is less than or equal to y |
| = | == |
x == y |
x is equal to y |
| ≠ | != |
x != y |
x is not equal to y |
>, <, >= and <= have the same precedence. == and != have the same precedence, which is lower than >, <, >= and <=. ==, !=, >= and <= contains spaces between its pair of symbols.7 > = 4
if statement uses a condition to decide whether to execute a statement (or a group of statements). if statements, one for each comparison operator. .py (short for Python) extension. ch02 examples folder, then enter:ipython fig02_01.py
run fig02_01.py
# fig02_01.py
"""Comparing integers using if statements and comparison operators."""
print('Enter two integers, and I will tell you',
'the relationships they satisfy.')
# read first integer
number1 = int(input('Enter first integer: '))
# read second integer
number2 = int(input('Enter second integer: '))
if number1 == number2:
print(number1, 'is equal to', number2)
if number1 != number2:
print(number1, 'is not equal to', number2)
if number1 < number2:
print(number1, 'is less than', number2)
if number1 > number2:
print(number1, 'is greater than', number2)
if number1 <= number2:
print(number1, 'is less than or equal to', number2)
if number1 >= number2:
print(number1, 'is greater than or equal to', number2)
37 and 42run fig02_01.py
7 and 7run fig02_01.py
54 and 17run fig02_01.py
#) indicates that the rest of the line is a comment.\ continuation character. print statement)—this is preferred according to the Style Guide for Python Code. input and intif Statements¶if statement consists of the keyword if, the condition to test, and a colon (:) followed by an indented body called a suite. =) instead of the equality operator (==) in an if statement’s condition is a common syntax error. x = 3
1 <= x <= 5
x = 10
1 <= x <= 5
| Operators | Grouping | Type |
|---|---|---|
() |
left to right | parentheses |
** |
right to left | exponentiation |
* / // % |
left to right | multiplication, true division, floor division, remainder |
+ – |
left to right | addition, subtraction |
> <= < >= |
left to right | less than, less than or equal, greater than, greater than or equal |
== != |
left to right | equal, not equal |
©1992–2020 by Pearson Education, Inc. All Rights Reserved. This content is based on Chapter 2 of the book Intro to Python for Computer Science and Data Science: Learning to Program with AI, Big Data and the Cloud.
DISCLAIMER: The authors and publisher of this book have used their best efforts in preparing the book. These efforts include the development, research, and testing of the theories and programs to determine their effectiveness. The authors and publisher make no warranty of any kind, expressed or implied, with regard to these programs or to the documentation contained in these books. The authors and publisher shall not be liable in any event for incidental or consequential damages in connection with, or arising out of, the furnishing, performance, or use of these programs.