Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Python 

Function to create variables in Python?

Hello,
I would like to know if there is any way to create a variable without writing it manually in the code. In other words I would like to know if there is a function that can create variables automatically.

I need to create 1000 string type variables but there are many to do manually. What method is used so that they can be declared automatically in the code? ... or should I declare them all manually? 😶
(May 6, 2019 06:25 PM)Avispon99 Wrote:  Hello,
I would like to know if there is any way to create a variable without writing it manually in the code. In other words I would like to know if there is a function that can create variables automatically.

I need to create 1000 string type variables but there are many to do manually. What method is used so that they can be declared automatically in the code? ... or should I declare them all manually?  😶

It depends what you'd be naming them (ex: string1, string2, string3 could be done easily with a loop) and if you already have values for them. Remember you don't need to declare variables in python, just assign values.

If you plan on creating them all at once and don't care what they're named you could use a simple loop.

More details would be helpful. Do you already have the values in a text file?
[Image: FTnHw1z.gif]
Literally just use a dictionary. You're welcome.

[Image: 9Y60Xjq.png]
Can you give the full context of what you need to do? Like dendro said, do you need them named in a numbered fashion or do you need them to be randomly generated? Is this for a school assignment or a personal one?

You could easily generate a list of numbers and then iterate or loop through that list to get the variable names.

Because if it is your own project Best Practice defined in PEP 8 is typically going to be that you name your variables unique but simple and easy to read. That way when you come back to your project months later you know what the hell your code is doing.
Please use a dictionary. I’m begging you.
I only Deal with Contracts
Just like the replies above. Look into using a 'dictionary' for Python.
Install Gentoo
I don't get why everyone is suggesting to use dictionary. Dictionaries are useful to handle complex data structures. I think that a list would be better suited to the task.

Quote:>>> my_strings = [""] * 5
>>> my_strings
['', '', '', '', '']
>>> my_strings[2] = "hello world"
>>> my_strings
['', '', 'hello world', '', '']
>>>


In above example I created a list of 5 empty strings and put "hello world" in the 3rd one.
https://stackoverflow.com/questions/6181...-in-a-loop
This should answer your question.

TL;DR: Use a list or a dictionary.
[Image: CJLOhGd.gif]

(May 6, 2019 06:25 PM)Avispon99 Wrote:  Hello,
I would like to know if there is any way to create a variable without writing it manually in the code. In other words I would like to know if there is a function that can create variables automatically.

I need to create 1000 string type variables but there are many to do manually. What method is used so that they can be declared automatically in the code? ... or should I declare them all manually?  😶

Code
import random
import string

random_strings_list = []

def randomStringDigits(stringLength=6):
   lettersAndDigits = string.ascii_letters + string.digits
   return ''.join(random.choice(lettersAndDigits) for i in range(stringLength))

for i in range(1000):
   random_strings_list.append(randomStringDigits(8))

for random_string in random_strings_list:
    do_something_with(random_string)
[Image: iMC2aQP.png]


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Javascript]  by Z3R00NDATOct 16, 2023 01:24 PM 3 134 Oct 16, 2023 01:24 PM
Last Post: Disease
[Python] by Bass_ReevesAug 20, 2023 01:44 AM 9 632 Aug 20, 2023 01:44 AM
Last Post: Lucky4L
[C] by FhackMar 2, 2022 03:52 AM 4 307 Mar 2, 2022 03:52 AM
Last Post: Fhack