In this tutorial, we will learn about the Variable in Python, and Constant or its types, and how to use them with the help of examples.

Define Variable in Python

Variable is the memory unit refer by a name for storing a value. This name which refers memory unit is know variable name. The value of the variable can be changes time to time.

Types

There are two types of variable in Python.

  • Local Variable
  • Global Variable

Local Variable

The variables declared inside the function is called local variables. The scope of the local variable only within the function , if we are calling outside of the function we will get error message.

#local Variable 
def add():    
    x = 20  
    y = 30  
    z = x + y 
    print("The sum is:", z)  
add() 
Example of Local Variable
Example of Local Variable
Example of Local Variable

Global Variable

Global variables which are declare outside of the function is Know global variables. The Inside the function declaring global variable by using global keyword.

Example of Global Variable

#global Variable
a = 25  
def mainFunction():  
    global a  
    print(a)   
    a = 'Basic Engineer'  
    print(a)  
  
mainFunction()  
print(a)  
The Example of Global Variable
Example of Global Variable
Example of Global Variable

Variable Declaration Using Data types

There are two types Variable Declaration Using Data types

  • Implicit Declaration
    • The data type of variable can be determine according to store constant Value.
    • Python is supports implicit declaration.
  • Explicit Declaration
    • The data type should be mention during declaration of variable and hence variable can hold only that type of value.
    • Python is does not Support explicit declaration.

Constant in Python

Constant are the value those never change through out the program.

There are various types of Constants

  • Integer constant
  • Float constant
  • Boolear constant
  • String Constant

If you have any queries regarding this article or if I have missed something on this topic, please feel free to add in the comment down below for the audience. See you guys in another article.

To know more about Python Wikipedia please click here .

Stay Connected Stay Safe, Thank you


Basic Engineer

Hey Readers! We have more than fifteen years of experience in Software Development, IoT, Telecom, Banking, Finance and Embedded domain. Currently we are actively working on Data Science, ML and AI with multiple market leaders worldwide. Happy Reading. Cheers!

0 Comments

Leave a Reply

Avatar placeholder

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