Python 102 - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Python 102

Description:

Python 102 For Dr. Bernard Chen University of Central Arkansas IT Academic For loop The for loop is a generic sequence iterator in Python It can step through ... – PowerPoint PPT presentation

Number of Views:90
Avg rating:3.0/5.0
Slides: 18
Provided by: bernar5
Learn more at: http://www.cs.gsu.edu
Category:
Tags: python

less

Transcript and Presenter's Notes

Title: Python 102


1
Python 102 For
  • Dr. Bernard Chen
  • University of Central Arkansas
  • IT Academic

2
For loop
  • The for loop is a generic sequence iterator in
    Python
  • It can step through the items in ANY ordered
    sequence object

3
For loop
  • The Python for loop begins with a header line
    that specifies an assignment target along with an
    object that you want to step through
  • for lttargetgt in ltobjectgt
  • ltstatementgt
  • When Python runs a for loop, it assigns item in
    the sequence object to the target one by one,
    and then executes the loop body

4
For loop
  • The name used as the assignment target in a for
    header line is usually a variable in the scope
    where the for statement is coded
  • After the loop, this variable normally still
    refers to the last item visited

5
For loop examples
  • The name x is assigned to each of the three items
    in the list in turn, from left to right
  • gtgtgt aaspam, eggs, ham
  • gtgtgt for i in aa
  • print i

6
For loop examples
  • gtgtgt aa1,2,3,4
  • gtgtgt sum0
  • gtgtgt for i in aa
  • sum sum i
  • gtgtgt product1
  • gtgtgt for i in aa
  • product product i
  • gtgtgt sum, product

7
For loop examples
  • The name x is assigned to each of the three items
    in the list in turn, from left to right
  • gtgtgt sstring in python
  • gtgtgt for i in s
  • print i

8
Loop Variations
  • There are also situations where you will need to
    iterate in a more specialized way in the loop
    operation.
  • For example, what if you need to visit every
    second or third item in a list?
  • We have range function to help

9
Counter loops range
  • The range function is really independent of for
    loops although its used most often to generate
    indexes in a for loop
  • There are three formats of range
  • gtgtgt range(5)
  • gtgtgt range(2,5)
  • gtgtgt range(0,10,2)

10
Counter loops range
  • With one argument, range generates a list with
    integers from zero up to but NOT including the
    arguments value
  • If you pass in two arguments, the first is taken
    as the lower bound
  • An optional third argument can give a step if
    used, Python adds the step to each successive
    integer in the result

11
Counter loops range
  • gtgtgt range(5)
  • 0, 1, 2, 3, 4
  • gtgtgt range(2,5)
  • 2, 3, 4
  • gtgtgt range(0,10,2)
  • 0, 2, 4, 6, 8

12
Counter loops range
  • Ranges can also be non-positive, and
    non-ascending, if you want them to be
  • gtgtgt range(-5,5)
  • -5, -4, -3, -2, -1, 0, 1, 2, 3, 4
  • gtgtgt range(5,-5,-1)
  • 5, 4, 3, 2, 1, 0, -1, -2, -3, -4

13
range in for loop
  • gtgtgt sum0
  • gtgtgt for i in range(100)
  • sum sum i
  • gtgtgt product1
  • gtgtgt for i in range(5,10)
  • product product 10

14
range in for loop
  • gtgtgt aa1,2,3,4
  • gtgtgt sum0
  • gtgtgt for i in aa
  • sum sum i
  • gtgtgt sum0
  • gtgtgt for i in range(len(aa))
  • gtgtgt sum sum aai

15
range in for loop
  • aa 90,80,75,60,80,77,65,30,50,100
  • If we only want to compute the sum of first 5
    scores
  • gtgtgt sum0
  • gtgtgt for i in range(5)
  • gtgtgt sumsum aai
  • If we only need the even number students score
  • gtgtgt sum0
  • gtgtgt for i in range(0,len(aa),2)
  • gtgtgt sumsum aai

16
For loop examples
  • Please Draw the following figure by using for
    loop

17
Python Official Sites
  • Python Tutorial
  • http//docs.python.org/tut/
  • Python Official Site
  • http//python.org/
  • Download Python Latest Version
  • http//python.org/download/
Write a Comment
User Comments (0)
About PowerShow.com