The Different Types of Data - Python Bootcamp: The Crash Course for Understanding the Basics of Python Computer Language (2016)

Python Bootcamp: The Crash Course for Understanding the Basics of Python Computer Language (2016)

Chapter 5. The Different Types of Data

Basically, data types define an object’s capabilities. In other languages, the effectiveness of an operation is tested by ensuring that the object cannot be stored where the operation is going to be performed. This system is known as static typing.

However, Python uses a different approach. This programming language allows you to store the object’s data type inside that object. Python also checks the validity of each operation as soon as you run them. Programmers refer to this system as dynamic typing.

This chapter focuses on the different kinds of data that you can use with Python.

The Standard Types

Python has a set of standard data types. These types are pre-installed into this programming language. Let’s divide these types into small groups. This section will use the hierarchy system used in Python’s official documentation:

The Numeric Types

· int– This stands for integers. For Python 2.x,“int” is identical to C longs.

· long– It stands for long integers whose length is non-limited. You’ll find this type in systems that use Python 2.x.

· float– This stands for floating-point numbers. Float is the equivalent of doubles in C.

· complex– This type is composed of complex numbers.

The Sequences

· list

· tuple

· byte– This is a sequence of numbers within the 0-255 range. You’ll find bytes in systems that use Python 3.x.

· byte array– This is the mutable version of bytes.

· str– This stands for“String.” Python 2.x systems represent strings as sequences of 8-bit items. Python 3.x systems, however, represent them as sequences of Unicode items.

The Sets

· set– This is an unorganized group of distinct objects.

· frozen set– This type is the immutable version of sets.

The Mappings

· dict– This stands for Python dictionaries. Computer programmers refer to this type as a“hashmap” or“associative array.” In general, each element of a dictionary has a corresponding definition.

Mutable and Immutable Objects

In the Python language, data types are categorized based on the mutability of their contents. Keep in mind that immutable data types prevent you from changing the objects inside them. That means you’ll succeed in slicing or reassigning the objects of mutable data. Immutable ones, however, will give you an error message.

Here’s an important principle that you should remember: variables are simple references to the objects inside a machine’s memory. Let’s assume that you paired an object and a variable using the following statement:

With the statement given above, you are making variables (i.e. 1, a, and s) point to certain objects. Python stores this relationship between variables and objects in the machine’s memory. Thus, you can conveniently access objects whenever you want.

For the next example, let’s say you performed a reassignment using the code below:

In this new statement, you linked the variables to other objects. As you’ve learned earlier, you can only change mutable objects (1 [0] = 1 is good, but s [0] =“a” will give you an error message).

How to Create Objects of a Defined Type

· Literal Integers – You can enter literal integers in three different ways:

o For decimal numbers– You can enter these numbers directly.

o For hexadecimal numbers– You have to prepend 0X or 0x to enter this kind of number.

o For octal literals– The method of entering these integers depends on the Python version you are using:

§ For Python 2.x– You must prepend a zero to enter octals.

§ For Python 3.x– You should prepend 0O or 00 to enter octals.

· Floating Point Integers – You can enter these numbers directly.

· Long Integers– You can enter a long integer in two ways:

o Directly (112233445566778899 is considered as a long integer)

o By appending the letter“L” (1L is considered as a long integer).

If a computation that involves short integers overflows, it is automatically converted into a long integer.

· Complex Numbers – You can enter this object by adding two numbers (i.e. a real number and an imaginary number). Then, enter these numbers by appending the letter“j.” That means 11+2j and 11j are complex numbers.

· Strings– You can enter strings as single- or triple-quoted objects. The difference between these two types lies in their delimiters and their potential length. Single-quoted strings are restricted to one line only. You can enter single-quoted strings using pairs of single quotation or double quotation marks. Check the following example:

Triple-quoted strings are similar to their single-quoted counterparts, but they can cover multiple lines. Obviously, their delimiters (i.e. the quotation marks) should be matched. You must enter these strings using 3 single or double quotation marks. Here’s an instructive screenshot for you:

· Tuples - You can enter tuples using parentheses. Place commas between objects to separate them.

You can enter a single-element tuple by enclosing it in parentheses and adding a comma. Here’s an example:

· Lists - Lists work like tuples, though they require square brackets:

· Dictionaries – You can create“Python dicts” by listing some pairs of values and separating each pair using a colon. Use commas to separate dictionary entries. Then, enclose the statements using curly braces. Check the image below:

Null Objects

Python uses“None” as a null pointer analogue. In this aspect, Python is similar to many programming languages. Actually,“None” isn’t a null reference or a null pointer in itself– it is an object that only has one instance. You can use“None” as a default argument value for functions. In Python, you must compare objects against“None” using“is” instead of“==.”