Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Python Expert

 

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)

 

Python, Programming

  • Category:- Python
  • Reference No.:- M9506004

Have any Question?


Related Questions in Python

A software company sells a package that retails for 99

A software company sells a package that retails for $99. Quantity discounts are given according to the following table: Quantity Discount 10 - 19 20% 20 - 49 30% 50 - 99 40% 100 or more 50% Write a program using python t ...

Questionwhat is a python development frameworkgive 3

Question What is a python development framework? Give 3 examples python development framework used today. and explain which development framework is used in which industry.

Question write a simple python program that takes use

Question: Write a simple python program that takes use inputs as non-zero digits and converts them into binary form. The response must be typed, single spaced, must be in times new roman font (size 12) and must follow th ...

Environment setupthe first mini project will be based on

Environment Setup The first mini project will be based on Ladder Logic programming. We will be using Schneider Electric's IDE called SoMachine Basic to do the programming. The latest ver- sion of SoMachine Basic for Wind ...

Question why is software configuration management

Question : Why is software configuration management considered an umbrella activity in software engineering? Please include examples and supporting discussion. The response must be typed, single spaced, must be in times ...

Assignment1 utilising python 3 build the following

Assignment 1. Utilising Python 3 Build the following regression models: - Decision Tree - Gradient Boosted Tree - Linear regression 2. Select a dataset (other than the example dataset given in section 3) and apply the De ...

In this programming assignment you will write a client

In this programming assignment, you will write a client pingprogram in Python. Your client will send a simple ping message to a server, receive a correspondingpong message back from the server, and determine the delay be ...

Homework -this homework will have both a short written and

Homework - This homework will have, both a short written and coding assignment. The problems that are supposed to be written are clearly marked. 1) (Written) Make heuristics Describe two heuristics for the slide problem ...

Question research pythons dictionary data type dictdiscuss

Question : Research Python's dictionary data type (dict). Discuss its interface and usage. Include examples. Discuss practical applications of dictionaries.

Architecture and system integrationcase study queensland

Architecture and System Integration Case Study: Queensland Health - eHealth Investment Strategy After evaluating various platforms, Queensland Health finally decided to adopt a Service Oriented Architecture (SOA) for its ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As