Python Conditions and If Statements

Logical Conditions

Equals: a==b

Not Equals: a!=b

Less than: a<b

Less than or equal to: a<=b

Greater than: a>b

Greater than or equal to: a>=b

Logical operators, sketchily

AND: x>=5 and x<=10x[5,10] (conjunction)

OR: x>5 or x=<1[x1;x>5, (disjunction)

NOT: not x>1x1 (negation)

Logical operators return True (if the condition is satisfied) or False (otherwise).

These conditions can be used in several ways, most commonly in if-statements and loops.

Examples

In [1]:
print(1 == True)
print(False == 0)
True
True
In [ ]:
1==1
In [ ]:
not 1==1 # it is kind of negation
In [ ]:
2>3
Out[ ]:
False
In [ ]:
1==1 or 2>3 # it is kind of disjunction
Out[ ]:
True
In [ ]:
11 and 2>3 # it is kind of conjunction
Out[ ]:
False
In [3]:
isEven = 9 % 2 == 0
print(isEven)


if isEven == True:
  print('even')
else:
  print('odd')
False
odd
In [ ]:
int(isEven), int(not isEven)
Out[ ]:
(1, 0)
In [2]:
1 + int(True), True + False
Out[2]:
(2, 1)
In [ ]:
x = 10
y = 20
# standart indentation 
if y > x: # true
    print("y is greater than x")
y is greater than x
In [ ]:
# short   
if y > x: print("y is greater than x"); print(""); y = y+x;
y is greater than x

In [ ]:
x = int(input("x="))
y = int(input("y="))
if x == y:
    print("x and y are equal")
else:
    print("x and y are not equal")
x=1
y=2
x and y are not equal
In [ ]:
x = int(input("x="))
y = int(input("y="))
if y > x:
    print("y is greater than x")
elif x > y:
    print("x is greater than y")    
else:
    print("x and y are equal")
x=1
y=2
y is greater than x
In [ ]:
x = int(input("x="))
y = int(input("y="))
x=1
y=2
In [ ]:
print("X") if x > y else print("Y") #one line if statement

if x > y:
  print ("X")
else:
  print ("Y")
Y
In [ ]:
print("X") if x > y else print("equal") if x == y else print("Y") ### more complex one line if statement
Y

Logical operators

In [5]:
a,b,c,d = 1,2,3,4
In [ ]:
if a < b or a < c:
    print("At least one of the conditions is True")
At least one of the conditions is True
In [ ]:
if a < b < c:
  print("Condition satisfied")
Condition satisfied
In [ ]:
if a < b and a < c:
    print("All conditions are True")
All conditions are True
In [ ]:
# first_condition = c<b or d<a

if not (c<b or d<a) and (a<b):
    print("Complex conditions example is False")
In [ ]:
 

An example problem

You get coordinates of two chess board cells: x1, y1, x2, y2.

Determine if a knight can reach the cell [x2, y2] in a single move, starting from the cell [x1, y1].

A knight's moves are the following (two cells in one direction, then one cell in a perpendicular direction):

...*.*..
..*...*.
....K...
..*...*.
...*.*..

Print "YES" if the knight can reach the cell [x2, y2] in a single move. Otherwise, print "NO".

In [ ]:
x1 = int(input())
y1 = int(input())
x2 = int(input())
y2 = int(input())

While loops and inputting sequences

A simple loop:

In [ ]:
n = 10
i = 1
while i <= n:
    print(i)
    i = i + 1
1
2
3
4
5
6
7
8
9
10

A loop for entering sequences of numbers terminated by 0.

In [ ]:
current = int(input())
minimum = current
while current != 0:
    # your code
    current = int(input())

print(minimum)
5
4
8
9
0
5

Use the previous loop to make a program that finds a minimum value of a sequence.

Use the same loop to make a program that computes a sum of a sequence.

break and continue

An alternative way of stopping the loop is to use the keyword break.

The keyword continue stops the current iteration of the loop and starts the next one.

In [3]:
i = 1
while i < 4:
  print(i)
  i = i + 1
  if i > 4:
    break
  print('After {}'.format(i-1))
else:
  print('There is an else')
1
After 1
2
After 2
3
After 3
There is an else
In [ ]:
i = 0
while i <= 20:
  i = i + 1
  if i % 3 == 0:
    continue
  print('{} is not divisible by 3'.format(i))

  
1 is not divisible by 3
2 is not divisible by 3
4 is not divisible by 3
5 is not divisible by 3
7 is not divisible by 3
8 is not divisible by 3
10 is not divisible by 3
11 is not divisible by 3
13 is not divisible by 3
14 is not divisible by 3
16 is not divisible by 3
17 is not divisible by 3
19 is not divisible by 3
20 is not divisible by 3