March 21, 2017

Python: Starting Python for the First Time (Windows & Linux)

Python is a programming language that is popular as a first programming language. This "getting started" tutorial is aimed at Windows users, with suggestions for Linux users.

Install Python and related tools

Install one of these distributions of Python, pick only one:
  • Just pick ONE of these. Installing multiple will create different versions of Python on your computer and that will cause conflicts later.
  • Anaconda
    • If drive space isn't an issue (you'll need 5-6 GB), get this. It is the most common distribution used by beginners and advanced users.
    • Comes with a common packages for life sciences pre-installed (e.g. pandas, numpy, matplotlib)
    • Makes creating different Python environments easy
    • Pick this if you are attending an Intro to Python workshop
  • Miniconda
    • Lighter version of Anaconda. It comes with less pre-installed programs. I like it.
    • Won't have "Anaconda Navigator" window, so you will need to control Python environments through the terminal (might be frustrating for beginners)
    • Pick this if you already know how to program, have other programming software on your computer, and don't want the full Anaconda installation to create duplicates of Jupyter Lab and other tools.
  • Python.org
    • If drive space is very limited, download this basic version
    • Any additional packages will need to be installed separately. Beware, this requires more steps and is less beginner friendly. 
  • During installation, select click the box to add whichever Python version to your PATH environment variable. You want this! It allows you to run Python scripts from the terminal later. 
Install a text editor for programmers (lightweight software), pick one or more: 
  • The default text application (e.g. Notepad.exe for Windows) will work because programs are just text files with special file extensions, but text editors specifically for programmers will make your life easier by color-coding the programming language syntax. This helps you notice when you forget a closing parenthesis or add an extra apostrophe.
  • Notepad++ (Windows)
  • Sublime Text (Windows, Linux, macOS) 
  • Geany (Linux) 
Install more complex IDE software (highly recommended for beginners), pick one or more: 
  • Jupyter Lab is popular and great for multi-script projects. 
    • From Windows terminal (cmd.exe):
      python -m pip install jupyterlab
    • From Python terminal:
      pip install jupyterlab
  • Spyder comes bundled with Anaconda and is my favorite IDE for Python. It is less complicated the Jupyter Lab.
  • RStudio Desktop now supports Python but I don't recommend it if you are learning Python for the first time. It is useful if you plan to use both R and Python languages together in the future. 
Install pip and use it to install Python packages

Open Jupyter Lab and start writing basic Python code

On Windows with Anaconda, open "Anaconda Navigator" 
  • Click "Launch" on the box for "Jypyter Lab" or "Jupyter Notebook".

On Windows with Miniconda, open software "Anaconda Powershell Prompt"
  • Type into the terminal and press [ENTER] button:
    jupyter lab

Jupyter Lab will launch
Whichever method used, the computer will launch Jupyter Lab on a web browser.

Create a new Jupyter notebook file with...
File: New: Notebook

Navigate to the new notebook tab (Untitled.ipynb) and type code into the gray text book. For example:
x = 5
y = 10
x + y 
 
To run the cell (the block of code), either press the play triangle icon or use keyboard shortcut [SHIFT] + [ENTER]

You should see the output of your code and a new cell (gray text box for code):
15

Type and press [SHIFT] + [ENTER]:
z = x*x

Your code will run and you will get a new cell. However, it doesn't look like it ran (no output). Your code assigned a value to variable z, but it did not instruct Python to print the output.

To print the output, try one of these:
z
print(z)

Either of these will print the output (the value) of variable z to your window.

Note that function print() cannot have a space between print and the open parenthesis. The function needs to be attached to the input inside the parentheses.

Try this and output:
print(x+y)
print("Hello world")
z = "Beautiful"
print("Hello",z)


Note that you just replaced the value of variable z so now it has a string value ("Beautiful") instead of a numerical value (x*x or 25). Variable values can be replaced. When writing code, keep track of what assigns values to your variables and when you are rewriting them.

You can also see the data types of your variables, for example:
print(x, type(x))
print(y, type(y))
print(z, type(z))
 
Remember to close every parenthesis that you open, or you will get an error.
'int' = integer
'str' = string (the programming name for "text" data)

There are different type of number datatypes. For example, change y to a number with decimals:
y = y+0.01
print(y, type(y))

'float' = number with decimals

