Flow Control

Flow control involves 3 things: boolean values, comparison operators, boolean operators

Boolean Values

Boolean value is either True or False (capitalize)

Comparison Operators

Operator Meaning
== Equal to
!= Not equal to
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to
In [1]:
42.0 == 42
Out[1]:
True
In [2]:
42 == '42'
Out[2]:
False
In [3]:
'string' == 'string'
Out[3]:
True

Boolean/ Logical Operator

There are 3 boolean operators: and, or, not

In [4]:
(1==2) and (1==1)
Out[4]:
False
In [5]:
1==2 or 1==1
Out[5]:
True
In [6]:
(1==1) and not (1==2)
Out[6]:
True

If, Else and Elif Statements

If statement

In [10]:
if True:
  print('Hello')
Hello

Else statement

In [12]:
if True:
  print('True')
else:
  print('False')
True

Elif statement

In [17]:
condition1 = 5
condition2 = 4
condition3 = 3

if condition1:
  print('condition1')
elif condition2:
  print('condition2')
elif condition3:
  print('condition3')
condition1

Block & Indentation

The indented code is called a block. A block is made up of lines of code that are indented at the same level. The indentation is how Python tells what parts is inside the if statements block and what isn't. A block begins when the indentation increases and ends when the indentation returns to its previous level.

Truthy and Falsey Values and bool() function

In the following example, blank string is Falsey, all others are Truthy. You can always see for yourself which values are Truthy or Falsey by passing them into the bool() function e.g. bool('') returns False

In [18]:
print('Enter a name:')
name = input()
if (name):
  print('Thank you for entering a name')
else:
  print('You did not enter a name')
Enter a name:

You did not enter a name

While Loops

Syntax

while True:
  # do something

In the IDLE interactive shell, if you get stuck with an infinite loop, press Ctrl+C for keyboard interrupt.

Break statement

The break statement causes the execution to immediately leave the loop without re-checking the condition.

In [25]:
name = ''
while True:
  print('Enter your name')
  name = input()
  if name == 'your name':
    break
print('Thank you')
Enter your name
your name
Thank you

Continue statement

The continue statement causes the execution to immediately jump back to the start of the loop and re-check the condition.

In [24]:
spam = 0
while spam < 5:
  spam = spam + 1
  if spam == 3:
    continue
  print('spam is ' + str(spam))
spam is 1
spam is 2
spam is 4
spam is 5

For Loops & Range() Function

for loops will loop specific number of times. Keywords break and continue can be used in for loops.

In [26]:
seq = [1,2,3,4,5]
for item in seq:
    print('hello')
hello
hello
hello
hello
hello

The range() function is a generator for generating a sequence or list of integers. The function range() is called with one, two or three arguments.

In [27]:
range(5)
Out[27]:
range(0, 5)
In [28]:
list(range(1, 5))
Out[28]:
[1, 2, 3, 4]

Range() with 1 argument

In [30]:
for i in range(5):
    print(i)

# range(5) returns range(0, 5)
0
1
2
3
4

Range() with 2 arguments

In [31]:
for i in range(12, 16):
    print(i)
12
13
14
15

Range() with 3 arguments

In [32]:
for i in range(0, 10, 2):
    print(i)
0
2
4
6
8
In [33]:
for i in range(5, -1, -1):
    print(i)
5
4
3
2
1
0

Useful Resources

  • Visualize Python, Java, Javascript, Typescript and Ruby Code Execution [Link]