<< Chapter < Page Chapter >> Page >
This module introduces control flow in general and explains the use of the while loop in Python.

Table of contents

Preface

This module is one in a collection of modules on Python designed for teaching ITSE 1359 Introduction to Scripting Languages: Python at Austin Community College in Austin, TX.

What you have learned

Earlier modules in this collection have included instruction on the following topics:

  • numbers
  • variables
  • identifiers
  • strings
  • scripts
  • lists
  • tuples

This has prepared you to use Python as a very fancy desk calculator.

What you will learn

The next step in learning to program with Python is to learn how to apply repetition, modularity, and decision logic to your scripts. This is oftenreferred to in computer jargon as control flow .

This module introduces control flow in general and explains the use of the while loop in Python as an example of control flow .

Viewing tip

I recommend that you open another copy of this module in a separate browser window and use the following links to easily find and view the Figuresand the Listings while you are reading about them.

(Note to blind and visually impaired students: Most of the Figures and all of the Listings inthis module are presented in plain text format and should be accessible using an audio screen reader or a braille display. Note however that the requiredindentation may not be properly represented by an audio screen reader.)

Figures

  • Figure 1 . Output from the script in Listing 1.
  • Figure 2 . Output from the script in Listing 2.
  • Figure 3 . Visualization of the program.

Listings

  • Listing 1 . Typical script from earlier modules.
  • Listing 2 . Use a while loop to manipulate a list.

General background information

A typical script in the earlier modules looked something like the one shown in Listing 1 .

Listing 1 . Typical script from earlier modules.
# Illustrates extracting a # list element and extracting# elements from a nested list ##------------------------------- print("Create and print a list")listA = [100,200,300,400,500] print(listA)print("Original length is:") print(len(listA))print("Replace an element") listA[2]= [2,4,8,16,32,64] print("Print the modified list")print(listA) print("Modified length is:")print(len(listA)) print("Extract and display each")print(" element in the list") print(listA[0]) print(listA[1]) print(listA[2]) print(listA[3]) print(listA[4]) print("Extract and display each")print(" element in nested list") print(listA[2][0])print(listA[2][1]) print(listA[2][2])print(listA[2][3]) print(listA[2][4])print(listA[2][5])

The output produced by the script in Listing 1 is shown in Figure 1 .

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Itse 1359 introduction to scripting languages: python. OpenStax CNX. Jan 22, 2016 Download for free at https://legacy.cnx.org/content/col11713/1.32
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Itse 1359 introduction to scripting languages: python' conversation and receive update notifications?

Ask