This question already has answers here:
How can I select a variable by (string) name?
(5 answers)
Closed 3 years ago.
I've a list of variable names, how can I print value of it using for loop..
var1="First"
var2="Second"
list=["var1","var2"]
for var in list:
print(var) # I want to print value of var,
present output
var1
var2
What I want is
First
Second
list=["var1","var2"]
You've made a list of strings, not variable names. What you need is
list=[var1,var2]
Related
This question already has answers here:
How do I create variable variables?
(17 answers)
Closed 1 year ago.
x = 14
variable_name = 'x'
How can I get the value of x when I only have the name of it as a string?
You can use vars():
vars()[variable_name]
Which gives the variables of the scope it's called at (Source).
This question already has answers here:
How to get the original variable name of variable passed to a function [duplicate]
(13 answers)
Closed 2 years ago.
I'm writing a function to draw plots of dataframes and I would like to name the plots according to the name of the dataframe passed to the function. The first line of the plotter function is:
def plotter(data):
Say the data frame to be passed is df1, I'm wondering how could I get the string 'df1' when calling plotter(data=df1)?
You can do this:
from varname import nameof
def plotter(data, name_df):
# name_df is a string which is is "df1", then you can add to the plot
plotter(df1, nameof(df1))
This question already has answers here:
Short description of the scoping rules?
(9 answers)
Closed 2 years ago.
I m new to python and based on what I learnt so far this code should be correct,but it doesn't run.Can any of you help out
list_x=[4,5,6,7]
total1=0
def total(list_1):
for values in list_1:
total1 += values
print(total1)
total(list_x)
Move the total1 variable inside your function. Right now, it is a global variable, which means that you can't modify it from within the function without declaring your reference to it as global.
list_x=[4,5,6,7]
def total(list_1):
total1=0
for values in list_1:
total1 += values
print(total1)
total(list_x)
This question already has answers here:
How to print actual name of variable class type in function?
(4 answers)
Getting the name of a variable as a string
(32 answers)
Closed 3 years ago.
I want print paramater's name in a python function
First, a variable is assigned a value.Then, pass the variable to a function.
Last,I want to print str(variable) inner the function.
varibale = 1234
def f(x):
print(....)
return
f(varibale)
Expected output is print out the 'varibale' whatever variable is.
if a = 1 ==> f(x), expected output is 'a';if b = 2 ==> f(x), expected output is 'b'.....
Why do you want to print out the value using a function?
If you know the name of the variable, you can simply write your requested function output yourself by just putting 'variable'
So in your example: instead of using f(x), just use print('x')
This question already has answers here:
How do I create variable variables?
(17 answers)
Closed 6 years ago.
I have a list of variables in Python with below values.
I am trying to print the values of these variables in the following manner, but this is unsuccessful. Can someone please help with a solution ?
a1=1
a2=2
a3=3
for i in range(1,4):
temp="a"+str(i)
print(temp)
I want the output in 'temp' print the values(viz) 1,2,3 whereas the output seen are the variables (viz) a1,a2,a3
Try This:-
a1=1
a2=2
a3=3
for i in range(1,4):
temp="a"+str(i)
print locals()[temp]