Introduction Python Fsum() Method with Examples

Introduction Python Fsum() Method with Examples

math.fsum() method is a library method of math module, it is used to find the sum (in float) of the values of an iterable, it accepts an iterable object like an array, list, tuple, etc (that should contain numbers either integers or floats), and returns sum in float of all values.

math.fsum() method is a library method of math module, it is used to find the sum (in float) of the values of an iterable, it accepts an iterable object like an array, list, tuple, etc (that should contain numbers either integers or floats), and returns sum in float of all values.

Python Fsum()

Syntax

    math.fsum(iterable)

Here, the value can be a range or an iterable like an array, tuple.

Parameters:

iterable – an iterable object like list, array, tuple, etc.

Return value:

The fsum() function returns the sum of the iterable or range in floating-point number. ‘f’ in fsum() stands for float.

Examples of Python Fsum() method

Example 1

# Python code demonstrate example of 
# math.fsum() method
import math

# iterable objects
a = range(10)   # a range object (0,10)
b = [10, 20, 30, 40, 50]    # list of integers
c = [10, 20, 30.30, 40, 50.0]   # list of integers & floats
d = [10.20, 30.40]  # list of floats
e = (10, 20, 30, 40.50) # tuple

# printing sum of all values of the iterable objects
print("fsum(a): ", math.fsum(a))
print("fsum(b): ", math.fsum(b))
print("fsum(c): ", math.fsum(c))
print("fsum(d): ", math.fsum(d))
print("fsum(e): ", math.fsum(e))

Output

fsum(a):  45.0
fsum(b):  150.0
fsum(c):  150.3
fsum(d):  40.599999999999994
fsum(e):  100.5

Example 2

# app.py

#importing math library
import math

#Finding sum of a range first

#taking input from the user
x=int(input("Please enter a range up to which you want to sum: "))

#finding sum of the range using fsum()
print("Sum of the range is: ",math.fsum(range(x)))

#Finding sum of an iterable

#taking input of a list 
print("Please enter array element with space separated integer: ")
arr=list(map(int,input().split(" ")))

#printing all array elements
print("All array elements are: ",arr)

#Finding sum of all the values using fsum()
print("Sum of all elements of the array is: ",math.fsum(arr))

Output

Please enter a range up to which you want to sum: 15
Sum of the range is:  105.0
Please enter the array element with space-separated integer:
1 3 5 7
All array elements are:  [1, 3, 5, 7]
Sum of all elements of the array is:  16.0

In this program, we first have an imported math library, and then we have taken an input integer from the user. After that, we have calculated the sum of all elements up to that number, and then we have printed that.

On the other hand, we have taken input for a list, where the user will have to given all list elements by a space. Then we have called fsum() to calculate the sum of all the list elements.

Python fsum() is one of the Python Math function that is used to calculate and return the sum of iterators like Tuples and Lists.

Thanks for reading !

Guide to Python Programming Language

Guide to Python Programming Language

Guide to Python Programming Language

Description
The course will lead you from beginning level to advance in Python Programming Language. You do not need any prior knowledge on Python or any programming language or even programming to join the course and become an expert on the topic.

The course is begin continuously developing by adding lectures regularly.

Please see the Promo and free sample video to get to know more.

Hope you will enjoy it.

Basic knowledge
An Enthusiast Mind
A Computer
Basic Knowledge To Use Computer
Internet Connection
What will you learn
Will Be Expert On Python Programming Language
Build Application On Python Programming Language

Python Programming Tutorials For Beginners

Python Programming Tutorials For Beginners

Python Programming Tutorials For Beginners

Description
Hello and welcome to brand new series of wiredwiki. In this series i will teach you guys all you need to know about python. This series is designed for beginners but that doesn't means that i will not talk about the advanced stuff as well.

As you may all know by now that my approach of teaching is very simple and straightforward.In this series i will be talking about the all the things you need to know to jump start you python programming skills. This series is designed for noobs who are totally new to programming, so if you don't know any thing about

programming than this is the way to go guys Here is the links to all the videos that i will upload in this whole series.

In this video i will talk about all the basic introduction you need to know about python, which python version to choose, how to install python, how to get around with the interface, how to code your first program. Than we will talk about operators, expressions, numbers, strings, boo leans, lists, dictionaries, tuples and than inputs in python. With

Lots of exercises and more fun stuff, let's get started.

Download free Exercise files.

Dropbox: https://bit.ly/2AW7FYF

Who is the target audience?

First time Python programmers
Students and Teachers
IT pros who want to learn to code
Aspiring data scientists who want to add Python to their tool arsenal
Basic knowledge
Students should be comfortable working in the PC or Mac operating system
What will you learn
know basic programming concept and skill
build 6 text-based application using python
be able to learn other programming languages
be able to build sophisticated system using python in the future

To know more:

Learn Python Programming

Learn Python Programming

Learn Python Programming

Description
Learn Python Programming

Learn Python Programming and increase your python programming skills with Coder Kovid.

Python is the highest growing programming language in this era. You can use Python to do everything like, web development, software development, cognitive development, machine learning, artificial intelligence, etc. You should learn python programming and increase your skills of programming.

In this course of learn python programming you don't need any prior programming knowledge. Every beginner can start with.

Basic knowledge
No prior knowledge needed to learn this course
What will you learn
Write Basic Syntax of Python Programming
Create Basic Real World Application
Program in a fluent manner
Get Familiar in Programming Environment