All IT Courses 50% Off
Python Tutorials

Python Variable Types and Assignment

Variables are a key concept in Python. They can be seen as containers that store data in Python. Variables can be defined in a more pythonic way, as a location in memory that feeds in data for the python interpreter to process. 

As an illustration, if a book is kept on a shelf, the book is the data while the shelf is the variable.

In this tutorial, you will learn:

  • What variables are in Python?
  • How to assign a variable.
  • How to concatenate a variable to a string.
  • The variable types in Python.
  • What global and local variables are in Python.
  • How to delete a variable in Python.

Let’s jump into it.

Variable Names in Python

The variable name is basically hinged on the data type it stores. There are different kinds of data types in Python including integers, floats, strings, dictionaries, lists, tuples, sets, etc. The variables that store these data types are most likely letters. It is good practice to ensure that your variable name is as descriptive as possible.

Rather than using x to store a list of car names, you can rather name the variable as car_names. Next, let us see how to assign variable in Python

Variable Assignment in Python

When you wish to assign a variable to a data type, you use the equal to sign (=). As a matter of fact, a single equal to sign is called the assignment operator in Python.

Let’s say we wish to name the name of students in a class.

student_names = ['Mosh', 'Vijay', 'Tobi', 'Steph']student_names = ['Mosh', 'Vijay', 'Tobi', 'Steph']

Observe the variable name that was declared, student_names. Variable names should always be lowercase. In situations where you would require more than one word, the words should be separated with an underscore.

How to Redeclare a Variable in Python

Python is an interpreter and not a compiler. This implies that the codes are usually read line by line. If you have assigned a variable to some data type, you can reassign the variable to another data or data type in a different line. When python sees this, it overwrites the previous assignment and uses the latest date/data type. In the bookshelf illustration, it is as though the books on the shelf were changed to something else. The same bookshelf but different data. Let’s see an example.

#assign variable names
student_names = ['Mosh', 'Vijay', 'Tobi', 'Steph']
#reassign variable names
student_names = ('Phil', 'Rahul', 'Janet', 'Mo')
 
print(student_names)

Output:

('Phil', 'Rahul', 'Janet', 'Mo')

As seen, the variable has been reassigned.

String Concatenation with a Variable in Python

You already know that you can print a declared variable to the terminal using the print function. In addition to that, you can concatenate a variable to a string and print the joint result on the terminal.

There is one caveat, however. The declared variable must be as a string. If you attempt to convert a string to an integer, it throws an error. See an example below

number = 2
print('The declared variable was ' + number)

Output:

Python Variable Types and Assignment

So how do we fix this? We must typecast the variable to a string to concatenate a variable to a string. See the example below.

#typecast the integer to a string

number = str(2)
print('The declared variable was ' + number)

Output:

The declared variable was 2

The Two Types of Variables in Python

Variables in Python have two broad classifications.

  • Global variable
  • Local variable

Global variables

Global variables are variables defined in a program but not within a function. They are variables that remain in force anywhere in the program, including inside a function. We’d see a coding example momentarily. For now, let’s understand what a local variable is.

Local Variables

Local variables are variables that are declared inside a function. It is important to point out that a local variable is only in force when the function is called. If you attempt to print a variable inside a function, without calling the function, it would return a ‘NameError: the variable is not defined’.

Lets see an example

#declare a global variable
print(variable_1)
def a_function():
    print('Printing the global variable inside the function: ', end='')
    print(variable_1)
    #declare a local variable
    variable_2 = 'infosys'
    print('Printing the local variable inside the function: ', end='')
    print(variable_2)
print('Printing the global variable: ', end='')
print('Printing the local variable outside the function: ', end='')
print(variable_2)

Let’s unpack what’s going on.

  • We defined a global variable variable_1
  • We printed it on the terminal
  • We defined a function a_function()
  • We printed the printed variable_1
  • We defined another variable, variable_2 in the function. Since variable_2 is defined in a function, it is a local variable.
  • We printed variable inside the function
  • Finally, we printed variable_2 outside the function. Since variable_2 is a local function, we do not expect it to hold effect outside the function. Hence, this step should return a ‘variable_2 not found error’.

Let’s run the program.

Output:

Python Variable Types and Assignment

As expected, it throws an error.

The second scenario would be to call the function and see if variable_1 (which was not defined inside the function) will be printed.

#declare a global variable
variable_1 = 'H2k'
print('Printing the global variable: ', end='')
print(variable_1)
def a_function():
    print('Printing the global variable inside the function: ', end='')
    print(variable_1)
    #declare a local variable
    variable_2 = 'infosys'
    print('Printing the local variable inside the function: ', end='')
    print(variable_2)
#call the function
a_function()

Output:

Python Variable Types and Assignment

As seen, the variable_1 (the global variable) was printed in the function. Even though it was defined outside the function, it can still be called in the function. This is why it is called a global function

Deleting a Variable

In python, you can delete a variable using the del statement. Whether local or global, the del statement deletes the variable from the memory of the interpreter. Check this out.

#declare a variable
variable_1 = 'H2k'
#delete the variable
del variable_1
print(variable_1)

Output:

Python Variable Types and Assignment

As seen, the variable has been deleted from memory.

In summary

In this tutorial, you have learned what a variable is and the two types of variables in Python. You learned that a global variable is declared anywhere in the program but not inside a function. It thus holds true anywhere it is called, even inside a function.

In contrast, a local variable is called inside a function and can only hold true when the function is called. Lastly, you have discovered how to delete a variable using the del statement in Python.

If you have any questions, please leave them in the comment section and I’d do my best to answer them.

Facebook Comments

Related Articles

Back to top button