Creative Commons License This work is licensed under a Creative Commons Attribution 4.0 International License. BS GIS Instructor: Inzamam Baig Lecture 5 Fundamentals of Programming
Iteration Updating variables: Updating a variable by adding 1 is called an increment >>> x = x + 1 Subtracting 1 is called a decrement >>> x = x - 1
The while statement n = 5 while n > 0: print(n) n = n - 1 print('Blastoff!') 1. Evaluate the condition, yielding True or False. 2. If the condition is false, exit the while statement and continue execution at the next statement. 3. If the condition is true, execute the body and then go back to step 1.
The body of the loop should change the value of one or more variables so that eventually the condition becomes false and the loop terminates If there is no iteration variable, the loop will repeat forever, resulting in an infinite loop
Infinite loops n = 10 while True: print(n, end=' ') n = n - 1 print('Done!') While this is a dysfunctional infinite loop, we can still use this pattern to build useful loops
Break Statement while True: line = input('> ') if line == 'done': break print(line) print('Done!')
Continue Statement while True: line = input('> ') if line[0] == '#': continue if line == 'done': break print(line) print('Done!')
For Loops Sometimes we want to loop through a set of things such as a list of words, the lines in a file, or a list of numbers departments = ['BS', 'GIS', 'KIU'] for department in departments: print('Department Name:', department) print('Done!')
Loop Patterns Often we use a for or while loop to go through a list of items or the contents of a file Or looking for something such as the largest or smallest value of the data we scan through
Loop Patterns These loops are generally constructed by: • Initializing one or more variables before the loop starts • Performing some computation on each item in the loop body, possibly changing the variables in the body of the loop • Looking at the resulting variables when the loop completes
Counting Items count = 0 for itervar in [3, 41, 12, 9, 74, 15]: count = count + 1 print('Count: ', count)
Counting and summing loops total = 0 for itervar in [3, 41, 12, 9, 74, 15]: total = total + itervar print('Total: ', total)
Maximum and Minimum largest = None print('Before:', largest) for itervar in [3, 41, 12, 9, 74, 15]: if largest is None or itervar > largest : largest = itervar print('Loop:', itervar, largest) print('Largest:', largest)
Maximum and Minimum smallest = None print('Before:', smallest) for itervar in [3, 41, 12, 9, 74, 15]: if smallest is None or itervar < smallest: smallest = itervar print('Loop:', itervar, smallest) print('Smallest:', smallest)

Python Lecture 5

  • 1.
    Creative Commons License Thiswork is licensed under a Creative Commons Attribution 4.0 International License. BS GIS Instructor: Inzamam Baig Lecture 5 Fundamentals of Programming
  • 2.
    Iteration Updating variables: Updating avariable by adding 1 is called an increment >>> x = x + 1 Subtracting 1 is called a decrement >>> x = x - 1
  • 3.
    The while statement n= 5 while n > 0: print(n) n = n - 1 print('Blastoff!') 1. Evaluate the condition, yielding True or False. 2. If the condition is false, exit the while statement and continue execution at the next statement. 3. If the condition is true, execute the body and then go back to step 1.
  • 4.
    The body ofthe loop should change the value of one or more variables so that eventually the condition becomes false and the loop terminates If there is no iteration variable, the loop will repeat forever, resulting in an infinite loop
  • 5.
    Infinite loops n =10 while True: print(n, end=' ') n = n - 1 print('Done!') While this is a dysfunctional infinite loop, we can still use this pattern to build useful loops
  • 6.
    Break Statement while True: line= input('> ') if line == 'done': break print(line) print('Done!')
  • 7.
    Continue Statement while True: line= input('> ') if line[0] == '#': continue if line == 'done': break print(line) print('Done!')
  • 8.
    For Loops Sometimes wewant to loop through a set of things such as a list of words, the lines in a file, or a list of numbers departments = ['BS', 'GIS', 'KIU'] for department in departments: print('Department Name:', department) print('Done!')
  • 9.
    Loop Patterns Often weuse a for or while loop to go through a list of items or the contents of a file Or looking for something such as the largest or smallest value of the data we scan through
  • 10.
    Loop Patterns These loopsare generally constructed by: • Initializing one or more variables before the loop starts • Performing some computation on each item in the loop body, possibly changing the variables in the body of the loop • Looking at the resulting variables when the loop completes
  • 11.
    Counting Items count =0 for itervar in [3, 41, 12, 9, 74, 15]: count = count + 1 print('Count: ', count)
  • 12.
    Counting and summingloops total = 0 for itervar in [3, 41, 12, 9, 74, 15]: total = total + itervar print('Total: ', total)
  • 13.
    Maximum and Minimum largest= None print('Before:', largest) for itervar in [3, 41, 12, 9, 74, 15]: if largest is None or itervar > largest : largest = itervar print('Loop:', itervar, largest) print('Largest:', largest)
  • 14.
    Maximum and Minimum smallest= None print('Before:', smallest) for itervar in [3, 41, 12, 9, 74, 15]: if smallest is None or itervar < smallest: smallest = itervar print('Loop:', itervar, smallest) print('Smallest:', smallest)

Editor's Notes

  • #4 This type of flow is called a loop because the third step loops back around to the top. For the above loop, we would say, “It had five iterations”, which means that the body of the loop was executed five times
  • #6 If you make the mistake and run this code, you will learn quickly how to stop a runaway Python process on your system or find where the power-off button is on your computer
  • #7 suppose you want to take input from the user until they type done, the loop runs repeatedly until it hits the break statement.
  • #8 you can use the continue statement to skip to the next iteration without finishing the body of the loop for the current iteration. All the lines are printed except the one that starts with the hash sign because when the continue is executed, it ends the current iteration and jumps back to the while statement to start the next iteration
  • #9 When we have a list of things to loop through, we can construct a definite loop using a for statement. We call the while statement an indefinite loop because it simply loops until some condition becomes False, whereas the for loop is looping through a known set of items so it runs through as many iterations as there are items in the set. department is the iteration variable for the for loop. The department friend changes for each iteration of the loop and controls when the for loop completes
  • #12 to count the number of items in a list
  • #13 there are built-in functions len() and sum() that compute the number of items in a list and the total of the items in the list respectively
  • #14 None is a special constant value which we can store in a variable to mark the variable as “empty”