Skip to content

Python Variables [100% Definitive Guide for Beginners]

Python Variables | 100% Definitive Guide for Beginner's

In this guide to Python Variables for beginners, you’ll learn:

1. What are Variables in Python?

2. How to declare Variable in Python?

3. Python Variable naming conventions

4. Assigning values to Variables in Python

5. Deleting a variable

So, Let’s Dive right in!

What are Variables in Python ?

In simple words:

A Variable is a name that refers to a value.

Python Variables

Technically Speaking:

Variables are the names of reserved memory locations where we store data/information.

But,

Hold on!

Which memory locations I’m talking about?

I’m talking about computer’s memory locations, which you also called as “RAM”.

And,

The data which we can store inside these memory locations might be a number, a string (sequence of characters), lists, Boolean values (True/False), sets and many more…

Let’s understand what actually Python variables are – By diving deep into behind the scenes!

Whenever our Program runs, It gets some memory in RAM.

Our Program get memory for two specific reasons, One for storing Data and the other for storing Instructions.

Our program’s Instructions will be stored in the memory available to hold the instructions.

Data can be stored anywhere at the remaining memory locations.

There are two important parts of any program. i.e., Data and Instructions.

Then,

What is an Instruction?

An Instruction is an order/command given to a computer processor.

Such as: We said add two numbers. So, add two numbers is an instruction.

But, Add what? So until there is no data, how will we add.

So, The instructions in the program use data for any manipulation (calculation).

Therefore, along with the instructions, data will also be needed in every program.

now, Where is the data stored?

Answer- Data must also be stored inside this memory (i.e., “RAM”).

There is a lot of space in this memory, but we need some space to hold the data.

And,

You have to store the data as long as your program is running.

The place where the data is stored in memory is given a name for identification. And, This name is called a Variable in Python.

Example:

understanding Python variables with images and examples.

As we can see in the above figure,

a, b and c are the three named locations in the memory. And, these a, b and c are called Variables.

So,

Basically, You can think of variables as a container (Box), Where you can put your values.

Now, The value you have stored (put in the box) may or may not change in the future.

It depends upon the particular situations (means according to the requirement of the user) and within the context of a mathematical problem or experiment.

Understanding the above concept with the help of an example:

Suppose, We are storing 5 here in a box and the name of that box is X.

It means, we are assigning 5 to x (i.e., x = 5)

understanding python variables with the help of images and examples

Of course, You can give any name to the variable, It does not matter, You can say abc, You can say a Box or anything else.

Programmer generally choose names for their Variables that are meaningful – they document what the variable is used for.

But, Why they are called as Variables?

Normally when you say Variable, This term simply means You can change the value,

Can we change here, can I say X = 8 , can I change the value from 5 to 8.

Yes, Of course!

You can change the value of X from 5 to 8.

And,

Now, The value of X becomes 8.

understanding python variables with the help of images and examples

And,

That’s why, They are called as Variables.

But, Here is a twist in Python.

Whenever you assign a value to a variable in any other programming languages such as C, C++ or Java,

Let’s say: we are assigning 10 to a here (i.e., a = 10).

You will see that,

The compiler or interpreter does the following:

  • 1. Creates a variable named a first
  • 2. Allocates the memory for this variable. (And for each memory location their is an unique address)
  • 3. Gives 10 to the variable a

Now,

Listen carefully!

When you assign 10 to a in Python,

The interpreter does the following:

  • 1. It first creates the value 10
  • 2. After that, It allocates the memory for this value (i.e., 10)

And, Mind it!

In Python: The interpreter allocates only one memory location for each value.

Because,

As we already know,

Python is an object-oriented language. And, It considers value as objects. Even everything in Python is an object.

So, How can a single object exists at two different places.

You can think of objects as a person, and how can a single person exists at two different places at a time.

It means:

If you create two variable named a and b,

And, If both the variable assigns the same value let’s say 4,

Then, These two variables (i.e., a and b) are pointing/referring to the same memory location where the value 4 is stored.

understanding python variables with the help of images and examples

But,

In any other Programming languages, For different variables there are different memory locations, even if both the variables contain the same value.

This happens Because,

In C,C++, Java or any other Programming languages the variable ( i.e., name of the memory location) creates first after that the value assigns to that variable.

So,

If a variable ( Let’s say- x) has an address (i.e., memory address) 237654 and it contains 6.

And, An another variable (Let’s say- y) has an address 236539 and it also contains 6.

But, When you observe carefully,

You will see that, Even if both the variable contains the same value but, The addresses are not same.

And, That’s why

x and y are pointing two different memory locations.

So, From the above discussion we can conclude that:

In Python :- Addresses of memory locations is for the value and not for the variable.

But,

In other programming languages :- Addresses of memory locations is for the variable.


How to declare Variable in Python ?

You might be wondering that,

What does declaration of Variable means?

It simply means that, We are informing or giving an instruction to the compiler or interpreter about some basic properties of a variable,

such as: name of the variable, type of value it holds (i.e., data-type of the variable) and its initial value if any it takes.

Do we need to declare variables in Python?

The short answer of this question is “NO”.

We do not need to declare variables in Python.

But, Why?

Because,

As we already know,

Python is a dynamically typed language :- It is a property of a language where type (i.e., data-type) of a variable is checked at run-time.

We don’t have to declare the type of a variable or manage the memory during assigning a value to a variable in Python.

To create a Variable in Python we just have to assign a value to it.

So,

You don’t have to give any additional commands unlike any other programming languages like C, C++ or Java. Where you have to give additional commands for declaration and assignment purposes.

The moment you assign a value to a variable in Python, It automatically detects what type of value you are assigning.

And, this is done by the Python interpreter.


Python Variable naming conventions

  • 1. The first character of a variable name must be an alphabet [ uppercase (A to Z) or lowercase (a to z) ] or an underscore (_), And then it can be followed by any of the character, alphabet, digit, or underscore.

Example:

Python 3.9.4 (tags/v3.9.4:1a79785, Apr 21 2021, 13:44:55) [MSC v.1928 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> Name = "Python"
>>> Name  
'Python' 

>>> name = 'C++' 
>>> name 
'C++' 

>>> _standard = 12 
>>> _standard 
12 

>>> roll_no_123 = 'Harry' 
>>> roll_no_123 
'Harry' 

>>> B16_Cat = 'Tom' 
>>> B16_Cat 
'Tom'
>>>|

  • 2. A variable name cannot start with a digit.

Example:

Python 3.9.4 (tags/v3.9.4:1a79785, Apr 21 2021, 13:44:55) [MSC v.1928 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> 1number = 739823                                        
     ^                              
  SyntaxError: invalid syntax   
>>> number12 = 739823  
>>> number12  
739823

  • 3. The only special character used in Python variable name is an underscore (_).
  • 4. No special characters like @, #, ! , $%, ^, & or punctuation characters ( ? ,  , : ; etc.) is allowed.

Example:

Python 3.9.4 (tags/v3.9.4:1a79785, Apr 21 2021, 13:44:55) [MSC v.1928 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> @gmail = "Imran"
     ^
  SyntaxError: invalid syntax
>>> wow! = "happy"
       ^  
SyntaxError: invalid syntax
>>> ca$h = 100
      ^
SyntaxError: invalid syntax

  • 5. Keywords (reserved words/predefined-words) cannot be used as a variable name.

Example:

Python 3.9.4 (tags/v3.9.4:1a79785, Apr 21 2021, 13:44:55) [MSC v.1928 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> for = 10
      ^
 SyntaxError: invalid syntax

>>> break = "break the execution"
        ^
  SyntaxError: invalid syntax 

Why above examples are showing invalid syntax ?

Because, for and break are keywords in Python.


  • 6. We cannot use a space for separating multiple words. Multiple words can be separated using an underscore.

Example:

Python 3.9.4 (tags/v3.9.4:1a79785, Apr 21 2021, 13:44:55) [MSC v.1928 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> what is your name = 'Imran'
        ^
   SyntaxError: invalid syntax

>>> what_is_your_name = 'John'
>>> what_is_your_name
'John'

  • 7. Python is a case sensitive language. This means that “student” and “STUDENT” are not same. Because one word (student) is in lowercase and the other in uppercase respectively.

Example:

Python 3.9.4 (tags/v3.9.4:1a79785, Apr 21 2021, 13:44:55) [MSC v.1928 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> Student = 'Future'
>>> STUDENT = 'Past'
>>> student = 45
>>> print(student)
45
>>> print(STUDENT)
past
>>> print(Student)
Future

  • 8. Variable names can be of any length.

Example:

Python 3.9.4 (tags/v3.9.4:1a79785, Apr 21 2021, 13:44:55) [MSC v.1928 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> abcdefghijklmnopqrstuvwxyz = 'alphabet'
>>> abcdefghijklmnopqrstuvwxyz
'alphabet'

Assigning values to Variables in Python

Assigning a value to a variable means storing a value to a variable.

Or,

You can say – Assigning a value means giving a value to a variable.

Example:

Let’s say: i = 2

The statement i = 2 gives the value 2 to the variable i. This is known as assigning a value to a variable i.

Here, i is assigned the value 2.

This means that the space in the computer’s memory associated with i now holds the value 2.

And,

As you can see from the above example, You can use the assignment operator (=)  to assign a value to a variable.

So,

Assigning variables in Python is so simple, you just have to write the variable name on the left hand side of  =  and the value on the right hand side.

Let’s have a look at some of the common examples:

Assigning different values to multiple variables:

message = 'Creating multiple variables'
x = 15
miles = 1.61

print(message)
print(x)
print(miles)

This example makes three assignments. The first assigns a string to a new variable named message; the second gives the integer 15 to x;

And,

The third assigns the (approximate) value of kilometer in one miles.

Output:

Creating multiple variables
15
1.61

Assigning a single value to multiple variables:

x = y = z = 5

print(x)
print(y)
print(z)

Output:

5
5
5

Re-assigning a value to a variable:

language = 'Java'
print(language)

language = 'C++'
print(language)

Output:

Java
C++

As you can see from the above example,

You can re-assign Python variables even after you have assigned once.

In the first assignment statement, we assigns a string called “Java” to a new variable named language.

Later, we re-assigning “C++” to the same variable named language.


Delete a variable:

You can delete Python variables using the keyword del.

n = 10
print(n)

del n
print(n)

Output:

10
NameError: name 'n' is not defined

Once you delete variable n and print n it will show the above comment (i.e., NameError: name ‘n‘ is not defined), which means your variable is now deleted.


Conclusion:

Variables:- Python Variables are the names of reserved memory locations where we store data/information.

Declaration of Variables in Python:- We do not need to declare variables in Python.

Because,

Python is a dynamically typed language :- It is a property of a language where type (i.e., data-type) of a variable is checked at run-time.

To create a Variable in Python we just have to assign a value to it.

Assigning values to Variables in Python:- Assigning a value to a variable means storing a value to a variable.

Let’s say: i = 2

The statement i = 2 gives the value 2 to the variable i. This is known as assigning a value to a variable i.


I hope this article helped you a lot.

If you have any question,

Feel free to ask in the comment section below right now!

Happy Learning! 🤗


Share this Post

Leave a Reply

Your email address will not be published.