It will increment by 1. When you use an iterator like range(), you assign the next value from the iterator each time, and whatever you assigned to i in the loop is ignored.

If you want it to double each time, don't use range(), do your own updating and end test.

i = 1
while (i < 100):
    print(i)
    i *= 2
Answer from Barmar on Stack Overflow
🌐
Stack Overflow
stackoverflow.com › questions › 46699420 › python-for-loop-multiplication
Python for loop multiplication - Stack Overflow
That also means you need to initialize the result with 1, not 0, because multiplying by 0 is always 0. n = int(input('Enter a negative number: ')) result = 1 for i in range(n, 0): result *= i print(result) ...
Discussions

For Loop in Python
Pardon if I have been posting series of questions. Newbie Python learner here, and am going through PCEP materials, and having some questions along the way… Thank you in advance for the help. In these two codes, I confused why the answer is such: Code 1: my_list = [0,1,2,3] x = 1 for elem ... More on discuss.python.org
🌐 discuss.python.org
0
0
September 7, 2023
How would you make a function that multiplies all the values in a list, baring in mind that a list could have 3 values or 5?
If you just learned loops, what you are looking for is a for loop. Since you mentioned functions we'll go ahead and make this a function. What do we want in our function? Well, we want it to take a list and return the multiplied values. NOTE: For simplicity, I'm going to assume your list is valid (numbers only), but in an actual function you'd want some error checking. How does our function definition look? def multiply_list_values(number_list): OK! We have the basics. So let's think through what we want to do. We want to go through each value of the list and multiply it with the previous total. We could also easily use recursion here, but since you didn't mention learning that I won't. It's fairly simple in either case. So how do you go through all the values of a list? Using a for...in loop. It will look something like this: def multiply_list_values(number_list): for number in number_list: print(number) If you run this, and have number_list as a list of numbers, you will see each individual number printed out in order. That's a good start, but it doesn't really do what we want. So lets think about our base case, where we want the very first number multiplied. If we were adding, we'd start at 0, but since we're multiplying, we need to start with 1, as 1 * X = X. Let's see how this looks: def multiply_list_values(number_list): result = 1 for number in number_list: print(result * number) Well, we get the same thing. What we need to do is multiply the value by each subsequent number to continually increment the result. How do we do this? We can assign a variable the result of a calculation involving itself. This doesn't work like a math equation. For example, if you had a variable x = 1 and wanted to add 2 to that value, you could do this: x = x + 2 and now x would be 3. So let's try the same thing with multiplication: def multiply_list_values(number_list): result = 1 for number in number_list: result = result * number print(result) So what do we have now? If you run this, you'll see the multiplied values getting higher and higher, and it will run through the whole list. Great! That's exactly what we want. Incidentally, this is such a common thing to do there is a shorthand way built into Python to make this easier. Instead of result = result * number you can remove the result on the right side and put your math operator before the equals sign like this: result *= number. This does the exact same thing. In our x example, you could change the x = x + 2 to x += 2 for the same result. Now that the math is finished, we just need to return the result after the loop, making our simplified and final function look like this (with test usage): def multiply_list_values(number_list): result = 1 for number in number_list: result = result * number return result def main(): number_list = [1, 2, 3, 4, 5] print(multiply_list_values(number_list)) # Output # 120 if __name__ == '__main__': main() There are many problems in Python (and programming in general) that can be solved this way. The for..in loop is extremely useful and can be used for all sorts of operations, not just mathematical ones, so keep it in mind whenever you want to do operations on every element of a list or dictionary. Let me know if you have questions! More on reddit.com
🌐 r/learnpython
17
2
February 28, 2024
How can I increment i with an additional value within a for loop?
Firstly, the i += 1 part of your code does nothing. Second, the range function can take up to three arguments. When you pass all three, the first is the starting number, the second is the last number (but remember that Python uses half-open intervals), and the last is the step. So you'd do for i in range(0, 4, 2): print(i) More on reddit.com
🌐 r/learnpython
11
2
May 27, 2022
Python for loop increment - Stack Overflow
How is this working and for loop increment doesn’t work? for i in range(0,10): if i == 3: i = i + 1 continue print(i) More on stackoverflow.com
🌐 stackoverflow.com
🌐
Reddit
reddit.com › r/learnpython › how to multiply printed for loop values with each other?
r/learnpython on Reddit: How to multiply printed for loop values with each other?
February 2, 2023 -

Hi,

I am trying to write a code to run factorials for any integer values inputted.

What works:

  1. Input a value statement (x)

  2. For loop (outputs the x value in descending order up to minimum value 1)

So now when I input 5, I get the list 5, 4, 3, 2,1

If I input 3, it prints 3, 2, 1

How can I get printed numbers to multiply with each other e.g., 5 * 4 * 3 * 2 * 1

When I input 3, I

🌐
Codecademy
codecademy.com › forum_questions › 55dfe28086f552612b00043f
how do i increment for loop in powers of 2? | Codecademy
my_list = [0,1,2,3,6,5,7] for i in range(0,len(my_list),2**i): print my_list[i] this code gives an error
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › how to increment for loop in python
How to Increment For Loop in Python - Spark By {Examples}
May 31, 2024 - To increment a for loop based on the length of a sequence, you can use the len() function to get the length of the sequence and then use the obtained length in the range of the for loop.
🌐
GeeksforGeeks
geeksforgeeks.org › python › ways-to-increment-iterator-from-inside-the-for-loop-in-python
Ways to increment Iterator from inside the For loop in Python - GeeksforGeeks
January 22, 2026 - As a result, modifying the loop variable inside the loop does not affect iteration, and controlled stepping must be handled explicitly. Example: Why Incrementing the Loop Variable Does Not Work
Find elsewhere
🌐
Python.org
discuss.python.org › python help
For Loop in Python - Python Help - Discussions on Python.org
September 7, 2023 - Pardon if I have been posting series of questions. Newbie Python learner here, and am going through PCEP materials, and having some questions along the way… Thank you in advance for the help. In these two codes, I confused why the answer is such: Code 1: my_list = [0,1,2,3] x = 1 for elem in my_list: x *= elem print (x) Answer: [0] Code 2: my_list = [0,1,2,3] x = 1 for elem in my_list: new_list = x * elem print (new_list) Answer: [3] Question 3: I want to create a code that multi...
Top answer
1 of 4
16
If you just learned loops, what you are looking for is a for loop. Since you mentioned functions we'll go ahead and make this a function. What do we want in our function? Well, we want it to take a list and return the multiplied values. NOTE: For simplicity, I'm going to assume your list is valid (numbers only), but in an actual function you'd want some error checking. How does our function definition look? def multiply_list_values(number_list): OK! We have the basics. So let's think through what we want to do. We want to go through each value of the list and multiply it with the previous total. We could also easily use recursion here, but since you didn't mention learning that I won't. It's fairly simple in either case. So how do you go through all the values of a list? Using a for...in loop. It will look something like this: def multiply_list_values(number_list): for number in number_list: print(number) If you run this, and have number_list as a list of numbers, you will see each individual number printed out in order. That's a good start, but it doesn't really do what we want. So lets think about our base case, where we want the very first number multiplied. If we were adding, we'd start at 0, but since we're multiplying, we need to start with 1, as 1 * X = X. Let's see how this looks: def multiply_list_values(number_list): result = 1 for number in number_list: print(result * number) Well, we get the same thing. What we need to do is multiply the value by each subsequent number to continually increment the result. How do we do this? We can assign a variable the result of a calculation involving itself. This doesn't work like a math equation. For example, if you had a variable x = 1 and wanted to add 2 to that value, you could do this: x = x + 2 and now x would be 3. So let's try the same thing with multiplication: def multiply_list_values(number_list): result = 1 for number in number_list: result = result * number print(result) So what do we have now? If you run this, you'll see the multiplied values getting higher and higher, and it will run through the whole list. Great! That's exactly what we want. Incidentally, this is such a common thing to do there is a shorthand way built into Python to make this easier. Instead of result = result * number you can remove the result on the right side and put your math operator before the equals sign like this: result *= number. This does the exact same thing. In our x example, you could change the x = x + 2 to x += 2 for the same result. Now that the math is finished, we just need to return the result after the loop, making our simplified and final function look like this (with test usage): def multiply_list_values(number_list): result = 1 for number in number_list: result = result * number return result def main(): number_list = [1, 2, 3, 4, 5] print(multiply_list_values(number_list)) # Output # 120 if __name__ == '__main__': main() There are many problems in Python (and programming in general) that can be solved this way. The for..in loop is extremely useful and can be used for all sorts of operations, not just mathematical ones, so keep it in mind whenever you want to do operations on every element of a list or dictionary. Let me know if you have questions!
2 of 4
4
Well, loops are indeed the right thing to do here. One approach would be to keep a running total: start with a value of 1, then loop through the list and multiply your total by the current value each time.
🌐
Datamentor
datamentor.io › python › for-statement
Python for Loop (With Examples)
The above code can create the multiplication table of any value that is provided as input. Python allows us to include one for loop inside another, known as nested for loop.
🌐
Reddit
reddit.com › r/learnpython › incrementing with a for loop
r/learnpython on Reddit: Incrementing with a for loop
June 19, 2021 -
import numpy as np
from pandas import *
import matplotlib
import math
import random
import statsmodels
from pandas_datareader import data as pdr
import yfinance as yf
yf.pdr_override()
###########################################################################################################

weights = read_excel('/home/user/Documents/weights.xlsx',index_col = 'Date') #loading weights file
tickers=(weights['RIC']) #load securities in portfolio
weights = (weights['Fund1'])
Results_data = DataFrame()
Position_size = 10000


new_data = DataFrame()
for t in tickers:
    new_data[t] = pdr.get_data_yahoo(t, start="2017-01-01", end="2021-04-30")['Adj Close'] # downloading pricing data


for t in tickers:
        Results_data[t] = (new_data[t]/new_data[t].shift(1))-1 #Calculation of simple return
        Position_size = (Position_size * (1 + Results_data))

New to python and I'm having a little bit of an issue with the last for loop.

I now understand that python resets my position size variable at each iteration unlike say vba in which it would not reset to 10000. Is there any known workaround for this that I'm missing ?

🌐
Quora
quora.com › How-do-you-code-a-for-loop-that-increments-by-two-numbers-at-once
How to code a for loop that increments by two numbers at once - Quora
In python the increment is controlled by the range function. To increment by 2, just add the increment parameter to range function. ... This prints the value of i after every loop, you will notice that the value of i is incremented by 2.
🌐
Alma Better
almabetter.com › bytes › tutorials › python › for-loop-in-python
For Loop in Python
December 13, 2023 - A for Loop needs a starting point, ending point, and increment to determine how many times it will run. For loops, iterate over a collection of data like arrays or lists. For loops are useful for performing operations on multiple data points ...
🌐
365 Data Science
365datascience.com › blog › tutorials › python tutorials › working with loops in python
Working With Loops in Python – 365 Data Science
April 24, 2023 - This is one of the strong points in our case and we should exploit this. The procedure: Go to the box of spaghetti in the first dictionary ... Multiply those two. This operation must be repeated for each food product.
🌐
Educative
educative.io › answers › how-to-create-a-multiplication-table-for-any-number-in-python
How to create a multiplication table for any number in Python
The range() function generates a sequence of numbers automatically. We can loop through this sequence, running specific code for each number until we reach the final number in the series.