![]() |
|
[Python] Function to create variables in Python? - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [Python] Function to create variables in Python? (/showthread.php?tid=5967449) |
Function to create variables in Python? - Avispon99 - May 6, 2019 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? 😶 RE: Function to create variables in Python? - dendro - May 6, 2019 (May 6, 2019 06:25 PM)Avispon99 Wrote: Hello, 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? RE: Function to create variables in Python? - Woes - May 6, 2019 Literally just use a dictionary. You're welcome. RE: Function to create variables in Python? - Lindros - May 8, 2019 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. RE: Function to create variables in Python? - POLY MATH - May 19, 2019 Please use a dictionary. I’m begging you. RE: Function to create variables in Python? - GnosticPlayers - May 19, 2019 Just like the replies above. Look into using a 'dictionary' for Python. RE: Function to create variables in Python? - michalmonday - May 19, 2019 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 In above example I created a list of 5 empty strings and put "hello world" in the 3rd one. RE: Function to create variables in Python? - V For Vendetta. - May 20, 2019 https://stackoverflow.com/questions/6181935/how-do-you-create-different-variable-names-while-in-a-loop This should answer your question. TL;DR: Use a list or a dictionary. RE: Function to create variables in Python? - RUMRUN - May 23, 2019 (May 6, 2019 06:25 PM)Avispon99 Wrote: Hello, Code import random |