Python - Variables.

Python - Variables.

·

8 min read

Contains summary from the Book* : Python Data Structures and Algorithms

What is a variable?

Variables are reserved memory location to store data. They are labels that are attached to objects and act as a pointer or a reference to an object. For example:

a=[4,2,0]
b=a

In this example, variable a points to the memory location of list object [4,2,0]. When we create variable b and equate that to a, it is now pointing at the same memory location of [4,2,0]. And if we decide to add an element to that list

a.append(3)

This change will be reflected on both a and b.

Try it yourself


Variable Declaration

Python is a dynamically typed language. So unlike Java or C, when we initialize a variable we do not need to declare their type.

Although variables represent certain values like String and Int, the variable name that points to these values do not need to have a type. Variables are just a reference to a memory location.

You can easily redeclare a variable once you declare them. After you redeclare the variable, it just points to another memory location and the previous value is lost.

a = 4
type(a) 

a="I reassigned a"
type(a)

Rules for variable declaration:

  1. The first letter must start with either _ or a letter. The first letter cannot start with any numeric values.
  2. Variables can consist of only alphanumeric characters and underscores (A-Z, a-z,0-9 and _)
  3. Variables are case sensitive. PyThon is different from Python.
  4. There are certain reserved keywords like if break class del which cannot be used as variable names. You can find a list of keywords here

Variable Type Conversion or Casting

The process of converting an object of one data type (integer, string, float) to another in Python is called type conversion. There are two types of type conversion.

  1. Implicit Type Conversion.
  2. Explicit Type Conversion.
  1. Implicit Type Conversion

Variable Scope

There are two types of variables in Python. Local and Global

  1. Local Variables: Whenever Python executes a function, it creates a local environment that contains the names of the variables and parameters assigned within the function. The variables within this local environment is called local variable.

  2. Global Variables: A variable created outside of a function in the global namespace are called Global Variables. These variables can be used within any function.

To further clear our understanding of variable scope and redeclaration, let's look at this example:

a = 10
b = 20
c = 30
def myfunc ():
  global c 
  a = 12 
  c ="Hello World"
  print("Value of a inside the function: "+str(a))
  print("Memory location of a inside func: "+str(id(a)))

  print("Value of b inside the function: "+str(b))

  print("Memory location of c inside func: "+str(id(c)))
  print("Value of c inside the function: "+str(c))

myfunc()
print("Memory location of a outside func: "+str(id(a)))
print("Global variable a after running the func: "+ str(a))

print("Global variable b after running the func: "+ str(b))

print("Memory location of c outside func: "+str(id(c)))
print("Global variable c after running the func: "+ str(c))

This results in the following output.

Value of a inside the function: 12
Memory location of a inside func: 140196548835392

Value of b inside the function: 20

Memory location of c inside func: 140196486430000
Value of c inside the function: Hello World

Memory location of a outside func: 140196548835328
Global variable a after running the func: 10

Global variable b after running the func: 20

Memory location of c outside func: 140196486430000
Global variable c after running the func: Hello World

variables.png

Here, we defined three global variables a,b andc. All of them represented integer values in the beginning.

In the function myfunc() we represented the global variable by adding global keyword before c. And as we can see , when we changed the value of c from 30 to Hello World it's changed globally. However, the value of a persisted only within the scope of the function.

This is because Python checks for a variable's existence in two places: the function and the global namespace. When we assigned the value of a, to 12 the interpreter found that the value was defined within the function and printed it's value. Here, the variable a is local to the function, so it disappears when the function terminates.

Since b isn't defined within the function, it was looked for in the global namespace. Similarly, forc, it saw the keyword global and looked into the global namespace. Here, the variable c is treated as a global variable and thus it's value changes in the global scope.


Conclusion.

In this article, we covered the basics of Python Variables. You should now have a clear understanding of variables, how to declare variables and about variable scopes.

Note: Please let me know if you have any question, comment or concerns in the comment down below. Based off your suggestions, I will update this article with more details and illustrations in the future.


References:

  1. Python Variables. w3schools.com/python/python_variables.asp. Accessed 5 Mar. 2021.
  2. Python, Real. Variables in Python – Real Python. realpython.com/python-variables. Accessed 5 Mar. 2021.
  3. Python Type Conversion and Type Casting (With Examples). programiz.com/python-programming/type-conve... Accessed 8 Mar. 2021.
  4. Python Data Structures and Algorithms, Benjamin Baka