Now that we know how to interact with Python interpreter using REPL and execute our code, we are ready to learn more about Python language. And first topic for discussion is Python Data Types and their Usage.

In this post, we will learn about several basic data types such as numeric, string and Boolean types. By the end, you will have a good understanding of the various Python Data Types and how to use them for your application.

So let’s start.

1 – Integers

With Python version 3, there is no limit to how long an integer value can be. Of course, it also depends on the size of the system memory. But beyond that, an integer can be as long as you need it to be.

Python interprets a sequence of decimal digits without any prefix to be a decimal number.

See below example:

>>> print(15)
15

Similarly, we can have a very large integer number as below:

>>> print(3434343434343434666666 + 1)
3434343434343434666667

We can also represent binary, octal and hexadecimal numbers in Python by prepending the below strings before the integer values

PrefixInterpretationBase
0b
0B
2
0o
0O
Octal8
0x
0X
Hexadecimal16

For example, see below octal representation

>>> print(0o10)
8

Next, we have the hexadecimal representation

>>> print(0x10)
16

Finally, we also have the binary representation as below.

>>> print(0b10)
2

The underlying type of a Python integer irrespective of the base is int.

>>> type(0o10)
<class 'int'>
>>> type(0x10)
<class 'int'>
>>> type(0b10)
<class 'int'>
info

INFO

Deterministically basically means that natural events or social phenomena can be causally determined by preceding events or natural laws. In the context of computer science, deterministic algorithm is one that gives the same output for a particular given input. The machine that executes the algorithm goes through the same set of states to arrive at the output.

2 – Floating Point Numbers

The next important data type to consider is the float data type. This type designates a floating-point number. The float values are specified with a decimal point.

We can also use the character e or E followed by positive or negative integer to specify scientific notation.

See below example of various usages of floating point numbers

>>> 5.2
5.2
>>> type(5.2)
<class 'float'>
>>> 5.
5.0
>>> .2
0.2
>>> .5e7
5000000.0
>>> type(.5e7)
<class 'float'>
>>> 5.2e-4
0.00052

3 – Complex Numbers

We can also define complex numbers in Python. These type of numbers comprise of real part + imaginary part.

For example:

>>> 2 + 3j
(2+3j)
>>> type(2+3j)
<class 'complex'>

4 – String

String would probably be the most commonly used data type. It is nothing but a sequence of characters.

The string type in Python is called str

>>> print("This is an example of String")
This is an example of String
>>> type("This is an example of String")
<class 'str'>

A string in Python can contain as many characters as you wish. It is constrained only by the memory of the system.

A string can also be empty.

Let us try something with the string data type.

>>> print('I am a string with single quote ' character')
  File "<stdin>", line 1
    print('I am a string with single quote ' character')
                                             ^
SyntaxError: invalid syntax

Here, we get an error. The issue is that we already have a closing quote. But then there is a second one after the word character. Python assumes the second quote is the ending quote and hence, it throws a syntax error after that.

But what if we really want to have the quotation mark as part of the string?

If we really wish to include the quote, the easiest way is to delimit the entire string using the other type of quotes. See below example:

>>> print("I am a string with single quote ' character")
I am a string with single quote ' character
>>> print('I am a string with single quote " character')
I am a string with single quote " character

As you can see, we are using the other type to delimit the string when we want to print a certain type of quotation mark.

Conclusion

With this, we have successfully looked at the key Python Data Types and their usage. Armed with this understanding, we are in a good position to move on with more complex topics in Python.

In the next post, we will be looking at Python Variables.

If you have any comments or queries, please feel free to mention in the comments section below.

Categories: BlogPython

Saurabh Dashora

Saurabh is a Software Architect with over 12 years of experience. He has worked on large-scale distributed systems across various domains and organizations. He is also a passionate Technical Writer and loves sharing knowledge in the community.

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *