Python Variables with examples

Python variables

Hello Techies,

Today we are covering the “Python Variables” with examples.

Python Variables

In Python, a variable is considered a tag to which some value is added. Python is considered a value as an object.

Rules to create variables in Python

  • The name of each variable should start with alphabets or underscore(_).
  • No space is allowed in declaring a variable.
  • No special symbols are allowed in the middle of the variable declaration except underscore (_).
  • A variable is written with a combination of letters, numbers, and special characters _(underscore).
  • Reserved keywords are not used when creating variables.

So let’s see some examples of how to create a variable in Python by applying the above rules.

>>> a = 10
>>> a
10
>>> _a = 20
>>> _a
20
>>> a15 = 45
>>> a15
45
>>> a_ = 80
>>> a_
80

The above is the correct way to create a variable in Python. If you use the following example to create Python variables, it will generate an error.

>>> 15a = 80
File "<stdin>", line 1
15a = 80
  ^
SyntaxError: invalid syntax
>>>
>>> $name = 75
File "<stdin>", line 1
$name = 75
^
SyntaxError: invalid syntax
>>>
>>> and = 15
File "<stdin>", line 1
and = 15
^
SyntaxError: invalid syntax
>>> python variable = 15
File "<stdin>", line 1
python variable = 15
       ^
SyntaxError: invalid syntax

Declaring Variable and Assigning Values

We do not need to explicitly declare a variable in Python. When we assign a value to a variable, that variable is declared automatically.

Example:

>>> test = 10
>>> test
10

Re-declare the variables

You can re-declare Python variables once you have declared them.

Example:

>>> test = 10
>>> test
10
>>> test = 20
>>> test
20

In the example above, we will initially declare the value of the test variable as “10”. Then we will reassign the variable test to “20”. In this case, you get 20 as the final output from the test variable.


Object Identity

In Python, each created object is uniquely identified. Python guarantees that no two objects can have the same identifier. The built-in id() function is used to identify the object identifier. Consider the following example.

>>> x = 10
>>> y = 10
>>> print(id(x))
140707260012480
>>> print(id(y))
140707260012480
>>>
>>> a = 2
>>> b = 5
>>> print(id(a))
140707260012224
>>> print(id(b))
140707260012320

In the example above, both x and y are pointing to the same object so we get the same address for both variables. When we assign different values to a and b, it assigns different addresses to both variables.


Fetching value of previous operations

We use _ (underscore) to get the value from previous operations. The underscore (_) represents the output of the previous operation. Let’s look at an example of this.

>>> x = 5
>>> y = 3
>>> x + 10
15
>>> _ + y
18

Assigning values to multiple variables 

In Python, we can assign values to multiple variables in a single statement, known as Multiple Assignments.

This Multiple assignments operation can be achieved in 2 different ways, Assigning a single value to multiple variables or multiple values to multiple variables.

Check out the following example:

1. Assigning single value to multiple variables

>>> a=b=c=10
>>> print(a)
10
>>> print(b)
10
>>> print(c)
10

2. Assigning multiple values to multiple variables

>>> a, b, c = 5, 10, 15
>>> print(a)
5
>>> print(b)
10
>>> print(c)
15

How to delete variable in Python

In Python, we use the keyword “del” to delete a variable.

>>> test = 10
>>> test
10
>>> del test
>>> test
Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
NameError: name 'test' is not defined 

Can we use the same variable name for different types?

Yes, we can use but when we use the same variable name, the variable will refer to the new value and type.

Consider the following example,

>>> test = 10
>>> test = "TechPlusLifeStyle"
>>> print(test)
TechPlusLifeStyle

Python Variables Types

There are two types of Python Variables:- Local variable & Global variable.

Local Variable

Local variables are variables that are declared within a function and have a scope in the function. Let us understand the following example.

Example:

def multiple():
    # Defining local variable
    a = 5
    b = 6
    c = a * b
    print("The multiple is:", c)

# Calling a function
multiple()

Output:

The multiple is: 30

Global Variables

We can use the Global variable throughout the program and its scope is in the whole program. We can use global variables inside or outside the function.

Example:

# Declare a variable and initialize it
a = 10

# Global variable in function
def myFunction():
    # printing a global variable  
    global a
    print(a)
    # modifying a global variable  
    a = 'Welcome To TechPlusLifeStyle'
    print(a)

myFunction()
print(a)

Output:

10
Welcome To TechPlusLifeStyle
Welcome To TechPlusLifeStyle
Interview Question:

Q:  Mention what are the rules for local and global variables in Python?

Ans: Refer above Variable Types as an answer

Conclusion

In this tutorial, you learned about Python variables, We have covered the following points:

  • Rules to create Python variables
  • Declaring Variable and Assigning Values
  • Re-declare the variable
  • Object Identity
  • Fetching value of previous operations
  • Assigning values to multiple variables 
  • How to delete variable in Python
  • Python Variables Types

Leave a Comment