Strings

Input/Output: print

Input/Output: input

Comparison operators on int, float, string

Logic operators on bools

a and b are variable names (with Boolean values) Write truth tables for these

Example:

study_time = 15
sleep_time = 8
good_marks = (study_time > sleep_time)
good_sleep = not (sleep_time < 8)
both = good_marks and good_sleep
print(both)

Control flow - Branching

Form 1:
if <condition>:
  <expression>
  <expression>

Form 2:

if <condition>:
  <expression>
  <expression>
else:
  <expression>
  <expression>

Form 3:

if <condition>:
  <expression>
  <expression>
elif <condition>:
  <expression>
  <expression>
else:
  <expression>
  <expression>

<condition> has a value of True or False. Evaluate expressions in that block if <condition> is True.

Indentation

Control flow: While Loops

Example:

You are in the Lost Forest.
************
************
    Karel
************
************
Go left or right?
Each time Karel goes right, it gets lost in a forest. As soon as it goes left, it gets out of the forest.

Try this with if-else:

if input("You're in the Lost Forest. Go left or right? ") == right:
  if input("You're in the Lost Forest. Go left or right? ") == right:
    if input("You're in the Lost Forest. Go left or right? ") == right:
      if input("You're in the Lost Forest. Go left or right? ") == right:
        if input("You're in the Lost Forest. Go left or right? ") == right:
          .... #goes on forever
        else:
          print("You got out of the Lost Forest!")
      else:
        print("You got out of the Lost Forest!")
    else:
      print("You got out of the Lost Forest!")
  else:
    print("You got out of the Lost Forest!")
else:
  print("You got out of the Lost Forest!")

To do this, we need a new construct:

while <condition>:
  <expression>
  <expression>
  ...
n = input("You're in the Lost Forest. Go left or right? ")
while n == "right":
  n = input("You're in the Lost Forest. Go left or right? ")
print("You got out of the Lost Forest!")

Control flow: while and for loops

Iterate through numbers in sequence
# more complicated with while loop
n = 0
while n < 5:
  print(n)
  n = n + 1

Shortcut with for loop:

for n in range(5):
  print(n)

for Loops

for <variable> in range(<some_num>):
  <expression>
  <expression>
  ...
Each time through the loop, variable takes a value. First time, variable starts at the smallest value. Next time, variable gets the prev value + 1.

range(start, stop, step). The default values are start=0 and step=1. Loop until value is stop-1.

mysum = 0
for i in range(7, 10):
  mysum += i
print(mysum)
mysum = 0
for i in range(5, 11, 2):
  mysum += i
print(mysum)

break statement

while <condition_1>:
  while <condition_2>:
    <expression_a>
    break
    <expression_b>
  <expression_c>

Example:

mysum = 0
for i in range(5, 11, 2):
  mysum += i
  if mysum == 5:
    break
    mysum += 1
print(mysum)
Output?

for vs. while loops

for loops while loops
know number of iterations unbounded number of iterations
can end early via break can end early via break
uses a counter can use a counter but must initialize before loop and increment it inside loop
can rewrite a for loop using a while loop may not be able to rewrite a while loop using a for loop