Day 1 - Control Flow
Day 1 - Control Flow
http://yld.me/pbc-su19
Bootcamp: Topics
● Image Processing
● Machine Learning
What is a Computer?
A Computer is...
1. Interpretation
A program reads, parses,
and translates our source
code on-the-fly for execution
2. Compilation
A program processes our source code all at once and
produces an executable
Why Python?
https://www.anaconda.com/distribution/
>>> type(5)
int
Expressions: Evaluation
x + 1
Operator Description
x == y True if x is equal to y, otherwise False
x = 13 # Set x to 13
print(x + 5) # Print 13 + 5
● To set this value, we use the assignment statement
whereby the expression on the right is evaluated and
then stored in the variable on the left
Variables: Tracing
# Code x Description
1. x = 1 1 Set x to 1
2. x + 1 1 No update
3. x = x + 1 2 Set x to 1 + 1
4. x - 1 2 No update
5. x = x * x 4 Set x to 2 * 2
>>> int('10', 2)
2
● To execute a function, we call it by name and pass it
an appropriate set of input arguments.
if condition: if n == 0:
statement(s) print('n is zero')
Alternative Execution
if condition:
statement(s) # Condition is True
else:
statement(s) # Condition is False
Chained Conditionals
Example
if a > b:
if a < c:
print('a is between b and c')
Repeated Execution
While: Overview
Syntax Example
while condition: n = 0
statement(s) while n < 10:
print(n)
n = n + 1
While: Control Flow
Syntax Example
3. Go to step 1.
For: Range
>>> range(0, 3)
[0, 1, 2]
import random
while True:
r = random.randint(0, 10)
if r == 0:
break
print(r)
print(r)
Continue
while True:
r = random.randint(0, 10)
if r == 0:
break
if r % 2:
continue
print('{0} is even'.format(r))