This question already has answers here:
Dynamic variable in Python [duplicate]
(2 answers)
Closed 8 years ago.
I would like to know how to create lots of variables by looping it. I know other people have asked this before but everyone who knows says you need a good reason for it and to just set it in a dictionary. My reason is that I need to assign up to 6156119580207157310796674288400203776 variables and there is no way I can do that by typing them out.
I need something like:
while counter < 1000:
try[counter] = counter
So that I could do this:
>>> try837
837
>>>try453
453
etc.
(this is an example not the exact code but any answer for this will solve my problem)
I would also like to know why people are opposed to answering this particular question. I don't want to tax my computer more than I already am by assigning this many variables so if it is an issue that could harm my computer or my code I would like to know.
You don't want to do this. Create a dictionary with a key for each suffix that you would use. Then use try[557] in place of the variable try557.
>>> try_ = dict((counter, counter) for counter in range(1000))
>>> print try_[557]
557
I'm using the standard technique of affixing an underscore to the otherwise reserved word "try".
(I'm ignoring the ludicrously large number of variables you claim to need.)
Related
This question already has answers here:
How do I do a case-insensitive string comparison?
(15 answers)
Closed last year.
import time
while True:
npc=input("Josuke\n\nJotaro Kujo (Part 4)\n\nChoose NPC to talk to.")
if npc=="Josuke" or npc=="josuke":
confirm=input("[Press E to interact.]")
elif npc=="jp4" or npc=="JP4" or npc=="Jp4
Within this code you can see that there are 2 NPCs to interact to. Because there are many ways of addressing the name Jotaro Kujo (Part 4), the if statement has many "or"s, but I want to be able to condense it. Could I use an array to be able to put the possibilities in and then have the if statement identify if the value is within the array? (I haven't completed the code yet, but the problem doesn't require the full code to be completed.)
Yes, you can do it easily using the in operator to check if a particular string is in the list.
as an example:
lst = ["bemwa", "mike", "charles"]
if "bemwa" in lst:
print("found")
And if all the possibilities you want to cover are related to case-insensitivity you can simply convert the input to lower-case or upper-case and compare it with only one possibility.
This question already has answers here:
How to convert string to variable name?
(3 answers)
Closed 1 year ago.
I want to run a function of a module with a variable name for example:
Plugins.SomeUnknownName.function() without knowing their name but Plugins. and .function() stay the same.
I have tried several things like plugin = 'Plugins.' + unknown_plugin_name + '.function()' or
plugin_name = "SomePlugin"
Plugins.plugin_name.function()
But I don't know any further.
Maybe someone knows how to solve that.
Also this is my first stack overflow post, so please correct me if I did something wrong.
You can use getattr to pass the string name. Using numpy as an example here's what the normal usage would look like
>>> import numpy
>>> numpy.random.randint(1, 5)
2
Now similar to your example
>>> getattr(numpy, 'random').randint(1, 5)
2
So applying to your case
getattr(Plugins, 'plugin_name').function()
This question already has answers here:
Python efficient way to check if very large string contains a substring
(8 answers)
Closed 1 year ago.
I'm a beginner to Python. I'm using the request module to get the text from a website that contains blacklisted users for the login system of my program. I want to know how to check if a variable appears in another variable such as, "if variable appears in variable2: do something"
Can anyone help? Thanks.
You can check that using the in keyword -
if object1 in object2:
#do something
Share your code. It would give a better understanding of what you need to do. I think the below code will work.
import requests
x = requests.get('https://yourwebsite.com')
if variable in x.text:
#do something
This question already has answers here:
What is the naming convention in Python for variable and function?
(15 answers)
Closed 2 years ago.
Any tips on how I should properly be naming python variables? Like any correct forms or for clean code.
Like for instance
myNum = 1
or
mynum = 1
or
my_num = 1
PEP 8 recommends my_num. #Merp's comment is correct though. Be clear and consistent with whatever convention you choose.
Well, traditionally developers follow the 'camelCase' variable naming proceedure in most languages, which is basically shown in your 1st code snippet. However, there are exceptions in the wide range of programming languages out there in terms of norms that manifest themselves over time. In Python, most developers tend to use the variable naming shown in your 3rd code snippet, separating each word with an underscore.
This question already has answers here:
Why can't "i" be manipulated inside for-loop [duplicate]
(3 answers)
Closed 3 years ago.
I just have a basic question about a for loop in Python 3. It's a stupid, simple question, but I figure everyone else here is far more knowledgeable to me, and most answers are pretty bright! I do not profess to be intelligent, so I'd like some clarification. Running this loop seems to add a value of only one. I have defined x to be 1, so this makes sense, but I didn't give i a true value and it seems to just naturally be 1.
for i in range(0, 50):
x = 1
i = x + i
print(i)
I'm not getting an error, I'm just curious as to what's going on behind the scenes. Would love some explanation from someone who understands this more than I do!
You do not need the x=i and i=x+i inside your loop.
for i in range(0, 50):
print(i)
would work just fine and would output 0,1,2,3...47,48,49. i is being set according to the range(0,50). If you want i to start at 1 simply change the range to range(1,50). Your code also added 1 to each i value so in order to account for this in the range you would need to do range(1,51) which would print 1,2,3...48,49,50.
Further Reading
I would recommend reading these to better understand how for loops work and also more info about ranges:
Range()
For Loops