Lists in Python (how to add the elements) -


I have just started with Python. If I had previously had Haskell in Haskell, I had worked with most of the time / list in Python I want to do this

I have a list:

  l = [ 1,2,3,4]   

How can I add 4 elements to the list, so that I get 10 (1 + 2 + 3 + 4) as a result

I need recursive function and iterative (not as clean and stable as recycling, but still).

I did this in Haskell:

  sum [] = 0 index (x: xs) = x + sumlist x   

In Python I tried it:

  def list (l) if l == 0: # or no, I do not know the result 0 and do not know: l [0] + index (l) < / Code>  

but that's not working, maybe I'm still focusing on the Haskell's implementation style.

It would be nice if I get some help.

I have not used Haskell, but are rich in Python objects because they know that other objects How to work with You can understand this as an "interface between the object", and everyone is expected to behave well:

EAFP

For more apologies The simple Python coding style recognizes the existence of legitimate keys or properties and captures the exception if the assumption is false. This clean and sharp style exists except for many efforts and statements. The technique is available in many other languages ​​such as C.

Normally it is contradictory with the LBYL style, so the idea is that if there is a function in the standard amount , then you made sure It can be done at least two things:

  1. It shows how things that can be done .
  2. If it is not how to do it, it will increase the exception.

    Once you understand how it works, you can start doing anything to see what it does:

      & gt; & Gt; & Gt; Zodiac ((1,2,3)) # List 6 & gt; & Gt; & Gt; Yoga ([1,2,3]) # A tuple 6> and gt; & Gt; Zodiac ((1,)) # A tube with an object 1 & gt; & Gt; & Gt; The amount ([2]) # is a list with an object 2   

    so long as the item is movable , the amount can talk about it, of course, when If you pass something with which this can not work, you get the appropriate exception:

      gt; & Gt; Zodiac (1) traceback (most recent call final): File "& lt; stdin>", line 1, & lt; Module & gt; TypeError: The 'int' object is not iterable & gt; & Gt; & Gt; Zodiac ('1') traceback (most recent call final): File "& lt; stdin>", line 1, & lt; Module & gt; Unsupported operand type (s) for typeError: +: 'int' and 'str'   

    Note that the string is in Python, so you get a different exception.

    Has it been summarized in another way, or is it just this?

    Describing the importance of what kind of guide to "Pythonic" code, which states:

    There should be one - and Preferably the only one - the easiest way to do this

    The obvious way to include things will be with the function named yoga .

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -