Python Data Types

Now that we have successfully created and ran a Hello World program. We will take a look at some of the ways in which Python represents data. It is important for every programming language to be able to work with different types of data, be it strings (like what we saw in our hello world), numbers or even a collection/group of data in the form of lists. These data types determine if and how data values can be manipulated.

Python has several in built data types which include:

  • Numbers
  • Strings
  • Lists
  • Tuples
  • Dictionaries

At this point, we will only briefly consider the data types. A more in depth look will be taken on how they work and how to manipulate them as we proceed.

We can easily determine what data type we are dealing with by entering the type() function.
We can also convert one data type to another using the data_type() funtion. This works provided the data types are compatible.

Numbers in Python

By default, numbers in python are represented as
Integers (int): These are either positive or negative whole numbers.
Float (float): These are floating point numbers. Numbers that have a value after the decimal point
Complex (complex): These are complex numbers. They are represented as x+iy, where x and y are real numbers and I is an imaginary unit which is the square root of -1.

Python will generally assign the correct number type to values entered but we can also convert between different types using int(), float() and complex()

Strings In Python

A string is a collection of characters. Strings are created by beginning and ending a character sequence with either a single quote (‘) or a double quote (“) like we saw in our Hello World. It is important to note that strings must begin and end with the same type of quotes for them to work. We can, however, use a different type of quote within a string freely.

Like was possible with numbers, we can convert number data types into strings using str().

Because strings are a collection of characters, we can get individual characters from within a string using the square parentheses ([]) and an index location which begins from 0 up to the length of the string in question minus 1.

Lists, Tuples And Dictionaries

A list is a collection of data types. Like strings, they are indexed from 0 to n-1 where n is the number of items in the list. Lists are created using square brackets [] and commas in between items. Empty lists can also be created.

Tuples are very similar to lists in the sense that they are also a collection of values. The major difference is Tuples are immutable, meaning their values cannot be changed after creation. Tuples are created using parenthesis () and commas between values. Empty tuples can be created.

Dictionaries are another collection of data similar to lists with one major difference being that values are stored in pairs with one part of the pair (the key) being a reference to the other part of the pair (the value). This makes it very easy to find items as keys are easier to remember than the index position of the values. Dictionaries are created using curly brackets {} with key:value pairs separated by commas.

Now that we know these data types, we will look at the ways we can store them or reference objects for later use.

Leave a Reply

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