dynamic variable declaration in python

Solutions on MaxInterview for dynamic variable declaration in python by the best coders in the world

showing results for - "dynamic variable declaration in python"
Jade
23 Jun 2020
1#1st example
2user_input = raw_input("Enter a variable name : ")
3Enter a variable name: number
4globals()[user_input] = 25
5# you can use locals to assign local variable
6# This is dynmic variable declaration
7
8#2nd example 
9Class Table:
10  def __init__(self,x,y,datalist):# here x and y are no. of rows and column and datalist is 2d list
11    for i in range (x):
12      for j in range (y):
13        var = f'table{i}_{j}'
14        locals()[var] = datalist[i][j]
15        # here we extracted data from a list of variable length and assigned them to different variable
16        # this can be helpful in many situations