Python Data Types

Hi there and welcome back. Working late tonight and I’m a quarter through, my coffee that is. Consulting work on the other hand, I’ve got a ways to go. In an earlier post I said I was going to be in “Ruby Land”, well there was an exotic land nearby and I just couldn’t resist – it’s called “Python Land”. “Ruby Land” was a bit more playful, it had jesters, castles, shiny gems and cute (yeah I said cute) little monsters of all sorts. But here in “Python Land” are a bunch of majestic data crunching machines, engineered meticulously for their purpose – perhaps to understand mankind.

Anyway I’ve decided to take on Python along with Ruby because I have this super weird tendency to contrast things, but what I really want to say is that I want to connect them, conceptually. If I had it my way I’d want to connect everything but that wouldn’t leave any room for curiousity and self discovery now would it. Alright moving onto Python…

From the get go we find that Python is much more complex in its nature. Python comes packed with a bit more data types compared to Ruby and PHP which make the language ideal for scientific computing, image and data processing, machine and deep learning – you name it Python has it. How complex? Well, Python also provides a class made specifically for complex numbers, of data type “Numeric“.

Using type(), we can verify complex numbers:


>> type(36+4j)
<class 'complex'>

Now we know about strings, ints and floats – we see them in Ruby, PHP and Javascript. But Python has two other data structures the others don’t have, it has “Tuple” and “Dictionary“. On top of that Python also has the “List” data structure which behaves similar to an Array. I’ll illustrate the differences down below:

Tuple

A Tuple is a bit like a “List”, but if you compare their properties/methods by running


>>> dir([1, 2, 3, 4]) // for list

And running


>>> dir((1, 2, 3, 4)) // for tuple

You’ll find that lists have way more built in functionality packed into it. But here’s why Python isn’t so much the wild child, Python is that complex child. Because Tuple’s are immutable (unchangeable) in nature, they can be used as Hash Keys. They remind me a bit like Symbols in Ruby, except

Tuple data structures have other awesome use cases I’ll mentioned below:

First we can pack them in a list like a dictionary:


[('Thor', 'Hammer', 'Chris Hemsworth'), ('Captain America', 'Shield', 'Chris Evans'), ('Tony Sark', 'Iron Man', 'Rober Downey Jr.')]

Second we can make things a bit easier on the eyes, readability:


[('Thor', 'Hammer', 'Chris Hemsworth'), ('Captain America', 'Shield', 'Chris Evans'), ('Tony Sark', 'Iron Man', 'Rober Downey Jr.')]

# versus

[['Thor', 'Hammer', 'Chris Hemsworth'], ['Captain America', 'Shield', 'Chris Evans'], ['Tony Sark', 'Iron Man', 'Rober Downey Jr.']]

Thirdly, we we can use a Tuple data structure when we know our data will remain constant, they don’t ever change, they’re fixed, static – they’re immutable.

So if you’re looking to mutate your data structure then use a List or a Dictionary. Just remember of the four data structures in Python (List, Tuple, Set, Dictionary), that Tuple data structures are immutable.

Yes I know we haven’t covered Set data structures, I will need to cover that on a future post. I wanted to give Tuple the center stage here. We’ve barely set foot on this island, don’t want to get swallowed by a Python on our first day now.

Python is no joke, it’s majestic. Stay tuned!