Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Go Programs
    • Python Programs
    • Java Programs

Python fsum

by suresh

The Python fsum math function is used to calculate and return the sum of iterates (Tuples and Lists). In this section, we show how to use fsum function in Python with example.

The syntax of the math fsum Function in Python Programming Language is

math.fsum(Iterators);

Number: It can be a number or a valid numerical expression.

  • If the number argument is a positive or negative number, the fsum function calculates and returns the sum of iterated (Tuples and Lists).
  • If the number argument is not a number, fsum function return TypeError.

Python fsum Function Example

The Python fsum function calculates and returns the sum of iterates. In this Python example, we are going to return the sum of iterates for different data types and display the output.

# Python fsum Function

import math

Tup = (10, 20, 12, -40 , 50) # Tuple Declaration
Lis = [-98, 32, -39, -42 , 15] # List Declaration

print('Calculating SUM of Tuple Item = %.2f' %math.fsum(Tup))
print('Calculating SUM of Tuple Item = %.2f' %math.fsum(Lis))

print('Calculating SUM of Tuple Items Directly = %.2f' %math.fsum((1, 2, 3, 4, 5)))
print('Calculating SUM of List Items Directly = %.2f' %math.fsum([10, 20, 30, 40]))

print('SUM of Decimal Tuple Items Directly = %.2f' %math.fsum((1.25, 2.45, 3.2, 4, 5.95)))
print('SUM of Decimal List Items Directly = %.2f' %math.fsum([2.45, 20.24, 3.95, 4.59]))

print('SUM of List of String Items Directly = %.2f' %math.fsum(['a', 'b', 'c', 'd']))
Python FSUM Function
  1. Within the first two statements, We used the Python Tuple and Python List items as the arguments for fsum Function. If you observe the above screenshot, the fsum function is working perfectly on them.
  2. Within the next two statements, we passed the Positive integer Tuples and Lists as the fsum Function argument. From the above screenshot, see that the fsum Function is returning output.
  3. Within the next two statements, we passed both the Positive and negative decimal Tuples and Lists as the fsum Function arguments. As we said before, the fsum Function is returning the output.
  4. Last, We tried fsum Math function on List of String values. As we said before, Python returns TypeError as output.

Placed Under: Python

  • Download and Install Python
  • Python Arithmetic Operators
  • Python Assignment Operators
  • Python Bitwise Operators
  • Python Comparison Operators
  • Python Logical Operators
  • Python If Statement
  • Python If Else
  • Python Elif Statement
  • Python Nested If
  • Python For Loop
  • Python While Loop
  • Python Break
  • Python Continue
  • Python Dictionary
  • Python datetime
  • Python String
  • Python Set
  • Python Tuple
  • Python List
  • Python List Comprehensions
  • Python Lambda Function
  • Python Functions
  • Python Types of Functions
  • Python Iterator
  • Python File Handling
  • Python Directory
  • Python Class
  • Python classmethod
  • Python Inheritance
  • Python Method Overriding
  • Python Static Method
  • Connect Python and SQL Server
  • Python SQL Create DB
  • Python SQL Select Top
  • Python SQL Where Clause
  • Python SQL Order By
  • Python SQL Select Statement
  • Python len Function
  • Python max Function
  • Python map Function
  • Python print Function
  • Python sort Function
  • Python range Function
  • Python zip Function
  • Python Math Functions
  • Python String Functions
  • Python List Functions
  • Python NumPy Array
  • NumPy Aggregate Functions
  • NumPy Arithmetic Operations
  • Python Numpy Bitwise operators
  • Numpy Comparison Operators
  • Numpy Exponential Functions
  • Python Numpy logical operators
  • Python numpy String Functions
  • NumPy Trigonometric Functions
  • Python random Array
  • Python numpy concatenate
  • Python numpy Array shape
  • Python pandas DataFrame
  • Pandas DataFrame plot
  • Python Series
  • Python matplotlib Histogram
  • Python matplotlib Scatter Plot
  • Python matplotlib Pie Chart
  • Python matplotlib Bar Chart
  • Python List Length
  • Python sort List Function
  • Python String Concatenation
  • Python String Length
  • Python substring
  • Python Programming Examples

Copyright © 2021· All Rights Reserved.
About | Contact | Privacy Policy