Python Variables

Table Of Contents

  1. What Is A Variable?
  2. Variable Naming Rules.
  3. Variable Declaration.
  4. Assigning A Single Value To Multiple Variables.
  5. Assigning Different Values To Multiple Variables.
  6. Getting The Data Type Of A Variable.
  7. Global And Local Variables In Python.
  8. Delete A Variable.

(1) What Is A Variable?

  • Variable is a place in the memory location, where we store our values and give it a name.
  • Computer is having two types of memory location (Temporary Memory- RAM and Permanent Memory – ROM), variables store in the Temporary memory (RAM).
  • In Python, We do not need to declare variables before using them or declare their type. A variable is created the moment we first assign a value to it.
  • A variable is a name given to a memory location. It is the basic unit of storage in a program.

(2) Variable Naming Rules.

  1. The variable is a combination of character digits and underscore and the character includes letters in lowercase (a-z), letters in uppercase (A-Z), digits (0-9), and an underscore (_).
  2. A variable cannot begin with a digit. If an identifier starts with a digit, it will give a Syntax error.
  3. In Python, keywords are the reserved names that are built-in to Python, so a keyword cannot be used as an identifier – they have a special meaning and we cannot use them as identifier names.
  4. Special symbols like! @, #, $, %, etc. are not allowed in variable names.
  5. Python variable names cannot only contain digits.
  6. There is no restriction on the length of the variable.
  7. Variable names are case-sensitive.

(3) Variable Declaration.

  • Python is not “statically typed”. We do not need to declare variables before using them or declare their type.
  • A variable is created the moment we first assign a value to it.
  • A Python variable is a name given to a memory location. It is the basic unit of storage in a program.

Example-1

# Variable-1
mark = 98.89

# Variable-2
division = "First"

# Variable-3
course = "Science"

# Variable-4
subjects = ['Math','Science','History','Geography','Biology']

print("Mark:", mark, " \nDivision : ",division , "\nCourse: ",course, "\nSubjects: ",  subjects)
Output: 
Mark: 98.89
Division: First
Course: Science
Subjects: [‘Math’, ‘Science’, ‘History’, ‘Geography’, ‘Biology’]

(4) Assigning A Single Value To Multiple Variables.

  • You can assign a single value to multiple variables at a time using the assignment (=) operator.

Example-1

a = 10

b = 10

c = 10

d = 10

print(a,b,c,d)
a = b = c = d = 10







print(a,b,c,d)
Output:
10, 10, 10, 10

(5) Assigning Different Values To Multiple Variables.

  • You can assign different values to multiple variables at a time using the coma ( , ) operator.

Example-1

a = 98.45

b = "First"

c = "Science"

d = ['Physics','Chemistry','Math']

print(a,b,c,d)
a , b , c , d , = 98.45,"First","Science",['Physics','Chemistry','Math']







print(a,b,c,d)
Output:
98.45, “First”, “Science”, [‘Physics’,’Chemistry’,’Math’]

(6) Getting The Data Type Of The Variables.

  • We can get the data type of a variable with the type() built-in function.
  • It will return you the Data Type of the variable.

Example-1

a = 98.45

b = "First"

c = "Science"

d = ['Physics','Chemistry','Math']

print(type(a))

print(type(b))

print(type(c))

print(type(d))
Output:
<class ‘float’>
<class ‘str’>
<class ‘str’>
<class ‘list’>

(7) Global & Local Variables In Python

(a) Local Variable

  • Variables that are defined inside a function body have a local scope. These variables are called local variables.
  • This means that local variables can be accessed only inside the function in which they are declared.

Example-1

def localVariable():
    x = 10
    y = "Hello World!"
    print(x,y)
localVariable()
Output:
10 Hello World!

Note:

  • Here ‘x’ and ‘y’ are declared inside of the function body, Hence these are called ‘Local Variables’.

Example-2

def localVariable():
    x = 10
    y = "Hello World!"

print(x)

print(y)
Output:
NameError: name ‘x’ is not defined

Note: 
You can not access local variables outside of its scope. It will give you a “Name Error”.

(b) Global Variable

  • Variables that are defined outside of the function body have a global scope.
  • This means the Global Variables can be accessed outside of the function .

Example-1

x = 10
y = "Hello World!"
def localVariable():
    print(x,y)
localVariable()
Output:
10 Hello World!

Note:
Here ‘x’ and ‘y’ are declared outside of the function body, Hence these are called ‘Global Variables’.

(8) Deleting A Variable.

  • We can also delete a variable using the (del) command.
  • It will permanently delete the variable from the memory.

Example-1

x = 10

del(x)

print(x)
Output:
NameError: name ‘x’ is not defined

Leave a Reply

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