Friday 20 September 2013

Learning Python-
Intermediate
Lesson 4 :Iteration and Tuples
Carter Draper
Sources: Zane Cochran, Allan Martell & Code AcademyLet’s get ready!
 List Review
 Using Functions With Lists
 Using the Entire List in a Function
 For Loop
 While Loop
 TuplesFor Loops
For loops are traditionally used
when you have a piece of code
which you want to repeat n number
of times.
Executes a sequence of statements
multiple times and abbreviates the
code that manages the loop
variable.
for letter in 'Python': # First Example
print 'Current Letter :', letterrange vs. xrange in for
loops
The range function creates a list
containing numbers defined by
the input
The xrange function creates a
number generator
The range function generates a
list of numbers all at once, where
as xrange generates them as
needed
for a in range(10):
print (a)
for x in xrange(5):
print xAssignment
Lesson 4
For Loops
ExercisesWhile Loops
A while loop statement in Python
programming language repeatedly
executes a target statement as long as
a given condition is true.
It tests the condition before executing
the loop body.
count = 0
while (count < 9):
print 'The count is:', count
count = count + 1Assignment
Lesson 4
While Loops
ExercisesTuples
A tuple is a sequence of
immutable Python objects. Tuples
are sequences, just like lists.
The only difference is that tuples
can't be changed i.e., tuples are
immutable and tuples use
parentheses and lists use square
brackets.
tup1 = ('physics', 'chemistry', 1997, 2000);Accessing Values in Tuples:
To access values in tuple, use the
square brackets for slicing along with
the index or indices to obtain value
available at that indexAssignment
Lesson 4
While Loops
ExercisesReflection

No comments:

Post a Comment