Python Variables
As the name suggests, variable is something that can change. In Python, variables are containers that store data values. More specifically, variables are reserved memory locations that give data to the system to process the code.
A variable can also be seen as a symbolic representation for the memory that was reserved by the data. Another great thing about Python is that the value as well as the data type of the variable can be changed during the code execution.
Example
As you can see in the output, the data type of the variable was changed in the same code execution. The Python function “type” is used to get the information about the data type of the variable.
Python Variable Declaration
When we declare a variable, we bind it to a data type. There are some rules you have to follow while declaring a variable otherwise the interpreter will give you an error. The rules are as follows: –
- The first character in the variable name can be a small, a capital letter or an underscore (“_”) but it cannot be a number.
- Characters following the first letter can be any alpha-numeric character like numbers, alphabets, etc.
- Python is a case sensitive language; therefore, x and X will be different for the interpreter and will show an error if the value stored in the capital letter is called by the small letter counterpart.
- You cannot use those which have a special meaning or which are reserved for a particular purpose in Python. For example – print has a special meaning, input has a special meaning too, and so on.
- You can not use keywords as the variable names. For example – and, or, if, else, etc. (We will cover these as we move further)
Keywords are those which have a special meaning and are already defined in Python. They are used to define the syntax of the coding.
You can not use inbuilt function names as the variable names such as print(), int(), input(), etc.
Example
Data Types in Python
There are 5 standard data types in Python: –
- Numbers
- Strings
- Lists
- Dictionary
- Tuple
We have just mentioned these 5 data types here but these are important.
So, we have a complete article on Python Data Types here.
Multiple Declaration
Python lets you declare multiple variables in a single statement. If you want to make your code shorter, this is a great way to do it.
Example
Taking Inputs
You can also assign the same value to multiple variables in the same line of code.
Example
Variable Manipulation
In Python, “print” is used quite often to output variables. Two similar data types can be combined using the “+” operator to give a single data unit.
Example
But what happens when you try to add a number and a string?
Example
Types of Python Variables
There are 2 types of variables in Python. These are –
- Global variable
- Local variable
1. Global Variable
A variable which is created outside of a function is called a Global variable. You can use this type of variable anywhere in the code.
Example of Global variable - 1
Example of Global variable - 2
2. Local Variable
A variable that is only present exclusively for a particular function in which it was created, then that variable is a Local variable.
Example
If you want to make a local variable global, you can put the “global” keyword before the variable name in prior to declaring the variable.
Example
In this case, Python won’t show any error and will just print the output twice. Also, you can change the value of a global variable inside a function by adding “global” keyword before the variable inside the function.
Deleting a Variable
Python also allows you to delete a variable by using the command “del”
Example
As you can see, once you delete a variable and when we tried to print it, interpreter gives an error.
Bonus Tips
- Always keep the variable name short and meaningful.
- Decide carefully whether you want to declare a variable as a Local variable or a Global variable.