How to multiply numbers in a range in python

I am brand new to Python, and I'm currently taking a free online course.

I'm stuck trying to figure out how to code this simple optional problem found in my course:

[ ] using range(x) multiply the numbers 1 through 7

1x2x3x4x5x6x7 = 5040

I have made a variable for the range, 7, and a variable for the product used in the loop:

x = 7
product = 0

for item in range(x):

This is where I'm stuck. I try things in the loop like:

product = item * item

which doesn't even make any sense. I'm having a total mental block right now. I realize this should be an easy thing to do, but can't for whatever reason. I've tried making other variables like "number" and "previous_number" to make things easier for me, but I just get confused.

I need to use a for loop in this solution. I'm just not understanding the logic right now. A solution and explanation would be great! Please help!

asked Jul 12, 2017 at 20:43

How to multiply numbers in a range in python

1

Check this out:

answer = 1

for i in range(1,8):
    print("Right now, answer is", answer)
    print("Right now, i      is", i)
    print("I am going to multiply answer ({}) with i ({}) and store the result in answer".format(answer, i))
    answer = answer * i
    print("Now, answer is", answer)

print("Finally, answer is", answer)

answered Jul 12, 2017 at 20:51

inspectorG4dgetinspectorG4dget

106k25 gold badges137 silver badges234 bronze badges

1

You need to initialise product = 1 (as 1 is the multiplicative identity) and inside your loop you simply need:

product *= item

which is short hand for:

product = product * item

In python there is also a reduce function that handles these types of questions:

In []:
import operator as op
import functools as ft

x = 7
ft.reduce(op.mul, range(1,x+1))

Out:
5040

answered Jul 12, 2017 at 21:02

How to multiply numbers in a range in python

AChampionAChampion

28.7k3 gold badges54 silver badges70 bronze badges

Try something like this:

x = 7
product = 1

for item in range(1, x + 1):
    product = item * product

print(product)

Note that product starts at one and the range function starts at one as well. Otherwise item will continue to be multiplied by zero.

answered Jul 12, 2017 at 20:50

digit = range(1,8)    
print("digit", list(digit))  
product = 1   
for item in digit:  
    product = product * item  
    print("product is",product)  

How to multiply numbers in a range in python

answered May 19, 2018 at 10:12

How to multiply numbers in a range in python

1

multiplication = 1
for number in range(1,8):
    multiplication *= number
digits = list(range(1,8))
print("Multiply these numbers in sequence: ", digits, "=", multiplication)

Tomerikoo

16.6k15 gold badges37 silver badges54 bronze badges

answered Jan 25, 2020 at 22:49

How to multiply numbers in a range in python

from functools import reduce

some_list = range(1, 8)

print(reduce(lambda x, y: x * y, some_list))

greybeard

2,1087 gold badges25 silver badges59 bronze badges

answered Dec 27, 2020 at 4:10

How to multiply numbers in a range in python

1

Try this:

user_number = int(input("Please enter a number: "))

number = 1

for i in range(1,user_number + 1):
    number = number * i

print(number)

How to multiply numbers in a range in python

answered Mar 20 at 17:10

How do you multiply a range?

1. Select a blank cell, says Cell E1, and type the formula =A1*$D$1 (A1 is the first cell of the range you will multiply with the same number, D1 is the cell with the specified number you will multiply with) into it, and press the Enter key.

How do you multiply a whole list in Python?

Use the syntax [element * number for element in list] to multiply each element in list by number ..
a_list = [1, 2, 3].
multiplied_list = [element * 2 for element in a_list].
print(multiplied_list).

How do you multiply multiple inputs in Python?

how to multiply inputs in python.
x = input("give me the number you want to multiply").
y = input("give me the second number you want to multiply").
y = int(y).
x = int(x).
print (y * x).

How do you multiply an array in Python?

multiply() in Python. numpy. multiply() function is used when we want to compute the multiplication of two array. It returns the product of arr1 and arr2, element-wise.