In this notebook we will quickly go over Python at a high level. We will then go on to explore topics in more detail.
We will be creating computer programs to read, manipulate, visualize, analyze, and save air quality, atmospheric, and health data using functions that are defined in Python. Before we start with functions, however, we need to briefly touch on what data are, from the perspective of a Python program and that is rooted in the concept that in Python everything is an object.
Python programs are build from objects. Everything in Python is an object.
The most important compoenents of the programs we will be writing are data objects and function objects. Using a biology analogy data objects are like the blood circulating through our bodies and function objects are like our organs that take blood in and return modified blood and some biological behavior.
We will briefly look at functions and then data.
To get a handle on what a function is in Python, let's look at some example functions from "high school" mathematics
\begin{eqnarray} y = \sqrt{x},\\ y = mx+b. \end{eqnarray}In each of these examples, we take a value $x$ and returns a new value that we are assigning to the variable $y$.
In Python a function takes zero or more arguments (objects) and returns some object.
printThe first Python function we'll look at is print. print takes as input a string and returns as output that string "printed" to the screen. If we don't provide a string, print prints an empty string (which is nothing) to the screen.
In [ ]:
print("Hello, world")
print()
print(5+4)
In [ ]:
print("thing 1")
print("thing 1", "thing 2")
print("thing 1", "thing 2", "thing 3")
All is number.
~Pythagoras
Python has multiple data types, but the most important kinds that we will be dealing with are
Numeric
int). Integers are created by typing an integer (e.g. 1, 532, -4). (There is also an int function for converting (or trying to) an object to an integer.)float). Floating point numbers correspond to the real ($\mathbb{R}$) numbers and can be created by typing a decimal number (e.g. 4., 3.5, 5.4e3), by doing division (e.g. 3/4), or from the float function.Strings (for representing text) (type str). Strings are a sequence of characters and are defined by typing the characgters between single quotes (e.g. 'abc'), double quotes ("def"), or either triple single or triple double qutoes ('''ghi''',"""klm""").
Data will be either a literal or a variable. Consider the following Python statements
5+4
a=5
a+4
In [ ]:
name = "george washington"
In [ ]:
name also has methods functions that do things to the string. One such method is capitalize.
In [ ]:
name.capitalize()
In [ ]:
Computer programs are essential to modern science. They do everything from control the instruments we use (e.g. microscopes or telescopes), store the data we've collected, and help us analyze and understand the data we've collected.
We're going to use Python to write a very simple program to help us understand the relationship between weather and air quality. In later classes, we'll write programs to acquire sensor measurements from a remote computer using an application program interface (API) and explore the relationship between air quality, elevation, and wind.
In [ ]:
a_num = 5
a_num_also = 4.5
rslt = a_num + a_num_also
print(rslt)
a_num = 4 # assign a new value to a_num
print((a_num-rslt)/3)
x = int("54")
print(x)
In [ ]:
first_name = "Brian"
last_name = "Chapman"
name = first_name+last_name
print(name)
print(first_name[2])
In [ ]:
Conditional execution involves asking true or false questions about our data and doing different things depending on the answer. These questions are in the form of
if SOME_TRUE_OR_FALSE_QUESTION: # IF THE ANSWER IS TRUE THEN WE EXECUTE THE INDENTED CODE BLOCK
DO SOMETHING HERE
elif SOME_OTHER_TRUE_OR_FALSE_QUESTION: # elif stands for "else if"
DO SOMETHING DIFFERENT HERE
else:
DO SOME DEFAULT EXECUTION HERE IF ALL THE OTHER QUESTIONS WERE ANSWERED FALSE
We can have multiple elif statements and we don't have to have an else statement.
Our if/elif/else example above is an example of code blocks. Python uses colons (:) and indentation to indicate code blocks: code that goes together. Code blocks will show up in many places including if/else blocks, loops, and functions.
In [ ]:
bmi = 27.3
if bmi < 18.5:
print("underweight")
elif 18.5 <= bmi < 25:
print("normal")
elif 25 <= bmi < 30:
print("overweight")
elif 30 <= bmi < 35:
print("obesity")
else:
print("extreme obesity")
In [ ]:
There are three basic ways to repeatedly do something in Python
while loopsfor loopsWith while loops we do something as long as a condition is true
In [ ]:
x = 0
while x < 5:
x = x + 1 # could also write x += 1
print(x)
With for loops we loop (iterate) over elements of a sequence, in this case a list of integers.
for loops will be our primary mechanism for iterating over data.
In [ ]:
for elem in [1, 2, 3, 4, 5]:
print(elem)
<span xmlns:dct="http://purl.org/dc/terms/" property="dct:title">University of Uah Data Science for Health</span> by <span xmlns:cc="http://creativecommons.org/ns#" property="cc:attributionName">Brian E. Chapman</span> is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.|
In [ ]: