Variables in Python

Variables in Python – Constant, Global & Static Variables

Variables in Python is a name that is used to refer to memory location. Python variable is also known as an identifier and used to hold value.

In Python, we don’t need to specify the type of variable because Python is a infer language and smart enough to get variable type.

Variable names can be a group of both the letters and digits, but they have to begin with a letter or an underscore.

It is recommended to use lowercase letters for the variable name. Rahul and rahul both are two different variables.

A variable is a container for a value. It can be assigned a name, you can use it to refer to it later in the program.

Based on the value assigned, the interpreter decides its data type. You can always store a different type in a variable.

Assigning Values to Python Variables:

The Variable in Python is created as soon as we assign a value to it. Python also does not require specifying the data type of the variable, unlike other commonly used programming languages.

There is no need for an explicit declaration to reserve memory. The assignment is done using the equal to (=) operator.

a = 10

b = “Testing”

print (a) # a is an int type variable because it has an int value in it

print (b) # b is a string type variable as it has a string value in it

type (a)  #To check the data type of variable a

type(b) #To check the data type of variable b

Output:

10

Testing

<class ‘int’>

<class ‘str’>

Multiple Variable Assignment:

We can assign a single value to multiple variables as follows:

a = b = c = 5

a, b, c = 2, 25, ‘abc’

Python Variable Types:

There are two types of variables in Python – Local variable and Global variable. Let’s understand the following variables.

  1. Local Variables in Python:

A variable that is declared inside a python function or a module can only be used in that specific function or Python Module. This kind of variable is known as a local variable. Python interpreter will not recognize that variable outside that specific function or module and will throw an error if that variable is not declared outside of that function.

# Declaring a function

def add():

# Defining local variables. They has scope only within a function

a = 20

b = 30

c = a + b

print(“The sum is:”, c)

# Calling a function

add()

Output:  The sum is 50

In the above code, we declared a function named add() and assigned a few variables within the function. These variables will be referred to as the local variables which have scope only inside the function. If we try to use them outside the function, we get a following error.

print(a)

NameError: name ‘a’ is not defined

When we tried to use local variable outside their scope; it threw the NameError.

Global Variable in Python:

On the other hand, the global variable in Python is a variable that can be used globally anywhere in the program. It can be used in any function or module, and even outside the functions, without having to re-declare it.

Global variables can be used throughout the program, and its scope is in the entire program. We can use global variables inside or outside the function.

A variable declared outside the function is the global variable by default. Python provides the global keyword to use global variable inside the function. If we don’t use the global keyword, the function treats it as a local variable.

# Declare a variable and initialize it

f = 101

print(f)

# Global vs. local variables in functions

def someFunction():

# global f

f = ‘I am learning Python’

print(f)

someFunction()

print(f)

 

Output:

101
I am learning Python
101

Related Posts:

Python Tutorial – Learn Python

What is Python? What makes Python so Powerful?

Namespacing and Scopes in Python

Operators in Python

Data Types in Python

STRING Data Type in Python

LIST Data Structure in PYTHON

TUPLE Data Structure in PYTHON

Differences between List and Tuple

SET Data Structure in PYTHON

DICTIONARY Data Structure in PYTHON

Database Programming in Python

What is Multithreading in Python?

Python Exception Handling Using try, except and finally statement

File Handling in Python

Python Random module

Python reduce() Function

Python map() Function

Python filter() Function

Lambda Function in PYTHON (Anonymous Function)

Python Interview Question And Answers