Variables of Python Programming Language

Variables of Python Programming Language
Variables of Python Programming Language

Variable in Python: Variable is a name of storage space which is used to store data. Variable value is a changeable. Variable is most important part of programming languages. There is no need to declare a data type variable name in python.

Python variables can be store data in a program. A variable is a name that a program can access. Every variable in python has a specific type. Variable name are not same.

Python Variable Declaration Syntax

name="Jiocoding"
pin_code=244008
currency=596.90
print("Website Name:", name)
print("State Pin Code:", pin_code)
print("Currency Value:", currency)
============OUTPUT============
Website Name: Jiocoding
State Pin Code: 244008
Currency Value: 596.90

Here name pin_code and currency are the name of variables. Value of name is Jiocoding, pin_code is 244008 and Currency is 596.90.

Rules for Naming Variables in Python with Examples

  • The first letter of a variable should be alphabet or underscore(_).
  • The first letter of variable should not be digit.
  • After first character it may be combination of alphabets and digits.
  • Blank spaces are not allowed in variable name.
  • Variable name should not be a keyword.
  • Variable names are case sensitive for example name and NAME are different variables.

local variable in python definition

Local Variable: A variable declared inside the body of the function is called local variable. Local variable can be used only inside that function in which it is defined.

Example of Local Variable in Python

def local_variable():
       name="Jiocoding"
       print("Local Variable:", name)
local_variable()
============OUTPUT============
Local Variable: Jiocoding

Example of Local Variable Used Outside

def local_variable():
       name="Jiocoding"
       print("Local Variable:", name)
local_variable()
print("Local Variable:",name)

============OUTPUT============
print("Global Variable:",name) #error
NameError: name 'name' is not defined

Global Variable in Python Definition

Global Variable: A variable which is created outside a function is called global variable. It can be used anywhere in the program.

Example of Global Variable in Python

name="Python Language"
def local_variable():
       name="Jiocoding" 
       print("Local Variable:",name)
local_variable()

print("Global Variable:",name)
============OUTPUT============
Local Variable: Jiocoding
Global Variable: Python Language

Global Keyword Usage in Python

global keyword: Simply we can't modify the value of global variable inside a function but by using global keyword we can modify the value of global variable inside a function.

Example of Global Keyword in Python

def Local_Data():
       global name
       name="Jiocoding"
       print("Inside Local Function Data:",name)
Local_Data()
print("Outside Local Function Data:",name)

============OUTPUT============
Inside Local Function Data: Jiocoding
Outside Local Function Data: Jiocoding

Conclusion of Variables in Python

Conclusion: This is a variables of python. It can be used python program. It is very important python language. I can more briefly defines next chapter. If your any query variables related you can contact me.

Post a Comment

0 Comments