We can make use of the the built-in sum() function. Let's take a look at a few examples.
Example 1: Calculate the sum of a list of integers
int_numbers = [11, 5, 2, 17, 32, 81, 52]
sum_of_int_list = sum(int_numbers)
print(f"Sum of {int_numbers}: ", sum_of_int_list)
Output:
Sum of [11, 5, 2, 17, 32, 81, 52]: 200
Example 2: Calculate the sum of a list of floats
float_numbers = [11.2, 5.11, 2.75, 17.56, 32.43, 81.45, 52.24]
sum_of_float_list = sum(float_numbers)
print(f"Sum of {float_numbers}: ", sum_of_float_list)
Sum of [11.2, 5.11, 2.75, 17.56, 32.43, 81.45, 52.24]: 202.74
Example 3: Sum of random numbers

-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Python,
- Python: Convert Date to DateTime
- How to sort a List using Lambda in Python
- Python matplotlib segmentation fault: 11 macOS Big Sur
- What is Terminal Velocity and its Formula? How to calculate it programmatically?
- How to install Python 3.11 on Mac
- How to flatten a nested list in Python
- Python: Pandas Merge DataFrames on Index Example
- How to Run all Cells at Once Jupyter Notebook
- Python - Convert float to String
- How to add borders to tkinter label text
- How to Exit a Loop in Python Code
- [Python] Fix: ValueError: All arrays must be of the same length
- Sorting an array using Bubble Sort in Python Programming
- How to Unzip a file using Python
- Python: Merge DataFrames Pandas Outer Join Example
- Change label (text) color in tkinter
- Convert Float to String in Python
- Fix: fatal error: No such file or directory compilation terminated
- Python: Access index/counter of a for loop iteration
- Import Other Python Files Examples
- How to install Anaconda on Mac (M1/M2 Mac)
- Python Regular Expression to Find All Matches in List
- How to Read a binary File with Python
- How to disable warnings while Python file execution
- Know current Python Version
More Posts:
- Java 8 Predicate Functional Interface Examples - Java
- Maven Eclipse (M2e) No archetypes currently available - Java
- Android Studio setup was canceled - How to resume - Android-Studio
- How to download Microsoft Visual Studio for M1/M2 Mac - MacOS
- Fix: Error: Cannot find module /node-examples/init - JavaScript
- How to retrieve all SharePoint Online List Items using Rest API - SharePoint
- Parse XML file in Java using DOM Parser - Java
- Fix: java.lang.UnsupportedOperationException Java Collections (List, Set, Map) - Java