We have gone through the basics of Python variables , Let’s further dive into variables in today’s chapter. We will discuss the different variable types, data types, finding out the data type of a variable, deleting variables, local and global variables in python.
Table of Contents
Python Variables
Python variables can be broadly classified into Local variables and global variables. Local variables are the variables which are defined with a function and are accessible only with the scope of the function. Global variables are accessible throughout the entire Python code, variables defined outside functions in Python are automatically Global variables by default.
Example of Python Local Variable:
# Declaring the function
def sumoftwovariables():
# Defining local variables which are accessible only within a function
x = 24
b = 20
c = a + b
print("The sum of the two variables is:", c)
# Calling function
sumoftwovariables()
The sum of the two variables is: 44
In the above example, we declared a function named sumoftwovariables() and assigned a few variables within this function. These variables are the local variables which have scope only inside the function. If we try to use them outside the function, we obtain an error that the variable was not found.
Example of Python Global variable:
#Example code for Global variable
x = "Ted"
#Function declaration
def myfunc():
y="Ned"
print(y)
#Calling the function.
myfunc()
print(x)
Ned
Ted
In the above example, the variable x is a global variable while the variable y is local. Hence ‘y’ can only be utilized within the function while ‘x’ can be utilized through the entire program.
Data types available in Python
There are many data types available in Python, which be categorized as numbers, sequences, dictionaries or sets:
- boolean – Boolean variable allow a value of either
True
orFalse
. - int – Represents an integer, such as 1, 4, 8, 50.
- float – Represents a floating point number, for example 1.2 or 34.76
- complex – An complex number is represented by “ x + yi “. Python converts the real numbers x and y into complex using the function complex(x,y).
- str – Strings, for example “hello”. In Python 3.x, strings represent a set of Unicode characters
- bytes – A sequence of numbers in the range 0-255
- byte array – An array of bytes, similar to bytes, with the difference that it can change
- list – Lists are used to store multiple items in a single variable
- tuple – Tuple is an ordered collection of multiple items within a single variable which is unchangeable
- set – An unordered collection of unique objects
- frozen set – Same as set, only immutable
- dict – A dictionary where each element has a key and a value.
Finding data type of variable in Python
To check the data type of an existing variable, Python provides us with a built-in function type() which returns the data type of a variable as output. Let us go through a example of code to check the data type of a variable.
# Code to check the data types of variables
a = ('Ted', 'Ned', 'Fred')
b = "Hello World"
c = 44
x = type(a)
y = type(b)
z = type(c)
print(x)
print(y)
print(z)
<class 'tuple'>
<class 'str'>
<class 'int'>
In the above example, the data type of the value assigned to variable ‘a’ is a tuple. The data type of the variable ‘b’ value is a string shortened as ‘str’ and the data type of the value assigned to ‘c’ is an integer value. We see in the output that the function type() returns the data types of these values accordingly.
Deleting variables in python
Python also provides an inbuilt function del for deleting the assigned values from the variables, once the function is executed the associated value is cleared.
x = 110
print(x)
del x
print(x)
110
Traceback (most recent call last):
File "main.py", line 4, in <module>
print(x)
NameError: name 'x' is not defined
Thus we see in the above example output, at first the program executes the print statement for variable x without any trouble. But after the del function has been used on the variable x, the program cannot find any defined value for ‘x’ and gives an nameError.
In the next articles we will look at example programs for utilizing the various different data types available in Python.