Getting Acquainted with Python Data Types

Written by Dan Sackett on September 11, 2014

The Python programming language, like any other, has a number of standard data types that it supports. In this post, let's look at those and some of the basic operations available.

For a lot of people coming from a typed language like Java, Python seems harder to understand and more error prone. With types, you know what to expect with variables and what kind of return value a function has. Python is incredibly loose in this regard but luckily the error messages are actually very helpful.

That said, reading code is quite easy because of the clear differentiation between data types. With Python, out primary building blocks are:

These five pillars will allow you to do just about everything you need to in Python. Let's see how each one is set up and some of the basic operations we can do with them.


Integers are our numbers. We can do math on them, make comparisons, and index with them. In python, defining an integer is simple.

x = 1
y = 9

An integer is simply one of many number types in python. The other numbers are:

# Floats
float = 3.14

# Long
long = 272849505L

# Complex
complex = 3.14j

Still, the integer is going to be your most common number to work with. With integers we can do a number of simple math commands such as:

1 + 1  # Addition
4 - 3  # Subtraction
3 * 5  # Multiplication
25 / 5 # Division
5 % 6  # Modulo (Remainder)

There are of course plenty more but I'll save python math for its own article. We can also do comparison with integers.

1 < 3   # Less than
7 > 20  # Greater than
4 == 4  # Equal to
10 >= 9 # Greater than or equal to
5 <= 8  # Less than or equal to

As you can see, python numbers are very standard.


A string is a quoted group of items in succession.

my_string = "Hello, world!"
my_numbers = "1, 2, 3, 4, 5"

In python, strings can be indexed:

str = 'Hello World!'

print str          # Prints complete string
# Hello World!
print str[0]       # Prints first character of the string
# H
print str[2:5]     # Prints characters starting from 3rd to 5th
# llo
print str[2:]      # Prints string starting from 3rd character
# llo World!

As well, we can add to them or repeat them.

print str * 2      # Prints string two times
# Hello World!Hello World!
print str + "TEST" # Prints concatenated string
# Hello World!TEST

As another note, strings can be defined with single quotes ' or double quotes ". If you choose to use single quotes, be sure that you escape single quotes within the actual string with a backtick like 'isn\'t'.


Lists are to python as standard arrays are to other languages. They actually allow a lot of the same operations that strings allow.

list = ['dan', 89, 'Pennsylvania', 3.14]

print list           # Prints complete list
# ['dan', 89, 'Pennsylvania', 3.14]
print list[0]        # Prints first element of the list
# ['dan']
print list[1:3]      # Prints elements starting from 2nd till 3rd 
# [89, 'Pennsylvania']
print list[2:]       # Prints elements starting from 3rd element
# ['Pennsylvania', 3.14]

Notice that you can mix types within a list. We can also add to a list or multiply it.

list2 = [9, 'Blimp']

print list2 * 2      # Prints list two times
# [9, 'Blimp', 9, 'Blimp']
print list + list2   # Prints concatenated lists
# ['dan', 89, 'Pennsylvania', 3.14, 9, 'Blimp']

There a number of other operations we can do on lists but that will also be saved for a dedicated post.


Dictionaries (dicts) are your hashmaps and associative arrays in python. They are composed of keys that point to values.

dict = {}
dict['one'] = "First"
dict[2] = "Second"

compact_dict = {'name': 'Dan', 'gender':'Male', 'title': 'Full Stack Developer'}

We can create empty dictionaries with the {} syntax and add keys to it dynamically. Keys can be words or direct indexes.

print dict['one']           # Prints value for 'one' key
# First
print dict[2]               # Prints value for 2 key
# Second
print compact_dict          # Prints complete dictionary
# {'name': 'Dan', 'gender':'Male', 'title': 'Full Stack Developer'}
print compact_dict.keys()   # Prints all the keys
# ['name', 'gender', 'title']
print compact_dict.values() # Prints all the values
# ['Dan', 'Male', 'Full Stack Developer']

As you can see, we can get all of a dictionary's keys or values with simple methods. One thing to remember though is that a dictionary has no order. Don't expect it to come back ordered a certain way.


A tuple is a data type that is similar to the list. A tuple consists of a number of values separated by commas. Unlike lists, however, tuples are enclosed within parentheses and are immutable. Essentially, they are "read-only lists".

tuple = ('dan', 89, 'Pennsylvania', 3.14)
tuple2 = (9, 'Blimp')

print tuple           # Prints complete tuple
# ('dan', 89, 'Pennsylvania', 3.14)
print tuple[0]        # Prints first element of the tuple
# ('dan')
print tuple[1:3]      # Prints elements starting from 2nd till 3rd 
# (89, 'Pennsylvania')
print tuple[2:]       # Prints elements starting from 3rd element
# ('Pennsylvania', 3.14)
print tuple2 * 2      # Prints list two times
# (9, 'Blimp', 9, 'Blimp')
print tuple + tuple2  # Prints concatenated tuples
# ('dan', 89, 'Pennsylvania', 3.14, 9, 'Blimp')

Since we are printing, the addition and multiplication operations are legal. The following would not be legal though.

tuple[2] = 1000    # Invalid syntax with tuple
list[2] = 1000     # Valid syntax with list

Remember, they are immutable.

Converting to a New Type

Oftentimes you may want to convert one datatype to another. Python has several built-in functions for this case.

# Converts x to an integer. base specifies the base if x is a string.
int(x [,base]) 

# Converts x to a long integer. base specifies the base if x is a string.
long(x [,base] ) 

float(x) # Converts x to a floating-point number.

# Creates a complex number.
complex(real [,imag])

# Converts object x to a string representation.

# Converts object x to an expression string.

# Evaluates a string and returns an object.

# Converts s to a tuple.

# Converts s to a list.

# Converts s to a set.

# Creates a dictionary. d must be a sequence of (key,value) tuples.

# Converts s to a frozen set.

# Converts an integer to a character.

# Converts an integer to a Unicode character.

# Converts a single character to its integer value.

# Converts an integer to a hexadecimal string.

# Converts an integer to an octal string.


This list is merely an introduction to python datatypes. Look forward to future posts about manipulating and working with these data types.


comments powered by Disqus