You should append all the values to a list, that allows you to easily iterate through the values later as well as not littering your namespace with useless variables and magic.
Answer from knutin on Stack OverflowYou should append all the values to a list, that allows you to easily iterate through the values later as well as not littering your namespace with useless variables and magic.
While using a list or dictionary is of course the right approach here, it is possible to create named variables, using exec(). You'll miss out on all the convenient operations that lists and dictionaries provide (think of len(), inserting, removing, sorting, and so on), but it's possible:
count = raw_input('Number of variables:')
for i in xrange(count):
exec('var_' + str(i) + ' = ' + str(i))
exec() accepts and executes strings that contain valid Python expressions. So the above piece of code is generating and executing code on the fly.
for i in range(4): print(i) i += 1
How can I make this print:
0
2
Python for loop increment - Stack Overflow
Changing a variable name in a for loop?
"I want to define several variables with trailing ascending numbers 0, 1, 2, etc." is a red flag that you really should be using a list.
More on reddit.comHow to increment the iterating variable within a for loop python - Stack Overflow
python - How do you create different variable names while in a loop? - Stack Overflow
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)
I wasn't really sure how to describe this, but bascially this is what I want to do:
a1=1;a2=2;a3=3
for i in [1,2,3]: print a(i)
I want this to print a1, a2, and a3. Is there a way to make that work?
"I want to define several variables with trailing ascending numbers 0, 1, 2, etc." is a red flag that you really should be using a list.
You probably want to defer to r/learnpython for these types of questions.
Nevertheless, it's not clear what you want to do. On the one hand, you could just make a a list and index into that:
a = [1, 2, 3]
for i in [0, 1, 2]:
print(a[i])
Or, probably what you want to do is put these things in a dictionary:
data = {'a1': 1, 'a2': 2, 'a3': 3}
for i in [1, 2, 3]:
print(data['a{}'.format(i)])
Sure you can; it's called a dictionary:
d = {}
for x in range(1, 10):
d["string{0}".format(x)] = "Hello"
>>> d["string5"]
'Hello'
>>> d
{'string1': 'Hello',
'string2': 'Hello',
'string3': 'Hello',
'string4': 'Hello',
'string5': 'Hello',
'string6': 'Hello',
'string7': 'Hello',
'string8': 'Hello',
'string9': 'Hello'}
I said this somewhat tongue in check, but really the best way to associate one value with another value is a dictionary. That is what it was designed for!
It is really bad idea, but...
for x in range(0, 9):
globals()['string%s' % x] = 'Hello'
and then for example:
print(string3)
will give you:
Hello
However this is bad practice. You should use dictionaries or lists instead, as others propose. Unless, of course, you really wanted to know how to do it, but did not want to use it.
I have a counter set up to notify me how many times I write to a file: amount_of_times = 1. However every-time I write to the file it is not incrementing, is there a way for me to get it to increment, so I know how many times I ran the program?
with open ('TextInfo.txt','w') as filer:
amount_of_times = 1
filer.write('I wrote one time')
filer.write('Again')
print('Executed amont of times: {}'.format(amount_of_times))
amount_of_times = amount_of_times + 1You need to define the negative variable before stepping into the loop:
negative = 0
for line in neg_file:
# no changes
The way you are proceeding, it is "created" every single loop.
I was re-setting positive and negative back to zero pre-maturely. They should be reset after the for word in words: loop.