Skip to content
Ryan James
Menu

← Python10 min read

We Need More Data!

Data types are a crucial part to any programming language and Python really makes it easy for anyone to create an object to contain multiple data types stored into it. It needs to be noted though that for this section, creating and using classes as a data object will not be discussed and will be coming in future sections.

We Need More Data!

Data Types

Data types are a crucial part to any programming language and Python really makes it easy for anyone to create an object to contain multiple data types stored into it. It needs to be noted though that for this section, creating and using classes as a data object will not be discussed and will be coming in future sections. This section will include examples of the following data types:

  • Lists
  • Tuples
  • Arrays

This does not include every single type of data type that you can use in Python to create objects that store multiple data types in them, but this should represent a good basis for understanding how to create and manipulate data objects in Python. So let’s get started!

Creating and Manipulating a ‘List’

Whenever I refer to a ‘list’ in programming, I’m trying to refer to the exact data type and not refer to a ‘list of numbers’ since there are multiple data types where we can store a ‘list of numbers’. We can even store a ‘list of numbers’ in a String if we wanted to. In the Python environment, a list is a data type where you can store multiple things from ints, floats, Strings, plus other data types like lists. So yes, you can store a list inside of a list due to the flexibility of Python’s data type structure.

myList = [1, 2, 3]

In the example shown above, we have created a list called myList. In myList there are 3 numbers stored: 1, 2, and 3. The indexing of a list begins with 0. So for this list the ‘1’ is stored at index 0, the ‘2’ is stored at index 1, and the ‘3’ is stored at index 2. This is very similar to how the indexing of Strings works.

myList = [1, 2, 3]
print(myList[1])

The code segment shown above will use the same list as previous, but will then print the second item in the list (index=0 is the first) which will print the number 2 on the screen.

There are multiple ways of printing a list of numbers to the screen. Most people would like to print a list separated by commas and that’s what has been done in the next 2 examples. The print methods shown will print identical things on the screen, they’re just 2 different ways of getting the same result.

myList = [1, 2, 3]

#print using a for-each loop
for number in myList:
   print(number, end=", ")

#print with no loops
print(*myList, end=", ")

With the example shown above, the text ‘1, 2, 3’ will be printed to the screen twice using both print methods. Again, both of these methods (plus more methods) will get what you want printed to the screen.

To add something to a list, you can use the append keyword and to remove something from a list you can use the remove keyword to remove a specific item or the pop keyword to remove the last item in the list.

myList = [1, 2, 3, 4, 5]
#add to the list
myList.append(12)
myList.append(15)
#remove the 3 from the list
myList.remove(3)
#remove the last item in the list, the 15
myList.pop()
print(*myList, end=", ")

In the code segment shown above, comments have been added to explain each of the list operations being used. At the end of the code segment, the print command is used to show what is left in the list. This will print the following to the screen after all of the manipulations: 1, 2, 4, 5, 12.

Creating and Manipulating a ‘Tuple’

Tuples are a data structure in Python that act a lot like Lists. When manipulating them, the indexes both start at 0 and printing them looks the same and both data structures support nesting like lists inside lists and tuples inside tuples. The exception is that tuples are considered immutable. What that means is that you cannot redefine a specific element in the tuple like you can a list.

myTuple = (3, 4, 5)
for number in myTuple:
   print(number, end=", ")

The code segment shown above demonstrates how to declare a tuple and then print it all on one line separated by commas. Now on to the redefinement of elements in a tuple:

myTuple = (3, 4, 5)
#this following line is OK
myTuple = (3, 1, 5)
#this is NOT OK
myTuple[1] = 1

In the code segment shown above a tuple is declared as myTuple. Then on the second line of code, myTuple is redefined which is perfectly ok. But on the last line, an attempt is made to redefine a specific element in myTuple which Python will not allow. When this is done it will stop the program and throw an error message on the screen.

Creating and Manipulating an ‘Array’

Arrays in Python are really useful because of the methods that come with them, but do take some more work to set up than the previous 2 data types that were previously mentioned. To start with, to use arrays, the array module must be imported. In all the examples that follow, that will be the first line of code shown.

from array import *
myArray = array('i', [5, 10, 15])

In the code segment shown above, an array of type ‘i’ is defined and the numbers ‘5, 10, and 15’ are put into the array for initialization. The type that comes first is necessary and sets up what can be put into the array. Type ‘i’ indicates that only signed integers of 2 bytes can be loaded into the array. Type ‘c’ indicates only characters can be put into the array and type ‘d’ indicates only floating point of 8 bytes can be put into the array. These are the most common data types to be loaded into an array.

Arrays are mutable like lists so their elements can be changed without a new array having to be created. The indices for arrays work like lists and tuples starting at zero. In the next example, some of the array methods are demonstrated.

from array import *
myArray = array('i', [5, 10, 15])
#search the array for '15'
print(myArray.index(15))
#insert an item into the array
myArray.insert(1, 25)
#delete the last item
myArray.remove(15)
#change the last item to 15
myArray[2] = 15
for number in myArray:
   print(number, end=", ")

In the example shown an array is initialized, ‘5, 10, 15’. Then the array is searched for the number 15 which will return ‘2’ since it’s at index 2. After that a new number, 25 is inserted at index 1 which changes the array to ‘5, 25, 10, 15’. In the last 2 lines, the 15 is deleted and the 10 is changed into a 15. With all of this code the array is printed and the result that gets printed to the screen is ‘5, 25, 15’.

To sum things up, 3 different options for creating data structures in Python were shown. Each of the data structures is very similar in how they are accessed. But, some are mutable and one of them was immutable. Also with arrays, extra methods are now possible since they are included with the arrays module. Each data structure has its own unique qualities that make it useful for the type of data that needs to be manipulated and the level of manipulation that will be used on the data. I hope this helps you next time you decide to use Python to manipulate a data set.

Keep coding!
Ryan