Hello, world.
¶This tutorial was generated from a Jupyter notebook. You can download the notebook here.
Welcome to the Introduction to Programming for the Biological Sciences Bootcamp! We will be using Python as the programming language, and we will use this first lesson to make sure your Python distribution is working properly. We will also make sure you have a way to use the command line on your machine.
Prior to starting the bootcamp, you should have completed Lesson 0: Configuring your computer.
We are at an interesting point in Python's history. Python is currently in version 3.5 (as of September 13, 2015). The problem is that Python 3.x is not backwards compatible with Python 2.x. Many scientific packages were written in Python 2.x and have been very slow to update to Python 3. However, Python 3 is Python's present and future, so all packages eventually need to work in Python 3. Today, most important scientific packages work in Python 3. All of the packages we will use do, so we will use Python 3 in this course.
Hello, world.
and the print()
function¶Traditionally, the first program anyone writes when learning a new language is called "Hello, world.
" In this program, the words "Hello, world.
" are printed on the screen. The original Hello, world.
was likely written by Brian Kernighan, one of the inventors of Linux, and the author of the classic and authoritative book on the C programming language. In his original, the printed text was "hello, world
" (no period or capital H
), but people use lots of variants.
In these lessons, which are generated using Jupyter notebooks, code is shown and exectuted in special cells, like the one below.
# This is a code cell.
# It does not contain any executable code, just comments.
In looking at this code cell, we have already learned our first bit of Python syntax. Lines beginning with the pound sign (#
) are comments and are ignored.
Ok, now a code cell with Hello, world.
print('Hello, world.')
Hooray! We just printed Hello, world.
to the screen. To do this, we used Python's built-in print()
function. The print()
function takes as an argument a string. It then prints that string to the screen. We will learn more about function syntax later, but we can already see the rough syntax with the print()
function.
Let's try printing something else.
print(1, 2, 3, 4)
Ah! We see that the print()
function can take multiple arguments and print them in succession. What if each number had its own print()
function?
print(1)
print(2)
print(3)
print(4)
We see that the print()
function prints a new line with each command. But what if we wanted to have successive print()
calls, but everything displayed on one line?
print(1, end=' ')
print(2, end=' ')
print(3, end=' ')
print(4)
Here, we have used the end
keyword argument to specify that the string we print should end with two spaces, and not a new line. We will learn about keyword arguments later.
With the print()
function in hand and our ability to make the Python interpreter greet the world, we are now ready to venture into programming!