Python Bootcamp�Day 2: Basic Syntax and Control Flow
Overview
Basic Syntax
Building Blocks
Values
Expressions
An expression is a combination of values, variables, and operators.�
An operator is a symbol that represents a kind of computation.
Every expression has a value when evaluated.�
Variables
We can store the results of an expression into a variable by using the the assignment.
Variable
A name or label that refers to stored data�(has a name, type, and value)�
Statements
A statement is unit of code that Python can execute (ex. print, assignment).
A program is a series of statements.
A statement does not necessarily have a value.
Activity: Tracing
Trace the execution of the following code:
# Program 1
x = 1
y = 2
print x, y
x = 1
y = 1
print x, y
# Program 2
x = 1
y = 2
z = x + y
print x, y, z
x = y
y = x
x * y
print x, y, z
Containers
Activity: Computations
Write code that computes the following:
Bugs
Conditional Execution
Logical Expressions
A logical expression evaluates to a truth value, in essence True or False.
Example
>>> length = 4
>>> width = 2
>>> length < width
False
Logical Operators
== equal to�!= not equal to�>= greater than or equal to�> greater than�< less than�<= less than or equal to��and both must be true�or one or both must be true�not reverses the truth value
Compare Two Objects
Glue Logical Expressions
Conditional Statements
We use conditional statements to check boolean expressions (which we call conditions), and change the behavior of the program accordingly.
if condition: if n == 0:
statement(s) print ‘n is zero’
Alternative Execution
Alternative execution is when we have two possibilities and the condition determines which one gets executed.�
if condition:� statement(s) # Condition is True�else:
statement(s) # Condition is False
Activity: Range Check
Write code that checks if a number is between 5 and 10 (inclusive).
Chained Conditionals
When we want to check more than one condition, we can use chained conditionals.�Each condition is checked in the order that they appear; if one is triggered, then the rest are skipped.
if condition 1:� statements(s)�elif condition 2:� statements(s)�elif condition 3:� statements(s)�else:� statements(s)
Nested Conditionals
Conditional statements can be nested or placed inside another conditional statement.
Example
if a > b:
if a < c:
print ‘a is between b and c’
Activity: Odd and Divisible
Write code that checks if the number entered in by the user is both odd and divisible by three.
Iteration
While: Overview
We can use a while statement to repeatedly execute a series of tasks (i.e. a loop).
Syntax Example
while condition: n = 0
statement(s) while n < 10:
print n
n = n + 1
While: Control Flow
While: Notes
Activity: Debugging
The following code is suppose to sum all the numbers between 0 and 9 (inclusive). Unfortunately, it has bugs. Correct these errors.
n = 0
total = 0
while n < 9:
total + n
print total
Activity: Loop Exercises
For: Overview
Iterating through a definite set of objects is a common looping pattern, so Python provides the for statement.
Syntax Example
for item in dataset: for i in [0, 1, 2, 3]:
statement(s) print i
For: Control Flow
For: Range
We can generate a list of numbers using the range function:
>>> range(0, 3)
[0, 1, 2]
>>> for i in range(0, 3):� print i
0
1
2
For: Enumerate
We can generate a list of pairs consisting of the index and the item using enumerate:
>>> list(enumerate([‘a’, ‘b’, ‘c’]))
[(0, 'a'), (1, 'b'), (2, 'c')]
>>> for index, letter in enumerate(‘abc’):
print index, letter
0 a
1 b
2 c
Activity: Loop Exercises
Break
We can exit a loop immediately by using the break statement
import random
while True:
r = random.randint(0, 10)
if r == 0:
break
print r
print r
Continue
We can skip the rest of the loop body and move on to the next iteration by using continue
while True:
r = random.randint(0, 10)
if r == 0:
break
if r % 2:
continue
print '{0} is even'.format(r)
Activity: Guessing Game
Write code that generates a random number and asks the user to guess what it is.
Guess the number: 1� Higher!
Guess the number: 3� Lower!
Guess the number: 2� Hooray, you got it!