GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • w3resource
  • Front End
  • HTML
  • CSS
  • JavaScript
  • HTML5
  • Schema.org
  • php.js
  • Twitter Bootstrap
  • Responsive Web Design tutorial
  • Zurb Foundation 3 tutorials
  • Pure CSS
  • HTML5 Canvas
  • JavaScript Course

This view

  • Original page
  • New address

GeezerVison is showing www.w3resource.com — Python: Product of list - w3resource

Python: Product of list

Last update on May 17 2025 05:43:15 (UTC/GMT +8 hours)


List Product Calculator

Write a Python program to compute the product of a list of integers (without using a for loop).

Sample Solution-1:

Python Code:

# Import the 'reduce' function from the 'functools' module.
from functools import reduce

# Define a list named 'nums' containing a series of integers.
nums = [10, 20, 30]

# Print a message along with the original list of numbers.
print("Original list numbers:")
print(nums)

# Calculate the product of the numbers in the list 'nums' using the 'reduce' function
# and a lambda function that multiplies two numbers.
nums_product = reduce((lambda x, y: x * y), nums)

# Print the product of the numbers, obtained without using a for loop.
print("\nProduct of the said numbers (without using a for loop):", nums_product)

Sample Output:

Original list numbers:
[10, 20, 30]
Product of the numbers :  6000

Flowchart:

Flowchart: Compute the product of a list of integers.


Sample Solution-2:

Python Code:

# Import the 'math' module to access the 'prod' function.
import math

# Define a list named 'nums' containing a series of integers.
nums = [10, 20, 30]

# Print a message indicating the original list of numbers.
print("Original list numbers:")
print(nums)

# Calculate the product of the numbers in the list 'nums' using the 'prod' function
# from the 'math' module.
nums_product = math.prod(nums)

# Print the product of the numbers, obtained without using a for loop.
print("\nProduct of the said numbers (without using a for loop):", nums_product)

Sample Output:

Original list numbers:
[10, 20, 30]
Product of the numbers :  6000

Flowchart:

Flowchart: Compute the product of a list of integers.


For more Practice: Solve these Related Problems:

  • Write a Python program to compute the product of all even numbers in a list.
  • Write a Python program to compute the product of a list using recursion.
  • Write a Python program to compute the sum and product of all elements in a dictionary.
  • Write a Python program to find the factorial of a number using list product.

Go to:

  • Python Basic Exercises Home ↩
  • Python Exercises Home ↩

Previous: Write a Python program to filter the positive numbers from a list.
Next: Write a Python program to print Unicode characters.

Python Code Editor:

 

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.


  • Daily Coding Challenges & Projects
  • Weekly Trends and Language Statistics



You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.