#### Previous topic

Running Python code

#### Next topic

Handling FITS files

# Python Types and Operations¶

Python supports a number of built-in types and operations. This tutorial covers the most common types, but information about additional types is available here

## Basic numeric types¶

The basic data numeric types are similar to those found in other languages, including:

• Integers (int):

```>>> i = 1
```
• Floating point values (float):

```>>> f = 4.3
```
• Complex values (complex):

```>>> c = complex(4., -1.)
```

Manipulating these behaves the way you would expect, so an operation (+, -, /, *, **, etc.) on two values of the same type produces another value of the same type, while an operation on two values with different types produces a value of the more ‘advanced’ type:

• Adding two integers gives an integer:

```>>> 1 + 3
4
```
• Multiplying two floats gives a float:

```>>> 3. * 2.
6.0
```
• Subtracting two complex numbers gives a complex number:

```>>> complex(2.,4.) - complex(1.,6.)
(1-2j)
```
• Multiplying an integer with a float gives a float:

```>>> 3 * 9.2
27.599999999999998  # int * float = float
```
• Multiplying a float with a complex number gives a complex number:

```>>> 2. * complex(-1.,3.)
(-2+6j)  # float * complex = complex
```
• Multiplying an integer and a complex number gives a complex number:

```>>> 8 * complex(-3.3,1)
(-26.4+8j)  # int * complex = complex
```

However, there is one case where this happens but is not desirable, and that you should be aware of, which is the division of two integer numbers:

```>>> 3 / 2
1
```

This behavior is widely regarded as a huge design mistake and Python 3.x has been fixed to behave like you would expect (more on Python 3.x later). A way to prevent this is to cast at least one of the integers in the division to a float:

```>>> 3 / float(2)
1.5
```

or:

```>>> 3 / 2.
1.5
```

## Lists, tuples, and sets¶

There are two types of sequences that appear similar at first glance, both of which can contain inhomogeneous data types:

• Lists (list):

```>>> l = [4, 5.5, "spam"]
>>> l
4
>>> l
5.5
>>> l
'spam'
```
• Tuples (tuple):

```>>> t = (4, 5.5, "spam")
>>> t
4
>>> t
5.5
>>> t
'spam'
```

The difference between these two types is that lists are mutable, and tuples are immutable:

```>>> l = 3
>>> l.append('egg')  # For a full list of methods, type l. then press TAB!
>>> l.insert(3,'spam')
>>> l
[3, 5.5, 'spam', 'spam', 'egg']

>>> t = 3
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
```

There are reasons why tuples are a useful feature (faster and hashable are the two main ones), but for now, it’s enough for you to know there is such a difference.

One useful operation with lists and tuples is +, which can be used for concatenation:

```>>> [1,2,3] + [4,5,6]
[1, 2, 3, 4, 5, 6]

>>> ('spam', 'egg') + ('more spam','!')
('spam', 'egg', 'more spam', '!')
```

Lists can be sliced, meaning that we extract a chunk from the list:

```>>> a = ['spam', 'egg', 'bacon']
>>> a[0:2]
['spam', 'egg']
```

Sets (set) are a third type of sequence which you can make from a tuple or a list:

```>>> set([1, 2, 3, 2, 'spam', 'egg', 'spam'])
set([1, 2, 3, 'egg', 'spam'])
```

Note that duplicate items have been removed. This is the mathematical definition of a set, i.e. a collection of distinct objects. The order of the objects is arbitrary (order is not preserved). Various operators can be used to represent set operations:

```>>> set([1,2,3]) - set([3,4])
set([1, 2])

>>> set([1,2,3]) & set([3,4])
set()

>>> set([1,2,3]) | set([3,4])
set([1, 2, 3, 4])
```

## Strings¶

Strings (str) will be familiar from other programming languages:

```>>> s = "Spam egg spam spam"
```

You can use either single quotes ('), double quotes ("), or triple quotes (''') to enclose a string (the last one is used for multi-line strings). To include single or double quotes inside a string, you can either use the opposite quote to enclose the string:

```>>> "I'm"
"I'm"

>>> '"hello"'
'"hello"'
```

or you can escape them:

```>>> 'I\'m'
"I'm"

>>> "\"hello\""
'"hello"'
```

You can access individual characters or chunks of characters:

```>>> s
'e'

>>> s[9:13]
'spam'
```

Note that strings are immutable (like tuples), that is you cannot change the value of certain characters without creating a new string:

```>>> s = 'r'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
```

As for lists, and tuples, concatenation is done with +:

```>>> "hello," + " " + "world!"
'hello, world!'
```

Finally, strings have many methods associated with them, here are a few examples:

```>>> s.upper()
'SPAM EGG SPAM SPAM'  # An uppercase version of the string

>>> s.index('egg')
5  # An integer giving the position of the sub-string

>>> s.split()
['Spam', 'egg', 'spam', 'spam']  # A list of strings
```

## Dictionaries¶

One of the remaining types are dictionaries (dict) which you can think of as look-up tables:

```>>> d = {'name':'m31', 'ra':10.68, 'dec':41.27}
>>> d['name']
'm31'
>>> d['flux'] = 4.5
>>> d
{'flux': 4.5, 'dec': 41.27, 'name': 'm31', 'ra': 10.68}
```

## Numpy arrays (not a built-in type!)¶

Numpy is a package that provides a data type we will refer to as a Numpy array. This behaves similarly to arrays in e.g. Fortran or IDL:

```>>> import numpy as np
>>> a = np.array([1,2,3])
>>> b = np.array([4,5,6])
>>> a + b
array([5, 7, 9])
>>> a * 2 - b
array([-2, -1,  0])
>>> a
3
```

Numpy supports multi-dimensional array, and slicing, stepping, and indexing, similarly to other languages. Indexing is zero-based (the first element is a) and the last element in a slice is exclusive (a[0:2] returns an array made from a and a). Slicing an array return a reference, not a copy, of the data.

Note

How Python lists are different from Numpy arrays:

• Python lists can contain anything, including other lists, objects, or complex data structures.
• When you slice a Python list it returns a copy.
• Vector math does not work on lists. For example, multiplying a list by an int n gives n copies of the list, adding another list concatentates, and multiplying by a float gives an error.

## A note on Python objects¶

Most things in Python are objects. But what is an object?

Every variable or function in Python is actually a object with a type and associated attributes and methods. An attribute is a property of the object that you get or set by giving the <object_name> + dot + <attribute_name>, for example img.shape. A method is a function that the object provides, for example img.argmax(axis=0) or img.min().

Use tab completion in IPython to inspect objects and start to understand attributes and methods. To start off create a list of 4 numbers:

```l = [3, 1, 2, 1]
l.<TAB>```

This will show the available attributes and methods for the Python list a. Using <TAB>-completion and help is a very efficient way to learn and later remember object methods!

```In : a.<TAB>
a.__class__         a.__getattribute__  a.__le__            a.__reversed__      a.count
a.__contains__      a.__getitem__       a.__len__           a.__rmul__          a.extend
a.__delattr__       a.__getslice__      a.__lt__            a.__setattr__       a.index
a.__delitem__       a.__gt__            a.__mul__           a.__setitem__       a.insert
a.__delslice__      a.__hash__          a.__ne__            a.__setslice__      a.pop
```a.sort
Copyright: Smithsonian Astrophysical Observatory under terms of CC Attribution 3.0 