What is variables in python & it’s scope, Constants.

Variable is used to stored data value in programming language. Data may be anything String, Integers, float etc. In python programming variable act like container to store value.

How to Declare & use Variable?

Declaring variable in python is very simple. See below example how you can declare and use variable.

x=”hello Programiz”

In the above example we declare variable x and we used variable by printing x.

When we declare variable than it reserved some memory location based on data types. It is similar like container for storing data values. In python there is no command for declaring variable when we assign value to variable it automatically declare.

How to Redeclare variable in python

In the above example first we assign x=6 than x become integer data types and then when we assign x=”sam” in next line now x become string data types means based on value it will automatically change it’s data types.



Cast variable in python

You can also cast data types of variables in python for that you need to used some in-built casting function in python. For casting variable in Strings, Integer, Float we can use str(), int(), float() method of python. Similarly there are so many methods for casting data types.



Get type of variable

Sometime you don’t know the data type of variable at that time you can use type() method of python. type() method simply used for getting type of variables.



How to assign multiple value to variable?

Assigning multiple value to variable is very easy in python programming.  See the below Example


You can also assign same value to all variable.


Constant variable in python

Constant is type of variable whose value is fixed, ones you assigned value you can’t change it later, it will show error.


Create constant.py file

Const PI=3.14

Create main.py file

import constant

Rules and naming convention of Variable

  • A variable name must start with a letter or the underscore character
  • A variable name cannot start with a number
  • A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _)
  • Variable names are case-sensitive (age, Age and AGE are three different variables)

Types of Variables

There are two types of variables in python

  • Global Variable

The variable which are declare outside of function and access anywhere in code means inside or outside of function is called as Global Variable.

Global variable has a global scope in program. Create a variable outside of a function, and use it inside the function


x = "Best"
def myfunc():
  print("Python is " + x)

This image has an empty alt attribute; its file name is Screenshot-178-1024x576.png
  • Local Variable

The variable which are declare inside of function and have local access in program is called as Local Variable.


def localvar():
   y = "local varible"

This image has an empty alt attribute; its file name is Screenshot-177-1024x576.png

you cannot use y variable outside of function.

Good to Know?

  • String variables can declare by single or double quotes
  • Variable name is case sensitive
  • You can re-declare a variable in python
  • Use del keyword for deleting variable in python
Was this article helpful?

3 thoughts on “What is variables in python & it’s scope, Constants.”

  1. 961298 81140I simply couldnt go away your website before suggesting that I actually enjoyed the standard information an individual provide on your visitors? Is gonna be back frequently in order to inspect new posts. 77887

  2. Pingback: URL

Leave a Comment