Python Variable

Python Variable

A variable is an identifiers that is used to store a values.

In Python you don't need to declare variable type.

In Python you don't assign values to the variables, whereas Python gives the reference of the object (value) to the variable.

Assign a value to a variable.

        word="Hello World"
        print(word)
Note : Here word is variable and "Hello World" is a value.

Result : "Hello World"

Change value of a variable.

        #assign value to a variable
        word="Hello World"
        print(word)

        #reassign value to a variable
        word="Good Bye"

        print(word)

Result : "Hello World"

Result: "Good Bye"

Assign Multiple values to multiple variables.

        x,y,z=1,2,3
        print(x,y,z)

        i,j,k="Good",1,4.5
        print(i,j,k)
    

Result : 1 2 3

Result: Good 1 4.5

Constants Variable

A variable value can never change once initialized.

Constants are written in all capital letters and underscores separating the words.

        MAX_VALUE=100
        MIN_VALUE=10
        
        print(MAX_VALUE)
        print(MIN_VALUE)
    

Result : 100

Result: 10

Note : You don't use constants in Python or you can say no concept of constants. The globals or constants module is used in the programs.

Variable Scope

There are two type of variable scope in Python:

  1. Global Scope
  2. Local Scope

1. Global Scope

Global variable declare outside a function.

Global variable scope as long as python script is running.

2. Local Scope

Local variable can be access within a function.

No comments: