Python Data Types: One Crucial Difference

Variables are labels which are pointing to a memory location. We can use these memory locations to store our values. The value which you set belongs to a particular python data type.

In Python:

a = 3

In this example, we are creating a variable named a which refer to a memory location. It contains the integer value 3.

We can use these variables in subsequent statements:

b = a + 2

Our new variable b is equal to 5.

Python is a Dynamically Typed Language

If you have used and learned other languages: Java, Delphi, C#, there is one crucial difference:  Python is a dynamically typed language instead of statically typed language.

What this means is, in Python variables don’t have any type. The value has a data type. You can set value from any data type to a variable:

a = 5            # int
a = 'john'       # string

While in another programming language (C#), the statements below do not compile:

int a = 5;
a = "john";   // compile time error: the variable is defined as an int.

Everything is an Object

In Python, everything is an object. What implications has this statement? 

The primitive and built-in types (int, long, str, float, etc.) are an object, therefore, can be subclassed. The classes themselves are objects. Objects have attributes and methods.

Variables could be from 6 principal data types.

The principal built-in types are numerics, sequences, mappings, classes, instances, and exceptions.

Before we learn data types let me show you a useful function: type

Type function shows you what the type of the variable or the value is:

a = 3
print(type(a))  # <class 'int'>

b = 'john wayne'
print(type(b))  # <class 'str'>

c = True
print(type(c))  # <class 'bool'>

If you are any time confused about what the data type of the value is you can use the type function.

Now see these types one by one.

Numeric Types

There are three numeric types: integer, floating point number and complex number.

Boolean data type is also a numeric type. It is a subtype of integers.

You can use arithmetic operators to manipulate numeric types: addition (+), multiplication (*), division (/), etc.

Integer: -2, 5, 55, 148, 2193413249999, -84747777

Integers are numbers that do not contain a fractional part. Signed and unsigned numbers are implemented by Integer data type. Integers are in unlimited size. Therefore, it is only restricted by memory size.

Therefore there is not a long type as with other programming languages. While in Python 2 there is a long type while in Python 3 there is only one data type that represents integer numbers.

It makes everything easier. In other languages, there are many integer data types: short, byte, int32, int64, etc.

Floating Point Numbers: 1.0, 38e7, -7.6

Floats are numbers which contains fractional part (numbers with decimal points).

Complex Numbers: 1.0 + 5.9j, 6.2j

Complex numbers have a real and imaginary part, which are each a floating point number.

You can extract these parts from a complex number z, using z.real and z.imag.

z = 1.8 + 2.0j

print(z.real)          # 1.8

print(z.imag)          # 2.0

Boolean: True, False

Boolean values are subtype of Integer type.

Sequence Types


List is the most useful data type in python. It is one of the parts of the language that makes python powerful and expressive.

A list contains a series of value. Values can be of any type. Lists enclosed by a bracket []. The items in the list are separated by comma (,).

mylist = [1,3,5,9]                   # mylist contains numbers
anotherlist = [5, 'john', True]      # lists can contain different types

Lists are indexable. Therefore, you can access the elements of lists with [n] operator:

print(mylist[1])             # prints 3, python uses zero-based indexes.

To access to elements of the lists we can use the slice (:) operator:

print(mylist[1:3])           # [3, 5]

Slice operator, [x:y], returns a list from the x’th item to the y’th item including first but excluding the last.

Python lists are zero-based index. So when I say mylist[1:] it means, return a list from the second item to the end of mylist.

Some examples would be useful:

mylist = [1,3,5,9,11,13,15]

print(mylist[0:4])         # [1, 3, 5, 9]

print(mylist[:5])          # [1, 3, 5, 9, 11]  start from the beginning

print(mylist[1:])          # [3, 5, 9, 11, 13, 15]  to the end

print(mylist[:])           # [1, 3, 5, 9, 11, 13, 15] copy the whole list

Some build-in functions that operates on lists are: len, max and min

print(len(mylist))       #  7

print(min(mylist))       #  1 

print(max(mylist))       #  15


Tuples are similar to lists, except with one big difference. They are immutable. Above all immutable means, once you create the object, you cannot change its content.

It’s like constants. All list functions work on tuples: len, min, max, etc. In contrast, you cannot use list functions which changes the content of the tuples.

Therefore you may wonder why tuples exist in the first place. Tuples have some advantages over lists:

  1. It is faster. If you have a value, which you don’t need to update, use tuples.
  2. It makes your program more secure. If you have some write-protected data, it guarantees that will never override the values.
tpl = (4, 5, 6, 'fadfa')

print(len(tpl))      # 4

If I try to append some value, it gives an error.

(PyCharm message: Process finished with exit code 1 ).


The range type represents an immutable sequence of numbers like tuples. Range is commonly used for looping a specific number of times in loops.

for x in range(10):

#prints 0,1,2,3,...,9

String (Textual Sequence Type)

The string value can be defined as the sequence of characters represented in the quotation marks. In python, string literals are surrounded by single or double quotes.

String data type could be defined as a textual sequence type. Like with list and tuple data types, slice operator works on string literals.

mystr = 'john'        # enclosed with a single quote

mystr2 = "bill"       # enclosed with double quote

print(mystr[1:3])     # oh


Set is an unordered collection of items besides that it is defined by values separated by a comma inside braces { }. Secondly, items in a set are unique. They are not ordered and not indexed.

fruits = {'apple', 'banana', 'kiwi'}

Dict (Mapping Type)

Dictionary is an unordered collection of key-value pairs which are unordered and indexed.

In Python, dictionaries are defined within braces {} with each item being a key-value pair. Key and value can be of any type.

person = {'name': 'john', 'age': 39}
person['name'] = 'alex'  # set the value to alex for key name.
print(person['age']) # prints 39.
person['city'] = 'washington' # Add a new key 'city' with the value washington

Conversion between data types

To be able to convert data types to another data type, python provides some useful functions like int(), float(), tuple(), set(), etc.

s = '1999'

myint = int(s)                            # converts to int

print(myint, ', type:', type(myint))      # 1999 , type: <class 'int'>

myfloat = float(myint)                    # converts to float

print(myfloat, ', type:', type(myfloat))  # 1999.0 , type: <class 'float'>

mytuple = tuple(s)                        # converts to tuple

print(mytuple, ', type:', type(mytuple))  # ('1', '9', '9', '9') , type: <class 'tuple'>

myset = set(mytuple)                      # converts to set

print(myset, ', type:', type(myset))      # {'1', '9'} , type: <class 'set'>

mylist = list(mytuple)                    # converts to list

print(mylist, ', type:', type(mylist))    # ['1', '9', '9', '9'] , type: <class 'list'>

Next: A gentle introduction to python operators.

Yorum bırakın

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir

Scroll to Top