As the 2nd and final part of this series on Python data Types, I’ll discuss the basics of complex numbers, types of variables and conversions. If you missed part-1 you may read it from here Python Basic Data Types – What They Are and Characteristics (Part – 1)
The last type of basic numerical data that has Python numbers is complex.
Complex numbers have a real part and an imaginary part and each of them is represented as a float.
To create a complex number, the following structure is followed <real_part> + <imaginary_part> j. And the real and imaginary part can be accessed through the real e attributes imag.
Arithmetic of numeric types
With all the numeric types, the operations of arithmetic can be applied: addition, subtraction, product, division, …
In Python, it is allowed to perform an arithmetic operation with a number of different types. In this case, the “smallest” numeric type is converted to that of the “largest” type, so the result’s type is always that of the largest type. We understand that the type int is less than the type float, which is less than the type complex.
Therefore, it is possible, for example, to add an int and a float.
In Python, the class that represents Boolean values is bool. This class can only be instantiated with two values / objects: True to represent true and False to represent false.
A peculiarity of the language is that any object can be used in a context where it is required to check if something is true or false. Therefore, any object can be used in the condition of an if or a while (they are control structures that we will see in future articles) or as the operand of a Boolean operation.
By default, any object is considered true with two exceptions:
- That implements the method __bool__() and it returns False.
- That implements the method __len__() and it returns 0.
Character string type
Once we have finished with the numbers, it is the turn of the letters.
Another basic type of Python, and essential, are the sequences or character strings. This type is known as a string although its true class is str.
Formally, a string is an immutable sequence of characters in Unicode format.
To create a string, you simply have to enclose a sequence of characters in single ‘ ‘ or double quotes ” “.
Single or double quotes can be used interchangeably with a particularity. Suppose you need to use a single quote in the string of characters. In that case, you have two options: use double quotes to enclose the string, or use single quotes but prepend the character \ to the single quote inside the string. The opposite case is similar.
Let’s see all this with an example:
>>> hello = ‘Hello “Pythonist”‘
>>> hello_2 = ‘Hello \’ Pythonist \ ”
>>> hello_3 = “Hello ‘Pythonist'”
>>> print (hello)
>>> print (hello_2)
>>> print (hello_3)
Unlike other languages, there is no “character” type in Python. However, it can be simulated with a single character string:
>>> character_a = ‘a’
>>> print ( character_a )
Other python data types
So far, we have gone over the basic Python data types; however the language offers many more types. Here is a preview of the most important ones, although we will see them in detail in other tutorials.
In addition to basic types, other fundamental types in Python are sequences (list and tuple), sets (set), and maps (dict).
They are all compound types and are used to group multiple values together.
- The lists are mutable sequences of values.
- The tuples are immutable sequences of values.
- The sets are used to represent unique sets of elements, i.e., a joint can not be two identical objects.
- The dictionaries are special types of containers that can access their items from a single key.
Know the type of a variable
Now I am going to introduce you to two functions so that you can play with everything that we have seen in this tutorial. They are type() e isinstance():
- type() receives an object as a parameter and returns its type.
- isinstance() receives two parameters: an object and a type. Returns True if the object is of the type that is passed as a parameter and False otherwise.
The last thing we’ll see in this data type tutorial is type conversion.
What does this mean?
Imagine you have a variable age of type string whose value is ’25’. You could say that age although it is really a character string, it contains a number. However, if you try to add 10 to age, the interpreter will give you an error because age is type str, and 10 is a numeric type.
How can I treat the variable age as a number? Converting it to a numeric type, for example, to type int.
To do this, Python offers the following functions:
- str(): Returns the string representation of the object that is passed as a parameter.
- int(): Returns an int from a number or sequence of characters.
- float(): Returns a float from a number or sequence of characters.
- complex(): Returns a complex from a number or sequence of characters.
If the above functions are passed an invalid value as a parameter, the interpreter will display an error.
Well, I hope you enjoyed this short series of posts on Python data types as much as I have done writing it. Don’t miss the next articles as those are the continuation of this one. In those posts, we will see other Python language elements that are indispensable to carry out operations on the different types of data.