# Getting started with python and C++

In this course you will be writing your own code to solve many interesting physics problems with your computer. We will primarily be using python and C++ for most of your assignments.

• Python, an interpreted language, is slow, but is easy-to-use and useful for rapid prototyping of simple programs and data analysis. For some of the work in this course, python will be the right choice.
• However, there are circumstances when python is simply too inefficient, in which case you should probably use C++. C++, a compiled language, is very fast, but has a steeper learning curve than python. Many efficient scientific codes, especially those requiring parallelization, are written in C++.

# Installation

You will need to install python and C++ on your machine (or find someplace you can ssh into to run them). Try to do this now.

## Python

A convenient package of python libraries, which includes important modules like numpy, scipy, and matplotlib, is Anaconda. We recommend you install Anaconda on your machine, so that you have all these necessary packages. Make sure that you get the right version for your machine. Check if your machine is 32-bit or 64-bit and use the corresponding installer. Please use python 3.xx Python has two version (2.7 and 3.6). For what we are doing it probably doesn't matter much which version you use but, all other things being equal, you should probably use the newer version (3.6) as this will be most useful to learn.

#### Linux

To install Anaconda for Linux, follow the instructions listed here.

#### Mac

To install Anaconda for Mac, follow the instructions listed here.

#### Windows

To install Anaconda for Windows, follow the instructions listed here.

## C++

To turn your C++ code into an executable program, you need a C++ compiler.

#### Linux

All Linux distributions should have the standard C++ compiler, the GNU C++ compiler called gcc or g++, already installed. So no extra work for you! To check if it really is installed, type into your terminal

g++ --version

which should show you if you have c++ and what version it is. You probably want to be around version 6.0

#### Mac

You essentially have two options. One option is to use the compiler already on your machine. Again type

g++ --version

and make sure it's there. Another option is to install macports (where you can install more recent compilers, etc.) See here if you want to do that (this is typically what I do on my macs). (The command to run once you have macports installed is sudo port install gcc8.

#### Windows

There are various options. One possibility (if you have the right version of windows) is to install ubuntu on windows (see here) and then use

sudo apt-get install [compiler name]

to install the compilers and git, etc. Another possibility is to use the free version of visual studio.

#### SSH into the engineering workstations.

If you don't want to (or have trouble) installing things locally, one option is to "SSH" (secure shell) into the University-provided Linux engineering workstations. These are Linux machines that come with gcc, as well as python, already installed. To access these resources, you need to be able to ssh into netid@linux.ews.illinois.edu, where you replace netid with your specific University net ID.

Another standard SSH program (for Windows) is PuTTY. You can download it here. It is pretty straightforward to use, but just in case some installation and usage instructions can be found here. When you login to netid@linux.ews.illinois.edu, you will be prompted to accept an SSH key and then you will need to enter a password. That password is your current password associated with your Illinois net ID.

# Editors

There are various editors that you can use. These include (but are not limited to)

• emacs (free)

• Visual Studio Code (free and here )

• atom (free and here )

• Sublime (?free for preview? and here )

## Python

Working in python often consists of writing simple "scripts", such as script.py. To execute this script, you simply type in

python script.py

and python does its magic.

## C++

Working in C++ is typically more involved and requires writing a collection of "header" (".h") and "source" (".cpp" or ".cc") files. Say you wrote the files main.cpp, helper.cpp, importantheader.h, otherheader.h as part of your assignment. The source files reference the header files by the #include syntax. To compile your code, type

g++ -O3 -std=c++11 file1.cpp file2.cpp -o myExecutable

This creates an executable titled myExecutable, which you can then run in Linux with the command ./myExecutable.

# Example code

At these two links, C++ example and python example, there are two examples of a simple code that computes the Fibonacci numbers and prints them to the command line. There is a version written in C++ called printFibonacci.cc and a python version called printFibonacci.py. To compile the C++ version, you need to execute

g++ -O3 -std=c++11 printFibonacci.cc -o printFibonacci

Then you can run the newly created printFibonacci executable by typing the command

./printFibonacci 20

which should then print the first 20 Fibonacci numbers.

The python script you can run by typing the command

python printFibonacci.py 20

which should produce exactly the same output. Make sure you can get these two examples working.

# Code references

The above information was intentionally very minimal on details. We will not spend much time on introducing you to the syntax of python, C++, or any other language you might want to use. We hope that you will look it up as you need to accomplish the projects. To help with that, we provide here a few python and C++ "cheat sheets" and tutorials that you might find useful. Such documents are often very handy and can help you remember specific commands that might be useful for the task at hand.

## Python

• A scientific python cheat sheet on basic python, ipython, numpy, scipy, and matplotlib.
• The official, long, and really comprehensive python tutorial.
• The official quick start to numpy and scipy.
• Matplotlib's official pyplot tutorial.

## C++

• A C++ cheat sheet.
• A comprehensive C++ tutorial. The first few sections are very much worth going over if you are not familiar with the language.
• Another lengthy C++ tutorial website.

## General

• Google is your friend. Don't be shy to search for specific coding questions.

## Good software practice

Throughout this course you will learn various techniques for good software practice and software techniques. The first thing to learn is how to do good version control. If you mess up something on your code, you want to be able to go back and see what you've done. To do that you need to keep regular snapshots. The best way to go about this is using git. Please get it on your computer and go through the 15 min. tutorial. You should regularly use it and need to use it to document your checkpoints.

One time Commands:

• git init (starts your repository)