Skip to content

Instantly share code, notes, and snippets.

@jendiamond
Last active November 25, 2023 15:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jendiamond/8062b1495d6d14c41aba to your computer and use it in GitHub Desktop.
Save jendiamond/8062b1495d6d14c41aba to your computer and use it in GitHub Desktop.
Python Chapter 4 Notes

Starting Out with Python 3rd Edition - Tony Gaddis

Chapter 4

repetition structure - a loop - write code once put it in a structure that repeats as many times as necessary

condition controlled loop - while loop - uses a true false condition

while loop (pre-test loop) condition controlled loop - uses a true false condition

  • condition tests for T or F
  • a statement that is repeated as long as the condition is true
  • condition is tested BEFORE performing an iteration
  • it will NEVER execute if condition is false

for loop count controlled loop - a loop that repeats a specific number of times - designed to work with a sequence of data items.

for variable in [val1, val2, val3]:
  print(variable)

iteration - each execution of the body of the loop

target variable - the target of an assignment at the beginning of each iteration

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

for x in range(5):
  print('Hello World')

range(1,5) - 1 is the start value, 5 is the ending limit - prints 1 2 3 4

step value - if you add a 3rd argument to a range function it will increment that number of times

for num in range(1, 10, 2):
  print num

returns >> 1,3,5,7,9

accumulator - the variable used to keep the running total

running total - sum of numbers that accumulates with each iteration of the loop

  • a loop that reads each number in the series
  • a variable that accumulates the total of the numbers as they are read

augmented assignment operator += , -=, *=, %= (total = total + number) == total += number

sentinel value - a special value that marks the end of a sequence of values(you choose it and make it unique enough not to be confusing)

validation loop (error trap) (error handler)

  • input is read
  • loop is executed
  • if input data is bad the loop executes its block of statements
  • loop displays an error message to user

input validation - if input is invalid the program discards it and prompts user for the correct data

priming read - get users input and send it to the validation loop as a condition to be t or f If true run the program. If false prompt user for the correct data

nested loop - loop inside another loop

  • an inner loop goes through all of its iterations for every iteration of an outer loop
  • inner loops complete their iterations faster than outer loops
  • to get the total number of iteration of a nested loop - multiply the number of iteration of all the loops
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment