This notebook is a concise introduction to the very basics of the Python language that we will use.

- Getting started
- Modules
- Boolean and numeric variables
- Strings
- Data structures
- Iteration
- Defining functions

To get started, you can use your Notebook as a calculator. For example:

In [1]:

```
2 + 3
```

Out[1]:

In [2]:

```
10/4
```

Out[2]:

`x`. Because the variable does not yet exist, the assignment statements creates the variable.

In [3]:

```
x = 5
```

The `print` function allows us to display information.

In [4]:

```
print('For truth is always strange; stranger than fiction.') # Lord Byron (the # starts a comment)
```

We can also use the print function to output values.

In [5]:

```
x = 10
print(x)
```

The Python language by design has a small core. Most of the fuctionality that we need is in modules or packages that we need to explicity load into our session. There are two ways to do this: either by loading the entire package (or a subset of it) or a specific function.

In [6]:

```
import numpy as np # the np is an alias
np.sqrt(4)
```

Out[6]:

In [7]:

```
from numpy import sqrt
sqrt(4)
```

Out[7]:

The most basic data type is a Boolean variable, which can be either `True` or `False`.

In [8]:

```
x = False
print(x)
```

In [9]:

```
x = 2 > 0
print(x)
```

In [10]:

```
type(x)
```

Out[10]:

`False` is automatically converted to zero and a `True` is converted to one.

In [11]:

```
y = 2*x
print(y)
```

The basic numerical data types are integers and floats.

In [12]:

```
x = 2
type(x)
```

Out[12]:

In [13]:

```
x = 2.0
type(x)
```

Out[13]:

String variables represent text data.

In [14]:

```
sentence = 'For truth is always strange; stranger than fiction.'
type(sentence)
```

Out[14]:

We now take strings as an example to explore some of the basic functionality available in Python.

In [15]:

```
sentence.lower()
```

Out[15]:

`lower` is a **method** available for the string object. A method is a function that operates on the associated object.

In [16]:

```
'truth' in sentence # checks whether the substring 'truth' appears in the sentence
```

Out[16]:

In [17]:

```
sentence = 'It is sunny today'
print(sentence)
new_sentence = sentence.replace('sunny', 'raining')
print(new_sentence)
```

In [18]:

```
len(sentence) # how many characters in the string variable?
```

Out[18]:

In [19]:

```
sentence[:11] # selects the first 11 characters of the string
```

Out[19]:

In [20]:

```
x =[1, 'Science', True]
type(x)
```

Out[20]:

In [21]:

```
print(x)
```

In [22]:

```
felines = []
print(felines)
```

In [23]:

```
felines.append('lion')
felines.append('leopard')
print(felines)
```

In [24]:

```
big_five = felines + ['elephant','rhino', 'buffalo']
print(big_five)
```

In [25]:

```
marine = ['shark', 'salmon', 'dolphin']
```

In [26]:

```
animals = [big_five, marine]
print(animals)
```

In [27]:

```
big_five[0] # first element
```

Out[27]:

In [28]:

```
big_five[3] # fourth element
```

Out[28]:

In [29]:

```
big_five[-1] # last element
```

Out[29]:

In [30]:

```
animals[1] # the second element of animals is a list
```

Out[30]:

In [31]:

```
animals[1][1] # get the second of element of animals, then the second element of the result
```

Out[31]:

In [32]:

```
big_five[:2] # first two elements
```

Out[32]:

In [33]:

```
big_five[1:2]
```

Out[33]:

In [34]:

```
big_five[-2:] # last two elements
```

Out[34]:

We can delete elements from a list in different ways.

In [35]:

```
big_five.remove('buffalo')
big_five
```

Out[35]:

In [36]:

```
del(big_five[-1])
big_five
```

Out[36]:

A dictionary is like a list, but instead of being indexed by integers, the indices can be of almost any type. The indices of a dictionary are called keys. More formally, a dictionary is a mapping from keys to values.

In [37]:

```
# 2016 populations
population = {'Sydney' : 5005358, 'Melbourne' : 4641636} # Here, Sydney and Melbourne are the keys
type(population)
```

Out[37]:

In [38]:

```
population['Sydney']
```

Out[38]:

In [39]:

```
population['Melbourne']
```

Out[39]:

In [40]:

```
population['Brisbane'] = 2349699
print(population)
```

Iteration allows us to run a block of code repeatedly.

In [41]:

```
for animal in big_five:
print(animal)
```

In [42]:

```
for city in population:
print(city)
```

In [43]:

```
for city in population:
print(population[city])
```

In [44]:

```
for i in range(5):
print(i)
```

Here is a more complex example.

In [46]:

```
numbers = np.arange(0,12,2)
print(numbers)
```

In [47]:

```
total = 0
for number in numbers:
print(number)
total += number # sums the number to the total, equivalent to total = total + number
print('Total = {0}'.format(total))
```

Defining functions makes the coding process more efficient, prevents errors, and improves readability.

In [48]:

```
import math
```

In [49]:

```
def area(radius):
return math.pi*radius**2 # the double asterisk is a power
area(1)
```

Out[49]:

In [50]:

```
area(2)
```

Out[50]: