VARIABLES IN PYTHON

Variables are buckets or holders which holds or contains the objects which are assigned to them during declaration

Every programming language has a variable concept which is essential for storing different types of objects in the heap memory

In below example,

Variable x is declared first and y is assigned to x. In this case, python allocate a pointer 2367230667120 for x and also reuse the same memory area for y in the heap. Again x value become 6 in which a new area 2367230667216 is pointed in the memory.

x = 3
print(id(x))
print('*'*40)
y = x
x = 6

print(x,y,\
      id(x),\
      id(y))

O/P:
2367230667120
****************************************
6 3 2367230667216 2367230667120
Variable Declaration In Python
var = 'string'
var1 = 1
var2 = 1.0
var3 = True
var4 = None
var5 = [1,1.0,'list',True,None,[1,1.0,'list'],(),{}]
var6 = ('tuple',)
var7 = {'set'}
var8 = {'k':'v'}
tmpvar = var

print(var + '\n' \
      + str(var1) \
      + '\n' + str(var2) \
      + '\n' + str(var3) \
      + '\n' + str(var4)\
      + '\n' + str(var5)\
      + '\n' + str(var6)\
      + '\n' + str(var7)\
      + '\n' + str(var8))

print(type(var),\
      type(tmpvar),\
      type(var1), \
      type(var2), \
      type(var3), \
      type(var4), \
      type(var5), \
      type(var6), \
      type(var7), \
      type(var8))

print(id(var),\
      id(tmpvar),\
      id(var1), \
      id(var2), \
      id(var3), \
      id(var4), \
      id(var5), \
      id(var6), \
      id(var7), \
      id(var8))

O/P:

string
1
1.0
True
None
[1, 1.0, 'list', True, None, [1, 1.0, 'list'], (), {}]
('tuple',)
{'set'}
{'k': 'v'}

<class 'str'> <class 'str'> <class 'int'> <class 'float'> <class 'bool'> <class 'NoneType'> <class 'list'> <class 'tuple'> <class 'set'> <class 'dict'>

2036667053296 2036667053296 2036660922672 2036742039664 140732748421224 140732748471512 2036742276672 2036742155424 2036742978016 2036742258176

Variable declaration guidelines

  • Underscore symbol(_) or a letter can be used in the beginning of a variable name.
  • A variable name cannot begin with a number.
  • Variable names can only contain alphanumeric characters and underscores (A-z, 0-9, and ).
  • Variable names are case-sensitive (var, Var and VAR are three different variables).
  • The reserved terms cannot be used to name the variable (keywords).

_var = 1
_var_ = 2
var1 = 3
1var = 4 # invalid declaration
@var = 5 # invalid declaration

There are two conventions that are named as snake case and camel case variables similar to english letters. These type of names are used to represent the variable.

CamelCase:

This term comes from a camel’s hunch or back which looks like a slope. Similar to this, the variable name is sloped with an upper case letter.

Example: AbcDef, WxYz

Snake case:

These variables resemble a snake with sine curves with an underscore in between

Example: abc_efg, xyz_abc

CamelCase = "This is a camel case variable"

snake_case = "This is snake case variable"

Leave a Reply