Python 2.7 Tutorial Pt 5

Python How ToIn this Python Video Tutorial I focus on looping in Python 2.5 through 2.7. I explain how to use the while loop and the many ways to use the for loop.

Along the way I explain how the in operator is used in Python and the range function.

Like always, the code follows the video. If you have any questions or comments leave them below. And, if you missed my other Python Tutorials they are available here:

The Code Used in the Above Video

#! /usr/bin/python

# While Loops

x = 1
while x <= 30:
print x
x += 1

# for loops

listCustNum = [0,1,2]
listCustName = [‘Bob Smith’,’Helen Jones’,’Mark Summers’]
listCustAge = [23,70,45]

for i in listCustNum:
print ‘%s is %d’ % (listCustName[i],listCustAge[i])

listEx = [1,2,3,4]

print 2 in listEx

for i in listEx:
print i

for i in range(1,31):
print i

listEx[:] = range(1,31)

for i in listEx:
print i

# Print just odd numbers

for i in listEx:
if (i%2) == 0:
print i

# Print odd numbers up till 25

for i in listEx:
if (i%2) == 0:
elif i == 25:
print i,

2 Responses to “Python 2.7 Tutorial Pt 5”

  1. ankit says:

    I am working on a program in which I have to read information from a frame arrive from a file(i.e 8 23 11 ). get it from sys.argv vector. then i have to enter all values(i.e 8 23 11) into a table. i have to do it again and again until i reach the last value of the input file. then i have to write the current state of my table to an output text file.this file must hae one line per port
    8 23 11 = port number(8),destination address(23) and source address(11) ? ?

Leave a Reply

Your email address will not be published.