How to add float numbers to a list in python

I think you are misunderstanding the split function and what it does.

$ python
Python 3.5.1 |Anaconda 4.0.0 [64-bit]| [default, Feb 16 2016, 09:49:46] [MSC v.1900 64 bit [AMD64]] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> line = "10 9 8 7 1 2 3 4"
>>> line_str = line.split[]
>>> line_str
['10', '9', '8', '7', '1', '2', '3', '4']

split takes the string and at every space splits the string into substrings, each substring is an element in the list

Your loop code is behaving like you need to find the individual numbers in the string.

>>> for element in range[len[line_str]]:
...         line_str[element]=float[line_str[element]]
...         line_str[element]=math.pow[line_str[element],2]
...         total=sum[line_str[0:len[element]]]
...         print[total]
...
Traceback [most recent call last]:
  File "", line 4, in 
TypeError: object of type 'int' has no len[]

So now lets loop through the list [using enumerate as suggested above] and convert each element to a float, square it using math.pow and store it back in the same list. The you can pass the list to sum[] to get your total. The beauty of Python is that you can always drop down into the interpreter and run your commands one by one and investigate the results.

>>> import math
>>> line = "10 9 8 7 1 2 3 4"
>>> line_str = line.split[]
>>> line_str
['10', '9', '8', '7', '1', '2', '3', '4']
>>> for i,value in enumerate[line_str]:
...   fvalue = float[value]
...   line_str[i] = math.pow[fvalue,2]
...
>>> line_str
[100.0, 81.0, 64.0, 49.0, 1.0, 4.0, 9.0, 16.0]
>>> sum[line_str]
324.0
>>>

Now the question you may one day ask yourself is, why did I want to use math.pow[fvalue,2] when I could have just done fvalue**2 or even fvalue*fvalue

When you are ready you can read about it here Exponentials in python x.**y vs math.pow[x, y]

Python is a very flexible language and allows you to add just about any type of data to a list, regardless of the data type of any of the other elements in the list. One common data type to add to a list is the float data type.

Append

In order to add a float to a list, we can simply use the append[] method:

>>> float_list = [1.0, 2.5, 3.9]
>>> float_list.append[11.2]
>>> float_list
[1.0, 2.5, 3.9, 11.2]

Extend

If you have more than one float to add to your list, or if you have an existing list of floats, you can use the extend[] method:

>>> float_list = [1.0, 2.5, 3.9]
>>> float_list.extend[[11.2, 12.3, 13.4]]
>>> float_list
[1.0, 2.5, 3.9, 11.2, 12.3, 13.4]

The extend[] method is different from append[] in that it takes an iterable of values, like a list, and adds them to the end of the target list. In essence, this is an easy way to combine two lists together.

Insert

The insert[] method allows you to insert a value into a list at a specific index. For example, the following code inserts the value 11.2 at index 3:

>>> float_list = [1.0, 2.5, 3.9]
>>> float_list.insert[3, 11.2]
>>> float_list
[1.0, 2.5, 3.9, 11.2]

Obviously this is more useful than the previous two methods if you need to add the float value to a specific spot in the list, as opposed to the end of the list.

Python sum[] function is used to get the sum of numbers of an iterable.

Python sum[]

Python sum[] function syntax is:

sum[iterable[, start]]

start is an optional number with default value of 0. If start is provided, then the sum of start and all the numbers in the iterable is returned.

Python sum[] list of numbers

s = sum[[1, 2, 3]]
print[s]

s = sum[[1, 2, 3], 10]
print[s]

Output:

6
16

Note that sum[] method doesn’t take keyword arguments, so if we write sum[[1, 2, 3], start=10] then it will throw exception as TypeError: sum[] takes no keyword arguments.

Python sum of a sequence of integers

Since sum accepts iterable as argument, we can pass tuple, bytes of numbers too.

s = sum[bytes[[1, 2]]]
print[s]

s = sum[bytearray[[1, 2]], 10]
print[s]

# sum of integers in different formats, tuple of numbers
s = sum[[1, 0b11, 0o17, 0xFF]]
print[s]

s = sum[[1, 0b11, 0o17, 0xFF], 0xF]
print[s]

Output:

3
13
274
289

Python sum of floats

s = sum[[1.5, 2.5, 3]]
print[s]

Output: 7.0 If you want to add floating point values with extended precision, you can use math.fsum[] function.

Python sum of complex numbers

sum[] function works with complex numbers too.

s = sum[[1 + 2j, 3 + 4j]]
print[s]

s = sum[[1 + 2j, 3 + 4j], 2 + 2j]
print[s]

s = sum[[1 + 2j, 2, 1.5 - 2j]]
print[s]

Output:

[4+6j]
[6+8j]
[4.5+0j]

You can checkout complete python script and more Python examples from our GitHub Repository.

Reference: Official Documentation

How do I add a list of numbers in a list in Python?

How to add Elements to a List in Python.
append[] : append the element to the end of the list..
insert[] : inserts the element before the given index..
extend[] : extends the list by appending elements from the iterable..
List Concatenation: We can use the + operator to concatenate multiple lists and create a new list..

How do you add a float value?

Example 1.
public class FloatSumExample1 {.
public static void main[String[] args] {.
Float f1 = 562.827f;.
Float f2 = 900.981f;.
// returns the sum of f1 and f2..
Float f3 = Float.sum[f1,f2];.
System.out.println["Number I = "+f1];.
System.out.println["Number II = "+f2];.

Chủ Đề