Iteration

CSCI 1012

Announcements

  • No lecture 17 Feb (next week)
    • Labs on W 19 Feb and F 21 Feb
  • Unit 1 Exam: 24 Feb
    • 1957 E St. Room 213 3:45-5:00 PM
    • 60 minutes
    • One side of one page (standard Letter) of notes
    • Similar format to quizzes

In-Class Exercises

  • File names:
    • last_digit.py
    • pizza_party.py
    • odd_string.py
  • Turn in on the submit server before the end of lecture for credit
  • Turn in one .zip with all the .py files
    • Unlimited attempts during lecture
  • We’ll provide solutions – pay attention!

Common Errors

  • Incorrect file names:
    • check_even.py vs. checkEven.py
  • Incorrect function name
    • def check_even(x): vs. def check_evens(x):
  • Spacing errors
    • 1 2 3 vs. 1 2 3

Read your autograder output!

Today

  • Conditionals review
  • Iteration:
    • while loop
    • for loop

Conditionals Review

Erstwhile

  • The idea of truth
    • Bools: True and False
  • Conditional execution:
    • if statements
    • elif
    • else

Truth

Values:

print(1 < 2)
print(1 == 2)
print(1 > 2)
print(1 != 2)



Variables:

x = 4
y = 3
print(x == y)

Boolean Expressions

x = 2
y = x == 3
print(y)
False


  • x == 3 is an expression
  • expression yields False
  • \(\rightarrow\) y is assigned False

Bools Are Not Strings

x = (3 < 4)
print(x == False)
print(x == "False")
print(True == "True")
True

Bools Are Not Strings

x = (3 < 4)
print(x == False)
print(x == "False")
print(True == "True")
True
False

Bools Are Not Strings

x = (3 < 4)
print(x == False)
print(x == "False")
print(True == "True")
True
False
False

Conditional Execution

  • Execute code based on some truth value
if condition:
  # execute if condition is True
else:
  # execute if condition is False
  • if
  • elif
  • else

Combining Conditionals

  • and executes if both sides are True
  • or executes if either side is True
  • not “flips” the truth value

In-Class Exericse

last_digit.py
def last_digit(x):
  if x % 10 == 0 or 1:
    return True
  else:
    return False
  • Argument x will be an integer
  • Function should return True if x’s last digit is 0 or 1
    • Otherwise, function should return False
  • Something doesn’t work. Fix it!

Iteration

The while loop

  • if statement:
    • evaluates condition, runs if True
  • while loop:
    • evaluates condition, runs if True
    • evaluates condition again…

while

x = 0
while x < 5:
  x += 1
print(x)
5

in the visualizer

  • while loops need a “stopping condition”

Tracing a while loop

x = 0
x_squared = 0

while x_squared < 100:
  x += 1
  x_squared = x ** 2

print(x_squared)

Photograph of a Pizza Pie

Image copyright: Andy’s Pizza. Fair use.

In-Class Exercise

pizza_party.py
def pizza_party(people):
  slices_per_pie = 8
  pies = 1
  if (pies * slices_per_pie) % people == 0:
    return True
  return False
  • Currently: returns True if one pie can be equally shared by the number of people
  • Goal: return the minimum number of pies needed so the number of people can equally share the slices
    • pizza_party(2) returns 1
    • pizza_party(6) returns 3
    • pizza_party(5) returns 5

The for loop

  • Used for looping over a “collection”
    • We know how many times we want the loop to run
  • vs. while loop:
    • while loop has “indeterminate” number of iterations

range

  • range in Python is a special “collection”
    • create it with the range function
    range(5) # 0 1 2 3 4
    print(range(5))
range(0, 5)
  • 1 argument: “stop”
  • 2 arguments: “start,” “stop”
  • 3 arguments: “start,” “stop,” “skip”
range(1, 5) # 1 2 3 4
range(1, 7, 2) # 1 3 5

range with for loop

for i in range(5): # 0 1 2 3 4 
  print(i)
0
1
2
3
4

Tracing a for Loop

for j in range(2, 10, 2):
  k = j + 1
  print(j, k)
print("End of loop.")

In-Class Exericse

odd_string.py
def odd_string(A):
  s = "" # create an empty string
  # use a for loop to iterate over odd numbers <= A
    # concatenate them with the string and reassign
    # you must account for the last digit "differently"
  return s
  • odd_string(5) should return "1 3 5"
  • odd_string(8) should return "1 3 5 7"

Next Week

Reading: Think Python Chapter 6

Homework 5 due Sunday, 16 Feb 11:55 PM

  • 6 attempts!