Iteration over lists, Python Programming

Assignment Help:

 

What if you had a list of integer values, and you need to add  them  up and give the sum?  Here are a number of different types of doing  it.

First,  here  is a type in a style  you  may  have  learned to write  in a Java class (actually, you would have used  for, but Python does not have a for that works  like the one in C and Java).

 

def addList1(l):

sum  = 0

listLength = len(l)

i =  0

while (i < listLength):

sum  = sum + l[i]

i =  i + 1 return sum

 

It increments the index i from 0 through the length of the list - 1, and includes the appropriate components of the list into the sum.  This is perfectly right, but pretty verbose and easy to get wrong.

Here is a method of version using  Python's for loop.

 

def addList2(l):

sum  = 0

for i in range(len(l)):

sum  = sum + l[i]

return sum

 

A loop of the form

 

for x in l: something will be executed once for each element in the structure l, with the variable x having each successive element in l on each iteration. So,for  x in range(3): print x will print  0 1 2. Back to addList2,  we look that i will take on variables  from 0 to the length  of the list minus 1, and on every iteration, it will include the appropriate component from l into the addition.  This is more compact and simpler to get right than  the ?rst method of version, but still not the good one  we can do!

 

This one is even more direct.

 

def addList3(l):

sum  = 0

for  v in l:

sum  = sum + v return sum

 

We do not ever really need to work with the indices.  Here, the internal variable v gets on each successive integer  in l, and those values  are goes into sum.

 

For the truly  lazy, it turns out that  the function we need  is already built  into Python. It is known as sum:

 

def addList4(l):

return sum(l)

 


Related Discussions:- Iteration over lists

Procedure calls, Procedure calls When you compute an expression of the...

Procedure calls When you compute an expression of the form ( ,  ..., )   the Python interpreter treats  this as a procedure call. It will be simpler to talk about

Lcr circuit, program on damped lcr cicuit

program on damped lcr cicuit

Algorithms, how to calculate area of a square in square metres

how to calculate area of a square in square metres

Internal models, Internal models As we want to create  more and more c...

Internal models As we want to create  more and more complex  machine with  software programs as controllers, we search  that  it is often needful  to create  additional types

Angle between 2 moving line , how to calculate angle between 2 moving line ...

how to calculate angle between 2 moving line in 2d space

Non-local references in procedures, Non-local references in procedures ...

Non-local references in procedures There  is an important subtlety in the  way  names  are handled in the  environment created by a function call. When  a value  that  is not

Problem, an array A[0..n-1] of nr red elements, nw white elements and nb bl...

an array A[0..n-1] of nr red elements, nw white elements and nb blue elements in random order such that 0 = nr,nw,nb = n and nr + nw + nb = n, and arranging them such that all reds

Environments in python, Environments in Python Generally, Python estab...

Environments in Python Generally, Python establishes the following binding environments: 1.  builtin     : the mother of all environments: it contains the de?nitions of

Non-local references, Non-local references So far, whenever we needed ...

Non-local references So far, whenever we needed to compute a variable, there  was  a binding for that  variable in the 'local' environment (the environment in which  we were e

Create a program to produce a business speak phrase, The goal of this exerc...

The goal of this exercise is to write a \business phrase" generator, which each time it is called produces lines of business speak, such as: It's time that we became uber-effici

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd