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 OverflowIt 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
i will start from the first value of the sequence generated by iterator (range() in this case) and then it will get multiplied by 2 and it'll retain this value until the next iteration of loop at which it will get the next value of the sequence generated by iterator range() so,
for i in range(5):
print("Before update")
print(i)
i*=2
print("After update")
print(i)
Output:
Before Update
0
After Update
0
Before Update
1
After Update
2
Before Update
2
After Update
4
Before Update
3
After Update
6
Before Update
4
After Update
8
Before Update
5
After Update
10
Videos
for i in range(4): print(i) i += 1
How can I make this print:
0
2
Hi,
I am trying to write a code to run factorials for any integer values inputted.
What works:
Input a value statement (x)
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
For the specific case of doubling each time, or for other regular sequences, you can use comprehension to achieve the same effect:
for i in (2**i for i in range(0, 7)): print i
Note that this is really just the same as:
for i in range(0, 7): print 2**i
In more general cases, however, you can use a while loop, as shown in another answer, or create your own generator function for use in the loop.
you can use the first option you mentioned only if you want to increment i by 2 each time, i.e:
for i in range (1,65,2):
pay attention that if you want i to also be equel to 64 you have to end your range at 65. the other option is you want i to be doubled each time:
i=1
while i<=64:
#some code comes here
i*=2
once again if you want 64 to be part of the loop you need to see where the loop ends (i.e at what value of i)
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 ?
I'm a beginner so I was just wondering how you would do this, please dont use any modules :), I have just learned loops and functions if that helps.
for the code,
for i in range(0,10):
if i == 3:
i = i + 1
continue
print(i)
the output is going to be,
0
1
2
4
5
6
7
8
9
Breaking down the code,
for i in range(0, 10)
for loop runs for i=0 to i=9, each time initializing i with the value 0 to 9.
if i == 3:
i = i + 1
continue
print(i)
when i = 3, above condition executes, does the operation i=i+1 and then continue, which looks to be confusing you, so what continue does is it will jump the execution to start the next iteration without executing the code after it in the loop, i.e. print(i) would not be executed.
This means that for every iteration of i the loop will print i, but when i = 3 the if condition executes and continue is executed, leading to start the loop for next iteration i.e. i=4, hence the 3 is not printed.
In the provided code when you try to use i in a for loop with range, it always changes to the number provided by range in the function without bothering to look at the increment made to i. so basically if you try list(range(0, 10)) this will give you [0, 2, 3, 4, 5, 6, 7, 8, 9]. so for goes through that list one by one without thinking if any changes were made to i or not.
which if seen
loop_1: i=0
loop_2: i=1
loop_3: i=2
loop_4: i=3 (here now you increment value by 1), i=4
loop_5: i=4 (but the for loop was going though the list from range function so the change didn't do anything)
loop_6: i=5 (and so on until 9)