Data Types

When one creates a variable in a programming language it must be a specified type.  The type will determine how the data is stored and how operations will work on it. Python only has a few basic built in data types; boolean, integers, floats, complex, strings, lists and dictionaries.  Other data types can be added using packages.  Examples of assignment to the basic data types are the following:

b=False; a=True         #boolean can only be True and False
i=0; j=1                #integers - whole numbers
x=0.0; y=1.5;z=1.e5     #floats - decimal numbers
c=0+0j; d=1.5-0.5j      #complex - complex numbers, j is \sqrt{-1}
s=''; word="Hello"       #strings - a sequence of characters
l=[]; list=[1,2,3]       #lists - a sequence of anything
d={}; dict={1:'A',2:'B'} #dictionary - a mapping of things to other things

The only other basic type in python is called None and it is the value of a variable that doesn’t exist. The difference between different data types is how they behave under operations. For example, if x=1 and y=2 then x+y=3. But if a=’1′ and b=’2′ then a+b=’12’. Python defines data types implicitly, which means it guesses what you want. They can also be changed with operations. For the x and y above x/y would give 0.5 and change the data type to float. You can check the data type with the type() function, you can also convert to the data type you want using bool(),int(),float(), etc.


Strings and lists are of a kind of data type called a sequence. Sequences have values and an order. The values can be accessed by indexing which starts with 0. So if word=’Hello’, then word[0]=’H’ and word[2]=’l’. If list=[‘apple’,’orange’,’banana’] then list[0]=’apple’ and list[1]=’orange’. If you try to index a sequence with a number greater than the number of elements in it minus 1 this will give an error.

Numpy Arrays

Numpy introduces a new data type called an array (ndarray). An array is a sequence like a list, but it behaves more like a vector does in math. A numpy array will do addition, multiplication and exponentiation like you would want. Also there are hundreds of numpy functions that operate on them. Unlike lists arrays must be of one data type and their size must be specified. Lists of one data type can be converted into an array. Or one can create an array with certain values. Arrays can be multidimensional. Ways to create a numpy array:

import numpy as np



Print this page