Python Variables

Python is not "statically typed". We do not need to declare variables before using them or declare their type. A variable is created the moment we first assign a value to it. A variable is a name given to a memory location. It is the basic unit of storage in a program.

The value stored in a variable can be changed during program execution.

A variable is only a name given to a memory location, all the operations done on the variable effects that memory location.

Rules for creating variables in Python:

Let's see run python code:

Let’s see the simple variable creation:



Re-declare the Variable:


Before declare:  100
After re-declare: 120.3

Assigning a single value to multiple variables:

Also, Python allows assigning a single value to several variables simultaneously with “=” operators.



Assigning different values to multiple variables:

Python allows adding different values in a single line with “,”operators.



Can we use the same name for different types?

If we use the same name, the variable starts referring to a new value and type.


How does + operator work with variables?



Can we use + for different types also?

No using for different types would produce error.

TypeError: unsupported operand type(s) for +: 'int' and 'str'

Global and Local Variables in Python:

Local variables are the ones that are defined and declared inside a function. We can not call this variable outside the function.

Welcome python ds

Global variables are the ones that are defined and declared outside a function, and we need to use them inside a function.

I love PythonDS

Global keyword in Python:

Global keyword is a keyword that allows a user to modify a variable outside of the current scope. It is used to create global variables from a non-global scope i.e inside a function. Global keyword is used inside a function only when we want to do assignments or when we want to change a variable. Global is not needed for printing and accessing.

Rules of global keyword:

Value of x inside a function : 20
Value of x outside a function : 20

Variable type in Python:

Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, data types are actually classes and variables are instance (object) of these classes.

Following are the standard or built-in data type of Python:


    Numbric data :  123
String with the use of Single Quotes:
Welcome to the Python-DS World

Set with the use of String:
{'r', 'G', 'e', 'k', 'o', 's', 'F'}

Dictionary with the use of Integer Keys:
{1: 'vishavjeet', 2: 'Gulfam', 3: 'Anupam', 4: 'Nimesh', 5: 'Nishu', 6: 'Sara'}

Object References:

Let, we assign a variable x to value 5, and another variable y to the variable x.

When Python looks at the first statement, what it does is that, first, it creates an object to represent the value 5. Then, it creates the variable x if it doesn’t exist and made it a reference to this new object 5. The second line causes Python to create the variable y, and it is not assigned with x, rather it is made to reference that object that x does. The net effect is that the variables x and y wind up referencing the same object. This situation, with multiple names referencing the same object, is called a Shared Reference in Python. Now, if we write:

This statement makes a new object to represent ‘Geeks’ and makes x to reference this new object.

Creating objects (or variables of a class type):