What are Variables in Python | Rules for Naming Variables | Local vs Global Variables

In this post we are going to discuss variables in python, how to create variables, types of variables and basic examples. We will also see the rules for naming the python variables.

What are Variables in Python

Variables are the place or a virtual location that is stored in memory with a certain name to store the data while working on any project, or program. In simple words, we can say variables are the box that is used to contain the data of the project that we need to store. Variables are just containers where we store the data and that container is being stored in the main memory of the machine from where we can easily get the container and use that value which is stored in a container called a variable. Variables help us in getting the project data stored in a Systematic way. Variables in python are also called identifiers.

Vrariable
Variable Storage

Storing of data in a Variable Here 21 is th3 data that need to store and age is the box where it is stored

Rules Of Naming Variables

There are a few rules which we need to keep in mind before naming a variable.It must be unique

it should be a reserved keyword. There the certain reserved keywords in every programming language which are being used to perform a particulate task at a time so they are called reserved keywords for example numpy, It is a reserved keyword as it is a name of a library used in python so we can not use this as a variable name as it will confuse the compiler whether you are using it as a library of the variable so that is kept reserved.

  • It can contain Numbers that is 0 to 9, alphabets lowercase, uppercase, and even underscore.
  • The variable name must start with a non-numeric character.
  • It must not have only numeric characters.
  • Variables are case sensitive where they work on ASCII values where a is different from A.
  • A variable name can start with the lower or upper case it is absolutely fine.
  • The length of the variable can be as large as 79 characters, it is recommended to keep it show and relevant to the data it is storing.
  • There are certain special characters that we can use in our variable name such as leading underscore(__), $,%,^,&,*.
  • If any of the given condition is not satisfied the variable name will show an error as the compiler will not accept them as variable

Types of variables

There are two types of variables

Local Variables

Local variables are the variables that are being used for short spam or for just one or functions or we can say only for one class. For example, we need the data of a certain variable a number of times and each time it needs some modification or is different from previous values at that time we use a local variable where the value of the variable in the last execution will not affect in next execution or we can say last the value that was stored in variable last time could not be used next as it does not have scope there.

Read more about rules from Python Variables. 

For example-
 the sides of squares will keep on changing when ever we take another square which need to be updated everytime we execute it in a project t find area, parameter or volumn etc of any given figures.

Global Variable

When certain variables are needed for more than one time or that always fix for every condition. We can say we do not want to make any changes to the value that the variable has for example the value of Pie is always constant we are never required to change it which makes it a universal or global variable which we never required to change or we can say we can use as it is a number of time whenever it is required in the project or program.

How to Create a Variable

We simply need a variable name as per given nomes and a data type for what type of data we need that variable for example

int age = 21;

here it is the type of data that is integer age is the variable name and 21 is the data that is stored.

in the same programming languages as python, we do not need to specify the type of data like here we have defined as int. In that case, we ca simply write it as

age = 21;

You can see our other details posts on variables –

Leave a Comment

%d bloggers like this: