This question already has answers here:
Python function global variables? [duplicate]
(6 answers)
Closed 7 years ago.
I am trying to set a dictionary for the staff's salary in Python. I want to create two functions, getname() and getsalary() so I tried:
empDic={}
def setname(n):
empDic={'n':''}
And then in python interactive 2.7 I typed:
>>>setname('marson')
>>>print empDic
{}
The result is still an empty dictionary, How can I deal with that?
You would need to modify the original dict:
empDic={}
def setname(d, n):
d[n] = ""
setname(empDic,"marson")
print(empDic)
{'marson': ''}
You are creating a new local variable empDic in your function, you are also using a string "n" not the name n passed in to the function.
Related
This question already has answers here:
Which is the preferred way to concatenate a string in Python? [duplicate]
(12 answers)
Closed 5 months ago.
I am wondering if we can use a variable name in order to create a new variable, for example:
Let's assume I have this variable:
Var = 'Jim'
Lets say I want to concatenate the variable with a string, in this case the string is the word Mr:
NewVar = "String"Var
So that if I print the new variable, the output would look something like:
MrJim
This can be achieved in bash like this:
NewVar=Mr${Var}
But I have not found a way to do this in Python. Please let me know if you know how to do it.
Have a look at python string interpolation.
var = "Jim"
new_var = f"Mr {var}"
This question already has answers here:
How do I create variable variables?
(17 answers)
Closed 1 year ago.
x=['a','b','c']
y=['d','e','f']
z=['g','h','i']
string='x'
#Now I would like to somehow get the list printed or returned by only using the string variable.
Use globals or locals:
x=['a','b','c']
y=['d','e','f']
z=['g','h','i']
string='x'
print(globals()[string])
['a','b','c']
If you have multiple list and you want to print them based on some key it's best to use a dictionary.
lstDic = {
"x":['a','b','c'],
"y":['d','e','f'],
"y":['g','h','i']
}
string='x'
print(lstDic[string])
This question already has answers here:
How do you create different variable names while in a loop? [duplicate]
(9 answers)
How do I create variable variables?
(17 answers)
Closed 4 years ago.
As of right now i am a beginner with python. I wanna make a giant list of variables without having to write out each one, so far heres the idea:
n=0
i=None
for i in range(10):
n = n + 1
x="hi"
var = x+"{0}".format(n)
print(var)
the output are strings but i want to make them variable that i can define freely. Any pointers>
This question already has answers here:
How can I create multiple variables from a list of strings? [duplicate]
(2 answers)
Closed 5 years ago.
I couldn't find an answer for this, so can you give a beginner pythonist some help. I have a string and if it is in globals, then I want to use that variable to manipulate it. I get an error that I'm using string to append, when my global variable is a list.
color = []
keyword = "color"
if keyword in globals():
keyword.append("testing")
globals() returns a dictionary, which you can use like any other dictionary
globals()[keyword] = <my_new_value>
This question already has answers here:
How to get the original variable name of variable passed to a function [duplicate]
(13 answers)
Closed 5 years ago.
want to get name of variable issued from outside.
For example:
def process(var):
print name(var)#prints 'nice_name'
nice_name=1
process(nice_name)
Is it even possible in python?
No, as we are just passing a value to the function