Math expressions with comments after the hashtag (#):
2 + 1 # addition
2 - 1 # subtraction
2*3  # multiplication
18 / 2 # division
2**3 # exponent

Adding comments is very helpful to take notes on your code. Write #comments regularly.

Conditional statements, if else code blocks, and loops

Relational operators:

x = 100

y = 2 

x < y  # less than

x > y  # greater than

x  <= y # less than or equal to

x >= y # greater than or equal to

x == y # double equal sign evaluates for equivalency (is x equal to y?)

x != y # is x NOT EQUAL to y?


Create ifelse statements.

x = 15

y = 10

if x > y: #Note the use of the colon to indicate the end of the if statement to be evaluated

print(str(x)+ " is greater than " +str(y))  #Code to run if statement is true

else: #What to run if the conditional statement is not true

print(str(x)+ "is " +str(y)+ "or less")


Note that Python requires correct indentation of lines for ifelse statements. Not all programming languages enforce this, but Python is strict about it.

The colons after the if conditional statement and after else are important to tell Python what to do. Python will evaluate if the conditional statement is true or not, then decide which code block to run.

Create a while loop. It repeatedly runs a loop until the conditional statement is no longer true.

iteration=0

while iteration < 5:

print("Iteration:", iteration)

iteration += 1 #increases variable 'iteration' by 1 each time the while loop is run


Be careful with while loops. If you don't create a way for it to be interrupted, you can manually stop the code with the square stop icon.

Combine conditional statements and decide if either needs to be true, or if both need to be true.

x=2 

y=15

a=5

b=6

is_true = x>y or a<b

print(is_true)

is_true = x>y and a<b

print(is_true)


Copying and aliasing comparisons:

a=[2,3,4]
b=a #tell computer to use the same value from a for b
print(a is b)
b=[2,3,4]  #write object b with values [2,3,4], same values as a but it's a different object
print(a is b) 
a==b #compare values of the variables

True
False
True

Lists, dictionaries

Ranges:

for i in range(5):

print(i)

0

1

2

3

4

for i in range(25,30):

    print(i)

25
26
27
28
29

Pre-defined lists and using list indexes to pull out values:

my_list = [1, True, 3, 'Vegas', 5]

print(my_list[3])

Vegas

Because Python is zero-indexed (lists begin with zero), the list item 3 is actually the 4th item because Python counts 0,1,2,3,4.

Strings can also be lists of characters:

word = "Python"

for letter in word:

    print(letter)

p

y

t

h

o

n


Use loops to add values to lists:

Long way:

squares = []
for i in range(5):
    squares.append(i**2)
print(squares)

[0, 1, 4, 9, 16]


Concise way:

squares = [i**2 for i in range(5)]
print(squares)


Slice lists (get list subsets):

fruits = ["apple", "banana", "kiwi", "guava", "cherry"]

for fruit in fruits[1:-1]: 
    print(fruit)

banana
kiwi
guava

The code was told where to start (at index 1, which is the second value since Python starts with 0), and where to end (at index -1, which is the second-to-last spot because Python can go backwards).


Dictionaries store key-value pairs:

student = {
    "name": "Jose Soandso",
    "age": 20,
    "major": "Math"
}

## see the dictionary
print(student)

## pull a specific value
student["age"]

## print in loop
for key, value in student.items():
    print(f"{key}: {value}") #f-string formatting


Functions


How to write a simple Python program inside a text file, then run it on the Windows terminal

These instructions use Notepad++ because that is how I learned Python. If you are using Spyder or RStudio instead, you can skip most of this and run the program directly on that software.
  1. Make a new folder at C:\Python 
  2. Open the program Notepad++
  3. Type the following and pay attention to parentheses, spaces, and quotation marks:
    print("Hello! Good morning!")
  4. Save into C:\Python with the following file name: hello.py
  5. Keep Notepad++ open. We will use it later.
  6. Open the "Run" Windows application by either searching for it or simply using [WindowsKey]+R. The Windows key looks like four squiggly boxes arranged as a square. It is usually two keys left of the spacebar.
  7. In the Run box, Open: cmd, then click OK. This opens a black command box which we will be using to run our Python script. 
  8. To navigate to the folder where our Python script is stored, type the following and press [Enter]:
    cd C:\Python
  9. That changed the directory which the command window is using to access and write files (cd = change directory). It is a very useful MS-DOS command.
  10. Type the following and press [Enter]:
    python hello.py
  11. That command runs our simple Python script. You should see a message that says:
    Hello! Good morning!
  12. Congratulations, you have run your first Python program!
  13. Go back to Notepad++ and change the message in between the quotation marks. Save.
  14. Go back to the cmd window and re-run your program by either re-typing python hello.py or using the up arrow key to search the cmd window memory for what you typed last. Press [Enter].
  15. Congratulations! You have now edited a program in Python!

Know the differences: Python 2 vs Python 3

Python went through a major update from version 2 to version 3. For Python beginners, it is VERY important to know this because it affects the syntax of code. If you are taking code from the internet to work on your project, you may need to edit it to to work with Python 2 or Python 3, otherwise you will get errors. 

The most obvious change is how to print an output.

Python 2.x
print "Hello World!"

Python 3.x
print("Hello World!")
 
The different print function syntax is usually the only thing I need to change when borrowing old code from the internet, but be aware of other differences between Python 2 vs Python 3.

Which Python version should you use? Python 3 for the life sciences. Definitely. If you are starting to learn for the first time, learn Python 3 and just remember to update the print function syntax if you incorporate old Python 2 code into your project. 

If you use Linux: check your Python versions by running these lines on the terminal:
python --version 
python3 --version 
 


How to make a Windows shortcut to run a Python script (easiest way) - optional!

Suppose you don't want to save your program to C:\Python and instead want to save your program to C:\Users\YourName\Dropbox\coding\python\test1\blah\blah\ or some other long path.

You can still run your program using the command window and the following commands:

cd C:\Users\YourName\Dropbox\coding\python\test1\blah\blah
python hello.py

But that's a lot to type. There is an easier way!

  1. Open Notepad (or Notepad++ or any simple text editor). Type this:

    cd C:\Users\YourName\Dropbox\coding\python\test1\blah\blah
    python hello.py
    pause


  2. Save that text file as run_hello.bat (or any name you want, just replace the .txt extension with .bat to create a batch file). It doesn't matter where you save this file. You can save it to the desktop.
  3. Double-click run_hello.bat and you will see a Windows command window open and run those three lines of code in your batch file. Windows will navigate to the directory you selected, run your Python program, and then leave the window open (with pause) so you can see any print output.
  4. Note: if you make the batch file in the same folder as the Python script, you can skip the change directory (cd) line entirely. You only need two lines in run_hello.bat:

    python hello.py
    pause

Install Python on Windows Subsystem for Linux (Ubuntu Distribution) - optional

Some bioinformatics Python tools are not available for Windows (e.g. bioconda for GWAS analysis). However, starting with Windows 10, they are still accessible if you set up the Windows Subsystem for Linux (WSL). This is not the same as dual boot. WSL allows you to run Linux within Windows. I recommend the Ubuntu version of Linux. 

To install Python on the WSL Ubuntu distribution, follow the Anaconda installation instructions for Debian. Ubuntu is a version of Debian.

Install prerequisites by typing this into the Ubuntu terminal:

sudo apt-get update
sudo apt-get install libgl1-mesa-glx libegl1-mesa
sudo apt-get install libxrandr2 libxrandr2 libxss1 
sudo apt-get install libxcursor1 libxcomposite1 libasound2 
sudo apt-get install libxi6 libxtst6


Download the Linux Anaconda installer (x86 probably) and save it wherever you're going to save all your Ubuntu files. For me, that's Dropbox.

Navigate to where the file is saved and run the installer. Note that in the WSL environment, /mnt/c is the C:/ drive. Example of how to navigate to a Dropbox folder:
cd /mnt/c/Users/YourName/Dropbox

Check the sha256sum for your download by typing this (or the equivalent for a different version):
sha256sum Anaconda3-2020.02-Linux-x86_64.sh

Result:
2b9f088b2022edb474915d9f69a803d6449d5fdb4c303041f60ac4aefcc208bb  Anaconda3-2020.02-Linux-x86_64.sh


If the sha256 matches (it does for me), run the installation file using the Ubuntu terminal:
bash Anaconda3-2020.02-Linux-x86_64.sh
Click [ENTER] past the user agreement. Accept the default installation location.

Activate Anaconda on the Ubuntu terminal: 
source anaconda3/bin/activate
conda info #information about anaconda

Close and re-open the Ubuntu terminal.

If needed, install bioconda on the WSL by typing this into the Ubuntu terminal:
conda config --add channels defaults
conda config --add channels bioconda
conda config --add channels conda-forge


How to do basic stuff on the Python console

  • Work directory
    • import os
    • print(os.getcwd()) #get current work directory
    • os.chdir('/Documents/User/example') #change directory
    • os.listdir() #list files in the current directory
  • Find out where Python is installed so you can set the Python interpreter for Spyder (if you're having issues installing packages for Spyder)
    • Windows console: py --sys



--------------------------------------------------------------

Updated 2/13/2026 to update installation links in the first section.

Setting up Ubuntu 24.04.x LTS desktop and server, RStudio server, and JypyterLab at home

Why?  Set up a Linux server computer on a home network if: You want to run code that takes a long time to complete. Let it run on the server...