Python Variables


Python Variables

A variable is a name given to the memory location that can hold a value. Yes, you read it right, It does not hold the value itself, it holds the reference to the memory address which holds the value.

Example:

x = 888
y = "PY"

Let us understand with an example by having two variables x and y, x holds value 888 and y holds value “PY”. Both the variables actually hold memory address where these variables are stored. For reference, x holds memory address 102 and y holds memory address 104, which further holds the respective values.

Let us understand this using the below diagram and table:

Python Variables
Variable Name : xVariable Name : y
Memory Address : 102
Value : 888
Memory Address : 104
Value : “PY”

Declaring Python Variables

Python does not require us to define the variable type or the variable itself.

  • In Python, variables are declared when we first assign a value to it.
  • We can directly start assigning value to the variable without defining it.

Let us understand with an example below:

We are declare a variable ‘a’ and assigning it a value 5.

In [1]:
a = 5

Now check the value ‘a’ holds.

In [2]:
print(a)

Declaring another variable ‘name’ and printing it.

In [3]:
name = "Tom"
print(name)

Reassigning Python Variables

We can re-assign a variable with a different type of value at any stage in the code. This makes Python a Dynamically Typed Programming Language.

Python is Dynamically Typed

  • Dynamically typed means that the datatype of a variable is interpreted at runtime, and you don’t need to explicitly declare the type of the variable when you define it. This contrasts with statically typed languages, where the variable type must be explicitly declared during compilation.
  • Dynamically typed also means that the type of the variable can be changed any time during the execution of the program.

Let us see with an example:

In [1]:
x = 5
print(x)

x = "Python"
print(x)
5
Python

Multiple Variable Assignment

Python allows multiple variables to be assigned values at once.

  • We can assign the same value to all the variables.
  • We can also assign different values to the variables.

Let us see an example below:

Assigning same value.

In [1]:
a = b = c = 5
print('a =',a)
print('b =',b)
print('c =',b)
a = 5
b = 5
c = 5

Assigning different value.

In [2]:
a,b,c = 5, 'Hello', 25.5
print('a =',a)
print('b =',b)
print('c =',b)
a = 5
b = Hello
c = Hello

Delete a Variable

In Python, we can delete a variable using the del keyword. Then the variable is no longer available in the memory.

In [1]:
a= 5
print(a)
5
In [2]:
del(a)

After deleting ‘a’, python gives NameError saying ‘a’ is not defined.

In [3]:
print(a)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-3-bca0e2660b9f> in <module>
----> 1 print(a)

NameError: name 'a' is not defined

Variable Naming Rules

Before we can use variables, the names of the variables have to meet certain rules. Let’s take a look at what these rules are:

  1. A variable name should not contain spaces.
  2. A variable name should not start with a number.
  3. A variable name can have any letter (uppercase or lowercase), number(0-9), or underscore(_). Note, as mentioned in point 2 that a variable name should not start with a number, but it can start with any letter or underscore(_).
  4. A variable name should not contain any of the special characters other than underscore(_).
  5. A variable names are case-sensitive. We can say that “name”, “Name” and “NAME” are different variables.
Variable Naming Rules

Resources

Handwritten Notes

References

  1. Python Variables
  2. Variable Names
  3. Everything about variables