Answers for "dynamic variable in python"

8

creating dynamic variable in python

for i in range(0, 9):
    globals()[f"my_variable{i}"] = f"Hello from variable number {i}!"


print(my_variable3)
# Hello from variable number 3!
Posted by: Guest on October-03-2020
1

Dynamic Variable declaration in Python

#1st example
user_input = raw_input("Enter a variable name : ")
Enter a variable name: number
globals()[user_input] = 25
# you can use locals to assign local variable
# This is dynmic variable declaration

#2nd example 
Class Table:
  def __init__(self,x,y,datalist):# here x and y are no. of rows and column and datalist is 2d list
    for i in range (x):
      for j in range (y):
        var = f'table{i}_{j}'
        locals()[var] = datalist[i][j]
        # here we extracted data from a list of variable length and assigned them to different variable
        # this can be helpful in many situations
Posted by: Guest on May-25-2021
1

python dynamic variable name

name = "a"
value = True
a_dictionary = {name: value}
print(a_dictionary["a"])
Posted by: Guest on November-02-2020
-1

creating dynamic variable in python

for i in range(0, 9):
Posted by: Guest on February-25-2021

Code answers related to "dynamic variable in python"

Python Answers by Framework

Browse Popular Code Answers by Language