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
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)
It seems that you want to use step parameter of range function. From documentation:
range(start, stop[, step]) This is a versatile function to create lists containing arithmetic progressions. It is most often used in for loops. The arguments must be plain integers. If the step argument is omitted, it defaults to 1. If the start argument is omitted, it defaults to 0. The full form returns a list of plain integers [start, start + step, start + 2 * step, ...]. If step is positive, the last element is the largest start + i * step less than stop; if step is negative, the last element is the smallest start + i * step greater than stop. step must not be zero (or else ValueError is raised). Example:
>>> range(10) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range(1, 11) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> range(0, 30, 5) [0, 5, 10, 15, 20, 25]
>>> range(0, 10, 3) [0, 3, 6, 9]
>>> range(0, -10, -1) [0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
>>> range(0) []
>>> range(1, 0) []
In your case to get [0,2,4] you can use:
range(0,6,2)
OR in your case when is a var:
idx = None
for i in range(len(str1)):
if idx and i < idx:
continue
for j in range(len(str2)):
if str1[i+j] != str2[j]:
break
else:
idx = i+j
You might just be better of using while loops rather than for loops for this. I translated your code directly from the java code.
str1 = "ababa"
str2 = "aba"
i = 0
while i < len(str1):
j = 0
while j < len(str2):
if not str1[i+j] == str1[j]:
break
if j == (len(str2) -1):
i += len(str2)
j+=1
i+=1