Define multiple variables using for loop on python [duplicate] - python

This question already has answers here:
How do I create variable variables?
(17 answers)
Closed 4 years ago.
I am a beginner in python coding. I have a couple of questions:
Question 1: How Can I define multiple variables using for-loop in Python
global I1_1
global I1_2
global I1_3
global I1_4
global I1_5
global I1_6
Questions 2: How can I call multiple objects using the for-loop.
For example, I have these objects:
lineEdit_1
lineEdit_2
...
lineEdit_100
I would like to call everyone of them in a for loop. I tried to look for something similar online but I didnt find. My first thought was to define a range for a variable i (1-100) then call lineEdit_i. Any idea?

It sounds like you're talking about dynamically creating variables, which to my understanding you cannot do. What you can do, however, is leverage the python data structure of lists. Initialize a list by setting a variable like my_list = [] then you can .append() any of your variables to the list, use a for-loop to loop through all your variables, and do whatever you need to in your loop.

Related

I am new to python, and realized that i can assign print i.e. an inbuilt function as a variable [duplicate]

This question already has answers here:
How to restore a builtin that I overwrote by accident?
(3 answers)
Closed 2 years ago.
I am new to python, and realized that i can assign print i.e. an inbuilt function as a variable, then when i use print('hello world')this shows the exact error that i faced
I am familiar to c++ and even in that we were never allowed to use an inbuilt function as a variable name.
those were the fundamental rules for naming a variable
If python.org has issued the new version I'm sure they would have done it for a reason, bbut i want to know how do i access my print statement after assigning a value to it?
you won't be able to access your print function unless you do hacky things, which I recommend not to do them in the middle of your code.
Also it is good to know that python (as c++) has scopes for variables, and variables "die" and they are no longer accessible when scope ends. For instance:
def change_print_value():
print = 3
change_print_value()
print('Print works as expected')
It is a good practice to avoid using reserved keywords as variable names. Any IDE has the keywords highlighted, so you can easily realize when you are using a keyword where you shouldn't.
print is not part of the reserved keywords list in python. Here's a comprehensive list of reserved words.
Functions are first class objects in python, so that means they can be treated and manipulated as objects. Since print is a function (and an object), when you call print = 1, you reassign the variable print to have a value of 1, so the functionality "disappears".

How do I get the toplevel variable list of python's top-level program? [duplicate]

This question already has answers here:
Viewing all defined variables
(10 answers)
Closed 3 years ago.
For instance, when I run in a python's top-level program file, I can use __name__
to get the string "__main__", but how do I get the current list of toplevel global variables?
You might want to have a look at this thread: Viewing all defined variables
Quoting the best answer:
dir() will give you the list of in scope variables
globals() will give you a dictionary of global variables
locals() will give you a dictionary of local variables

Accessing data in variables in class Python 3 [duplicate]

This question already has answers here:
Python: access class property from string [duplicate]
(4 answers)
Closed 4 years ago.
I am using Python 3.5 and wish to do something like this
I have created a class which has variables Bitcoin, Monero , 'Etherum' ,etc with various integer values ,I wish to extract them
var1="Bitcoin"
value=classobj.var1 // there is a class which has a variable called Bitcoin and its value is 10 I wish to get its value using classobject.Bitcoin but the variable called var is Dynamic
print (value)
How do I achieve the same ?
EDIT
I know it is possible using switch statement but I am looking for other ways
This is almost always a bad idea—and you really should explain why your design looks like this, because it's probably a bad design.
But "almost always" isn't "always", so Python has a way to do this:
getattr(classobj, var)

UnboundLocalError: local variable referenced before assignment Python [duplicate]

This question already has answers here:
Using global variables in a function
(25 answers)
Closed 8 years ago.
I've been having trouble with the "def" function. I know this question has already been asked, but the answer didn't satisfy me and I didn't see how to apply it to my code. I'm trying to make the the popular game 2048 in Python. Basically, when I define the function that makes the entire board move left, it bites me with the error: UnboundLocalError: local variable referenced before assignment. It seems that I have to define the variables "bone" and "btwo" in somewhere that isn't, like, global. But I am yet to figure out how to get that working. Setting parameters in my moveleft() function isn't working, e.g moveleft(bone,btwo). So I'm at my wit's end.
Now, I'll include the entire code, all commented up, but the part I think has the problem is where I define the function moveleft(). Also, if there are any outstanding stupid bits of code, please tell me. Also, try and keep it simple, I'm pretty rubbish with programming and its associated phrases and terms. This is only my 3rd attempt at a code.
I realise I'm asking a lot, but I would really, really, appreciate help with this.
Code:
http://pastebin.ca/2824228
Minimized version:
bone, btwo = 1, 2
def move_left():
if bone == 1: print("bone is 1")
if btwo == 2: print("btwo is 2")
btwo = 3
bone = 2
move_left()
If you are writing to a global variable inside a function, then you need to explicitly say that you are referring to a global variable. So put this as the first line in your function:
global bone, btwo, bthree, bfour, bfive, bsix, bseven, beight, bnine
And, why don't you use a list instead of defining 9 variables?

Python variable scope in if-statements [duplicate]

This question already has answers here:
What's the scope of a variable initialized in an if statement?
(7 answers)
Closed 3 years ago.
In Python, are variable scopes inside if-statements visible outside of the if-statement? (coming from a Java background, so find this a bit odd)
In the following case, name is first defined inside the if-block but the variable is visible outside of the if-block as well. I was expecting an error to occur but 'joe' gets printed.
if 1==1:
name = 'joe'
print(name)
if statements don't define a scope in Python.
Neither do loops, with statements, try / except, etc.
Only modules, functions and classes define scopes.
See Python Scopes and Namespaces in the Python Tutorial.
Yes, in Python, variable scopes inside if-statements are visible outside of the if-statement.
Two related questions gave an interestion discussion:
Short Description of the Scoping Rules?
and
Python variable scope error
All python variables used in a function live in the function level scope. (ignoring global and closure variables)
It is useful in case like this:
if foo.contains('bar'):
value = 2 + foo.count('b')
else:
value = 0
That way I don't have to declare the variable before the if statement.

Categories

Resources