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.
Example of Local Variable#local Variable def add(): x = 20 y = 30 z = x + y print("The sum is:", z) add()
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
The Example of Global Variable#global Variable a = 25 def mainFunction(): global a print(a) a = 'Basic Engineer' print(a) mainFunction() print(a)
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
0 Comments