Python Variables Tutorial

Python Variables

This is a detailed tutorial of Python Variables. Learn to create variables, their scope and how to assign them values of different data types.

Creating Variables in Python

Like all other programming languages, variables in Python are also used to store data values. The data can be of different types such as numerical or textual data. In Python declaration and initialization of variables can be done at the same time. A variable is declared as well as initialized whenever you write it and give it a value using the assignment operator. (=)

Check out the following example. In this, we’ve declared a variable x and have assigned a numerical value 3 to it and also another variable y to which we’ve given a textual value Gurmeet.

Declaring & Initializing Python Variables

Note. You can assign numbers directly after the assignment operator without the need for any quotes while for textual data, you must use quotes, either single or double, both works. Therefore, x = 'Gurmeet' and x = "Gurmeet" will mean the same. In python, you can print any kind of variables to the console screen using the print() function.

One good thing about variables and data types is that you need not define the data type for a variable explicitly like many other programming languages such as Java, C, and C++. Python will decide itself what should be the data type.

Once, the variables are declared and initialized, you can use them anywhere in the program. The default scope of the variables is global if they’re defined outside of any function which means they hold the same values outside or inside a function.

Naming Conventions For Python Variables

The variable names like x & y are good for examples and small code illustrations. But for a purposeful Python program, you must give good variable names like marks, points, gender, description, city, etc. Before you just start giving any names to your Python Variables, you must also know the different rules and conventions. If your variable name is disobeying any of these rules, it will not be accounted for the valid variables in your python program and error(s) will occur.

  1. The starting character of a variable can be either an alphabet letter or an underscore. The first letter can not be a number.
  2. Only alphanumerical characters (A-Z, a-z, 0-9) and underscores are allowed to be used as characters in your variable name. Except for this, you can not use any other symbol.
  3. The variables are Case-Sensitive, this means that City and city are two separate variables and can hold different values and even of different data types and scope.
Valid Variables.age, date_of_birth, height, gender, Color, COLOR, _color, color1, var1, etc.
Invalid Variables.1var, 2_age, date-of-birth, $gender, etc.

Multiple Variables Assignment

You can assign values to multiple variables at the same time in a single line of code in python. An example illustrates the same is written below.

Python Multiple Variables Assignment

You can also assign the same for multiple variables in a single line of code as demonstrated in the following code.

Multiple Variables Same Value Assignment Example

Merging Variable Values

You can use different operators to merge the value of two or more variables. But the variables of different data types will merge in different ways. You can use the + operator to merge variable values.

  • Merging two variables of textual data type i.e. string data type will concatenate the values.
  • Merging two variables of numerical data type such as integer will sum up the values.
  • If you’ll try to merge variables of different data types, an error will be raised.

An example to illustrate the concept of merging the variables is illustrated below.

The merged variable namePythonMarks will raise an error as here we’re trying to merge variables that belong to two different data types integer and string.

Merged Variables Example

Scope of Variables

When you’re writing a serious python program, the scope of the variables matters a lot. There are two different types of scopes that a variable can hold in Python. A variable can be either in the GLOBAL or LOCAL scope.

A variable with a GLOBAL scope has retained the same value throughout the python program. It has the same value inside and outside of any function(s). A variable with a LOCAL scope retains its value only side the function in which it is defined.

GLOBAL Scope

Whenever we define a variable outside of any function in a python program, it has a global scope and therefore it can be used inside or outside of the function. The following example illustrate such variable.

Python Variables GLOBAL Scope Example

The variable age holds the same value outside and inside of the function ageInside(). And even if you will change the value anywhere whether inside or outside the function, it will be changed everywhere as the variable holds the GLOBAL scope.

LOCAL Scope

If a variable is defined within a function, then it holds a LOCAL scope that means the variable value that is being defined within the function retains only within the function. And if the same variable is defined earlier outside of the function, then that outside value will be retained and can be used therefore anywhere outside of the function. An example illustrates this concept of LOCAL Scope of variables is given below.

Python Variables LOCAL Scope Example

First, the variable age is defined and assigned the value 21 outside of the function. Then the same variable is also defined inside the function with another value 20. As as the print() statement inside the function clears that the value of the variable age inside the function is 20 but when we again use the print() statement outside of the function to check the value of the variable age, it’s again 21, the original value of the variable defined earlier outside of the function at the start of the program.

This thing proves that the variable outside of the function holds GLOBAL scope and inside of the function holds LOCAL scope. But what if you wanted to use the same outside variable with GLOBAL scope inside of a function?

Well, for that we need to use a keyword in front of the variable definition. That keyword is global.

global Keyword

When a variable has to be defined inside a function and has to kept the GLOBAL scope, the global keyword is used. We’ll use the same example given above to explain the usage of the global keyword. Observe the changes carefully.

Python Global Keyword Example

Initially, outside of the function, the value of the variable age is 21. It holds the global scope. Now we’ve defined the age variable inside the function with the global keyword. It tells the compiler that the age variable is holding the GLOBAL scope now. Therefore, when inside the function we changed the value of this variable to 20, the value outside of the function also changed as proven by the print() statement printing the value of the variable age again outside be to 20, the changed value.

I hope you found this guide useful. If so, do share it with others who are willing to learn Python. If you have any questions related to this article, feel free to ask us in the comments section.

And do not forget to subscribe to WTMatter!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

Your email address will not be published. Required fields are marked *