Equals:
Not Equals:
Less than:
Less than or equal to:
Greater than:
Greater than or equal to:
AND: and (conjunction)
OR: or (disjunction)
NOT: not (negation)
Logical operators return True (if the condition is satisfied) or False (otherwise).
print(1 == True)
print(False == 0)
1==1
not 1==1 # it is kind of negation
2>3
1==1 or 2>3 # it is kind of disjunction
11 and 2>3 # it is kind of conjunction
isEven = 9 % 2 == 0
print(isEven)
if isEven == True:
print('even')
else:
print('odd')
int(isEven), int(not isEven)
1 + int(True), True + False
x = 10
y = 20
# standart indentation
if y > x: # true
print("y is greater than x")
# short
if y > x: print("y is greater than x"); print(""); y = y+x;
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 = 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 = int(input("x="))
y = int(input("y="))
print("X") if x > y else print("Y") #one line if statement
if x > y:
print ("X")
else:
print ("Y")
print("X") if x > y else print("equal") if x == y else print("Y") ### more complex one line if statement
a,b,c,d = 1,2,3,4
if a < b or a < c:
print("At least one of the conditions is True")
if a < b < c:
print("Condition satisfied")
if a < b and a < c:
print("All conditions are True")
# first_condition = c<b or d<a
if not (c<b or d<a) and (a<b):
print("Complex conditions example is False")
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".
x1 = int(input())
y1 = int(input())
x2 = int(input())
y2 = int(input())
A simple loop:
n = 10
i = 1
while i <= n:
print(i)
i = i + 1
A loop for entering sequences of numbers terminated by 0.
current = int(input())
minimum = current
while current != 0:
# your code
current = int(input())
print(minimum)
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.
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.
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')
i = 0
while i <= 20:
i = i + 1
if i % 3 == 0:
continue
print('{} is not divisible by 3'.format(i))