print('hello world')
hello world
Students will gain an introduction to programming in Python, working in interactive notebooks, and learning how to leverage outside resources for coding help. Moreover, students will gain a basic understanding of variables, data types, and working with simple expressions for comparison and computation.
Python is a general-purpose programming language widely used in scientific computing, image analysis, machine learning, and more. It allows you to specify a set of instructions, written as a script or a program, to execute a task of interest.
The purpose of this series of Python workshops is not to give you an extensive, in-depth overview of everything you can do in Python. Instead, we aim to equip you with the skills and terminology necessary to learn how to code in Python. Google, Stack Overflow, and GitHub Issues are invaluable tools you can use to your advantage for (1) gaining coding help and (2) learning how to write code by reading examples.
“The Jupyter Notebook is an incredibly powerful tool for interactively developing and presenting data science projects. A notebook integrates code and its output into a single document that combines visualizations, narrative text, mathematical equations, and other rich media. The intuitive workflow promotes iterative and rapid development, making notebooks an increasingly popular choice at the heart of contemporary data science, analysis, and increasingly science at large.” – Dataquest
All of our lessons will be presented in Jupyter notebooks due to their interactive nature (.ipynb file extension). They consist of two main components: a kernel
and cells
.
kernel
interprets and executes the code. Here we are using the Python kernel; however, you can specify a kernel for another language like R.cell
is a container for either text (Markdown) or code to be executed.To run the Python code in a cell, press Shift + Enter. Try it with the code below.
The simplest way to use Python is as a calculator. You can perform addition, subtraction, division, multiplication, and exponentiation. Let’s walk through some simple math calculations.
\[2 + 3\]
\[\frac{6}{2} \times 20 - 100\]
\[2^{8}\]
Was the above result what you expected? If you used ^
, your result would be 10; however, if you used **
, your result is 256. This demonstrates why it’s important to check the answers you get from your code to make sure the results seem reasonable. You might not always get an error when your code is incorrect.
What if you want to store some value (like the result of the above calculation) and use it for another task later? You can assign it to a variable with the assignment operator =
.
For example, let’s assign the value 22 to the variable x
:
Notice that the code above did not display any output. That’s because the value is now stored in the variable called x
.
Try assigning the values 1, 2, and 3 to the variables x
, y
, and z
, respectively.
If you’d like to see what the values of the variables are, you can use the print()
function. The input to the function (what goes inside the parentheses) is whatever you’d like to display to the terminal.
Once you have variables assigned, you can use them to do basic operations. For example, try calculating the following. Be sure to use the assignment operator when necessary and print your results to check that they make sense.
a
to the result of \(4 + 5 \times 2\).b
to the result of \(\frac{a}{2.5}\).Of note, you can name a variable almost anything you’d like (though it’s best to be descriptive for you and others who end up reading your code). However, a variable name must start with a letter and not a number. For fun, try assigning the value 3 to a variable called variable1
and see the result.
Programming languages often include different data types that can be used for various tasks (e.g., arithmetic, comparison, debugging). Here we’ll discuss four of the most common types: 1. integer: whole number 2. float: decimal number 3. string: text 4. boolean: True or False
Integers
Integers are positive or negative whole numbers with no decimal point. We’ve already been using them in today’s lesson. For example, print the variables x
, y
, z
, and a
.
Floats
Floats, short for floating-point values, are positive or negative numbers with decimals. As with integers, you can use them to do arithmetic or assign them to variables. Try defining and printing the following expressions:
f1 = 4.5
f2 = -3.0
f1 + f2
Strings
Strings refer to text (i.e., they’re a series of characters) and can be (1) assigned to variables for data manipulation or (2) used in print statements for debugging or monitoring tasks. You define them by placing the text within single or double quotation marks, as in:
oligo1 = 'GCGCTCAAT'
oligo2 = 'TACTAGGCA'
print(oligo1, oligo2)
print('String variable 1 is', oligo1)
GCGCTCAAT TACTAGGCA
String variable 1 is GCGCTCAAT
Booleans
Booleans are either True
or False
. Note the capitalization of the first letter. They are often used for comparisons (e.g., the output of 1 > 2
would be False
). Additionally, keep in mind that True
behaves as 1 and False
behaves as 0 in arithmetic operations.
Comparison operators (>
, <
, ==
, >=
, <=
) can be used to compare two expressions. The output of a comparison is a Boolean (True
or False
). We often use comparison operators to set up conditional statements (e.g., “If this comparison is True, then do something; if it’s False, do something else”). You’ll learn more about conditional statements in Lesson 2.
Let’s set up a few comparisons and use print statements to see the results:
print(2 > 1.5)
print(4 <= 2)
print(5 == 5.0)
print(6 >= 6)
print(7.4 < 3)
We’ve already started using one basic function, print()
. Each function has a purpose: it takes whatever is specified within the parentheses (the arguments), performs a task, and then outputs the result. Functions are extraordinarily useful for reproducible code, and you’ll learn more about them in Lesson 3.
For now, let’s explore some other basic functions. The function type()
returns the data type of the argument you give it. See the example below.
<class 'float'>
<class 'str'>
<class 'int'>
<class 'bool'>
Other useful functions let you specify or coerce a data type into another. int()
, float()
, bool()
, and str()
let you convert a value to an integer, float, boolean, or string. Let’s take a look at how this works for the int()
and str()
functions.
The following exercises will help you practice the concepts taught in this lesson.
1 + 3 * 3
(1 + 3) * 3
(4 * 4) + 5
1 > 3
"1" > "3"
(Which compares the strings lexicographically.)width
and height
, and assign them any positive integer values.area
.perimeter
.area
and perimeter
in descriptive sentences.n
and assign it any integer value.if
/else
) that prints “n is even” if n
is even, or “n is odd” if n
is odd. (Hint: use ("even", "odd")[n % 2]
.)n = 42
and n = 13
.10
12
21
False
False
# Question 2
width = 5 # example value, replace with any positive integer
height = 3 # example value, replace with any positive integer
area = width * height
perimeter = 2 * (width + height)
print(f"Area = {area}")
print(f"Perimeter = {perimeter}")
Area = 15
Perimeter = 16