Menu Close

Python variables, and their datatypes

What is a variable?

In computer science, variables represent memory locations, which store their own values. Variables are used to contain information and are essential to writing any program.

Datatypes

Not all variables are the same in Python. Every variable has its own datatype, as different datatypes have different properties and operations. For example, integers can be divided, but strings cannot.

Below is a table of the basic datatypes of Python.

Datatype Value
String Text
Integer All integer values
Float Any number with decimal values
Boolean True or False
NoneType None; null value

 

There are datatypes that can include many values. Below is a table of these mutli-value datatypes.

Datatype Value
List A mutable array of values from index 0 to n-1
Tuple An immutable array of values from index 0 to n-1
Range A sequence of numbers from 0 to n-1
Dictionary A table of values with custom indexes (keys)

 

Declaration and initialization of variables

Variables in Python are declared using the assignment operator (“=”). However, the syntax for each variable differs. For instance, multi-value datatypes are declared using different brackets that wrap around the array.

Consider the following segment of code:

The following code is the declaration of single-value datatypes.

#this is a string
myString = “New String”

#this is an integer
myInt = 123

#this is a float
myFloat = 1.0

#this is a Boolean
myBool = True

#this is NoneType
myNone = None

Below is the declaration of multi-value datatypes.

#this is a list
myList = [“item 1”, “item 2”, “item 3”]

#this is a tuple
myTuple = (“item 1”, “item 2”, “item 3”)

#this is a range
myRange = range(5)

#this is a dictionary
myDict = {“item1”: “Python”, “item2”: “Java”, “item3”: “C”}

Redeclaration of variables

Due to python’s dynamically typed nature, the datatype of the variables can be modified. This means that within a multi-value datatype, the items within the array can also be of different datatypes.

Example:

myVar = “This is a String”
print(myVar)

myVar = 123
print(myVar)

Output:

This is a String
123

As variables are not confined to their datatypes, python variables are easier to work with when compared to the variables of statically typed languages.

String concatenation

When we want to concatenate an integer to the end of a string, we would assume that it would be simple. However, due to their different datatypes, python will be unable to concatenate the two variables.

This is shown in the following code:

Example:

print(“IC” + 123)

Output:

TypeError: must be str, not int

Python will return a TypeError as it is impossible to concatenate an integer to the end of a string to form one large string.

 

However, it is possible to achieve the desired effect through type casting. Casting is the process of converting one datatype to another to modify two variables concurrently.

In order to cast an integer to a string, we use the “str(Var)” method to modify the variable’s datatype.

Example:

print(“IC” + str(123))

Output:

IC123

Deleting variables

Unused variables can waste system resources. Therefore, we can delete it using the “del” keyword.

By using del “Variable Name”, we can delete a previously declared variable.

Example:

myStr = “IC 123”
print(myStr)

del myStr
print(myStr)

Output:

IC 123
NameError: name ‘myStr’ is not defined

This error occurs because once we have deleted the variable, Python cannot find a variable named “myStr” to print.

Posted in Articles, Python

Related Articles

Leave a Reply

Your email address will not be published.

Leave the field below empty!