Loops Python - Nick Reynolds September 31, 2017 Loops This - - PowerPoint PPT Presentation

loops
SMART_READER_LITE
LIVE PREVIEW

Loops Python - Nick Reynolds September 31, 2017 Loops This - - PowerPoint PPT Presentation

Loops Python - Nick Reynolds September 31, 2017 Loops This Class Loop Types Assignment 1 Loops Whats next on the list? Whats a loop? I have a shopping list: Whats next on the list? Apple Banana


slide-1
SLIDE 1

Loops

Python - Nick Reynolds September 31, 2017

slide-2
SLIDE 2

This Class

  • Loops
  • Loop Types
  • Assignment 1
slide-3
SLIDE 3

Loops

slide-4
SLIDE 4

What’s a loop?

I have a shopping list:

  • Apple
  • Banana
  • Orange

I want to pick up each

  • ne in order.

Logically I loop through each until I’m done.

What’s next on the list? What’s next on the list? What’s next on the list? What’s next on the list? Nothing! Finished!

slide-5
SLIDE 5

For Loops

  • Most common loop in

Python >>> shopping = ['Apple', 'Orange', 'Banana'] >>> for item in shopping: ... print(item) Apple Orange Banana

slide-6
SLIDE 6

>>> shopping = ['Apple', 'Orange', 'Banana'] >>> for item in shopping: ... print(item) Apple Orange Banana

For Loops

Temporary loop variable

  • Most common loop in

Python

  • Item is a temporary variable

that will hold the value for each loop. e.g. Loop 1: Item = ‘Apple’ Loop 2: Item = ‘Orange’ Loop 3: Item = ‘Banana’

slide-7
SLIDE 7

>>> shopping = ['Apple', 'Orange', 'Banana'] >>> for item in shopping: ... print(item) Apple Orange Banana

For Loops

Temporary loop variable >>> for <variable> in <list>: ... # do stuff

  • Most common loop in

Python

  • Item is a temporary variable

that will hold the value for each loop. e.g. Loop 1: Item = ‘Apple’ Loop 2: Item = ‘Orange’ Loop 3: Item = ‘Banana’

slide-8
SLIDE 8

Pen and Paper

Checkpoint

slide-9
SLIDE 9

While Loops

  • Continues until the condition

is false

  • Used when the loop

condition is more complex Loop 1: Item = ‘Apple’ Loop 2: Item = ‘Orange’ Loop 3: Item = ‘Banana’ >>> shopping = ['Apple', 'Orange', 'Banana'] >>> n = 0 >>> while n < len(shopping): ... print(shopping[n]) ... n = n + 1 Apple Orange Banana

slide-10
SLIDE 10

>>> shopping = ['Apple', 'Orange', 'Banana'] >>> n = 0 >>> while n < len(shopping): ... print(shopping[n]) ... n = n + 1 Apple Orange Banana

While Loops

Condition

  • Item is a temporary variable

that will hold the value for each loop. e.g. Loop 1: Item = ‘Apple’ Loop 2: Item = ‘Orange’ Loop 3: Item = ‘Banana’ >>> while <condition>: ... # do stuff

slide-11
SLIDE 11

Pen and Paper

Checkpoint

slide-12
SLIDE 12

Practical

slide-13
SLIDE 13

Assignment 1

slide-14
SLIDE 14

References

  • http://pwp.stevecassidy.net/python/more-

python.html

  • https://thenounproject.com/