Archive
Archive
Contents
1 Introduction 1
2 Agenda 1
3 UNIX Shell - Composition 3
4 Python Fundamentals 4
5 Coee Break 8
6 Conclusions 8
1 Introduction
2 Agenda
2.1 Learning Objectives
1
Figure 1: Algorithms are magic by Vishakh Kumar under Public domain; from
makeameme.org
Good afternoon everybody and welcome to this workshop! Our goal in the next two hours
is to teach you a few basic computing skills that will make you a more eective researcher or
student. It's not to turn you into a professional programmer, it's to show you a handful of
things that will help you get more stu done in less time.
When you get to the real gnarly stu in programming, it might be time to go bug your
friends in the CS department. Especially when it comes to algorithms. But you should be
able to do a few things after this.
Don't worry
We'll be using nano as our editor. Feel free to use vi or the editor of your choice.
Git is a version-control system that lets you track what changes you've made. Helps
you share code with friends while also being a great undo button. Install by clicking
the appropriate link - Windows, macOS, Linux
Download and install Anaconda from the appropriate link - Windows, macOS,
Linux
2
Use all of the defaults for installation except make sure to check Make Anaconda
the default Python.
If you need help, I have two wonderful people who can help you out. Be nice to them!
They're doing this selessly.
2.3.2 Git
Git is a version-control system that lets you track what changes you've made
and lets you update a shared version of your code. For now, think of it as a
diary in which you write down what you've done and why, so that future you can
undo stu easily. Install by clicking the appropriate link - Windows, macOS,
Linux
2.3.3 Python
Python is a easy-to-read programming language that just so happens to be
popular in the sciences. We'll be using Anaconda to install the latest version
and a bunch of useful libraries for us. We'll also need Jupyter, a programming
environment that runs in your web browser
3
3.1.2 Ugh, this sucks
#+BEGIN NOTES I suppose it is possible to rant about how bash is a Terrible
Programming Language that I really don't want to use and that dierent shells
out there are so much better/cooler/less insane/etc, but it doesn't bother me
much and it really shouldn't bother you too much either. Try not to write too
many complicated bash scripts and don't worry about.
4 Python Fundamentals
4.1 Goals
You'll automate those functions to perform tasks over and over again
(in various combinations)
You'll learn some le input/output to make the computer read and
write useful information
Hey everyone! We're all gathered here to dive into the exciting world of programming.
Think of it as a superpower that lets you tackle your research problems by getting a computer
to do the heavy lifting both quickly and accurately. Imagine creating your own mini-programs,
or functions, that carry out specic tasks you dene. You'll even learn how to make these
functions work like clockwork, automatically handling tasks repeatedly and in dierent com-
binations.
4.2 Jupyter
To run Python, we are going to use Jupyter Notebooks via JupyterLab for the remainder
of this workshop. Jupyter notebooks are common in data science and visualization and serve
as a convenient common-denominator experience for running Python code interactively where
we can easily view and share the results of our Python code.
While there are alternative methods like IDEs (PyCharm, Visual Studio Code) or text
editors (Vim, Emacs) for writing and running Python code, Jupyter notebooks oer instant
execution and result viewing within the same interface.
JupyterLab enhances the experience with features like easy code typing/editing, tab com-
pletion for better access to object names, rich text annotations for better readability, and
the ability to display gures alongside the code for comprehensive analysis storytelling. Each
notebook includes cells for code, text, or images.
jupyter lab
4
For those of you who have used Jupyter in the past, you might be used to typing jupyter
notebook. This is eectively the same thing. It's a nicer, cleaner interface that works without
bugs. The old notebooks still work, but they're being phased out.
File: This menu contains le and directory actions like New, Open, Close,
Save, etc., and includes the Shut Down option for the JupyterLab server.
Kernel: Manages kernels, which are crucial for Jupyter and will be ex-
plained later.
You've all seen menu bars so let's just point out the few that matter
Code
Any Python interpreter can be used as a calculator on the y. Change the
values here and try it out.
import math
print(3 + (5*4) + math.cos(3))
This is great but not very interesting. To do anything useful with data, we need to assign
its value to a variable. In Python, we can assign a value to a variable, using the equals sign
=. For example, we can track the weight of a patient who weighs 60 kilograms by assigning
the value 60 to a variable weightkg :
4.4 Variables
Variables are like named boxes that store data. When we refer to the variable,
we're really talking about what's in the box.
name = "Vishakh"
temperature = 100
5
4.4.1 Exercise 01
What are the values in `mass` and `temp` after the following code is executed?
4.5 Functions
y = f (x)
f () is a function
x is an input (or input*s*)
y is the returned value, or output(s)
Not all functions in code take an input, or produce a usable output, but the principle is
generally the same.
4.6 Conditionals
num = 37
if num > 100:
print('greater')
print('done')
6
4.6.2 Conditionals in Python - else
We can also add an else statement.
num = 37
if num > 100:
print('greater')
else:
print('not greater')
print('done')
num = -3
if num > 0:
print(num, "is positive")
elif num == 0:
print(num, "is zero")
else:
print(num, "is negative")
This is structure similarly to the if statement in that if the condition is true (and no
previously tested conditions have been true) then the indented code block is executed.
We often want to loop over a list of elements and make a decision on the basis
of whether the element meets some condition.
7
One of Python's most distinctive features is the list comprehension, which you can use to
create powerful functionality within a single line of code.
newlist = [expression for variable in iterable]
List comprehensions start and end with opening and closing square brackets, []. Then
comes the expression or operation you'd like to perform and carry out on each value inside
the current iterable. The results of these calculations enter the new list. The expression is
followed by a for clause. THIS IS WHAT DISTINGUISHES IT FROM A LIST variable is
each item in the current list that is going through the iteration. The in keyword is used to
loop over the iterable. From the iteration that was performed and the calculations that took
place on each item during the iteration, new values were created which are saved to a variable,
in this case newlist . The old list (or other object) will remain unchanged. There can be an
optional if statement and additional for clause.
4.7.1 Exercise
new_list = [num for num in [20,21,23,24,25] if num > 20 and num % 2 == 0]
print(new_list)
5 Coee Break
6 Conclusions
Bibliography
License Information
No warranties are given. The license may not give you all of the
permissions necessary for your intended use.
In particular, trademark rights are not licensed under this license. Thus,
rights concerning third party logos (e.g., on the title slide) and other (trade-)
marks (e.g., Creative Commons itself ) remain with their respective holders.