bing
Flat 10% & upto 50% off + 10% Cashback + Free additional Courses. Hurry up
×
UPTO
50%
OFF!
Intellipaat
Intellipaat
  • Live Instructor-led Classes
  • Expert Education
  • 24*7 Support
  • Flexible Schedule

What is a variable in Python

A variable is a memory address that can change and when memory address cannot change then that variable is known as constant. Variable is a name of the memory location where data is stored. Once a variable is stored then space is allocated in memory. It defines a variable using a combination of numbers, letters, and the underscore character.

What is a Python variable?

In this module, we will learn all about variables in Python. Following is the list of all the topics that we are going to cover, in case you want to jump to a specific one.

Watch this video on Variable in Python

Creating and Declaring variables

Python does not have a specific command just to declare or create a variable however, there are some rules that you need to keep in mind while creating variables.

  • Name of the variable cannot start with a number. It should start with either an alphabet or underscore character.
  • Variable names are always case sensitive and can contain alpha numeric characters and underscore character.
  • Reserved words cannot be used as variable names.
  • The variables are always assigned using equal sign followed by the value of the variable.

A variable in Python is created as soon as you assign a value to it. Python also does not require specifying the data type of the variable unlike other programming languages.

Assigning Values to Variables

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

Example:

a = 10
b = “Intellipaat”
print (a) # a is an int type variable because it has int value in it
print (b) # b is a string type variable as it has a string value in it.

Multiple Variable Assignment

You can assign a single value to the multiple variables as follows:

a = b = c = 5

Also, we can assign multiple values to the multiple variables as follows:

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

Note: Python is type inferred language i.e. it automatically detects the type of assigned variable.

For instance, example 1:

test=1
type(test)

Output:

int

Example 2:

test1=”String”
type(test1)

Output:

str

Learn Python in 16 hrs from experts

 

Re-declaring a variable

After you have declared a variable you can again declare it and assign a new value to it. The python interpreter discards the old value and only considers the new value.  The type of the new value can be different than the type of the old value.

Example:

a = 1
print (a)
a = ‘intellipaat’
print(a)

Output:

1
intellipaat

 

Local variable

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

a=100
print (f)
def some_function()
f = ‘Intellipaat’
print(f)
some_function()
print(f)

Output:

100
Intellipaat
100

Here in this Python example, when the variable f is declared the second time inside the function named some_function, it becomes a local variable. Now if you use that variable inside the function there will be no problem as we can see in the output of second print (f), it prints the value assigned to f in the function that is, Intellipaat.

While when we try to print the value of f outside the function, it prints the value assigned to it outside the function. As we can see in the output of first and third print (f), it prints 100.

Global variable

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

Example:

a = 100
print (a)
def som_function():
global a
print (a)
a = ‘Intellipaat’
some_function()
print (a)

Output:

100
100
Intellipaat

Here in this Python example, we have re-declared the variable a in the function as a global variable. Now if we change the value of this variable inside the function and then print the value of that variable outside the function then it will print the changed value as we can see in the output of the third print(a). Since variable a was declared globally, it can be used outside the function as well.

Wish to Learn Python? Click Here

Deleting a variable

Python provides a feature to delete a variable when it is not in use, so as to free up the space. Using the command del “variable name”, we can delete any specific variable.

Example:

a = 10
print (a)
del a
print (a)

If we run the above program, the python interpreter will throw an error as ” NameError: name a is not defined” in the second print (a), since we have deleted the variable a using the del a command.

Concatenating Variables

In Python, if you want to concatenate variables of different datatypes, let’s say a number variable and a string variable, then you will have to declare the number variable as string. If the number variable is not declared as string variable before concatenating the number variable with a string variable, then python will throw a TypeError.

Example:

a = ‘Intellipaat’
b = 100
print a+b

Here this block of code will throw a TypeError as variable a is string type and variable b is number type. To remove this error, we will have to declare the number variable as string variable as shown in the example below:

a = ‘Intellipaat’
b = 100
print(a + str(b))

Output:

Intellipaat100
Become Python Certified in 16 hrs.
CLICK HERE

Constants

Constant is a type of variable that holds values, whose value cannot be changed. In reality we rarely use constants in python. Constants are usually declared and assigned on a different module/file.

Example:

#Declare constants in a separate file called constant.py
PI = 3.14
GRAVITY = 9.8

Then import it to the main file.

#inside main.py we import the constants
import constant
print(constant.PI)
print(constant.GRAVITY)

This brings us to the end of this module. Next module highlights Python datatypes and number datatype in Python. See you there!

Further, take a look at our Python course and prepare yourself with our free Python interview questions created by the experts.

Previous Next

Download Interview Questions asked by top MNCs in 2018?

"0 Responses on Python Variables"

    Leave a Message

    100% Secure Payments. All major credit & debit cards accepted Or Pay by Paypal.
    top

    Sales Offer

    Sign Up or Login to view the Free Python Variables.