0% found this document useful (0 votes)
2 views

Archive

The document outlines a workshop on basic computing skills aimed at enhancing research efficiency through UNIX shell and Python programming. It includes setup instructions for necessary software, learning objectives, and fundamental concepts of Python programming such as variables, functions, and conditionals. The workshop emphasizes practical skills for automating tasks and data manipulation, with resources and exercises provided for hands-on learning.

Uploaded by

grokking Stuff
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Archive

The document outlines a workshop on basic computing skills aimed at enhancing research efficiency through UNIX shell and Python programming. It includes setup instructions for necessary software, learning objectives, and fundamental concepts of Python programming such as variables, functions, and conditionals. The workshop emphasizes practical skills for automating tasks and data manipulation, with resources and exercises provided for hands-on learning.

Uploaded by

grokking Stuff
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Software Carpentry

Vishakh Pradeep Kumar


<2024-01-26 Fri>

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

Our goals are to:

ˆ teach you a few skills

ˆ help you get more stu done in less time.

ˆ turn you into a professional programmer

 algorithms are beyond me

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.

2.2 Some useful tips

ˆ Don't worry

2.3 Setup Instructions

We need to install a few pieces of software in order to get working.

ˆ Terminal - Windows, (macOS & Linux have one already).

ˆ Git - Windows, macOS, Linux

ˆ Python (through Anaconda) - Windows, macOS, Linux

ˆ Terminal or shell is how you do tasks quickly without a GUI.

 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

ˆ 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 it for us.

 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.1 Text Editor


nano is a basic editor and is what we're going to be using for everything
On macOS and Linux systems, vim is often the pre-set default text editor,
sadly known more for its complexity than intuitiveness. If you inadvertently
enter vim and need to exit, press the Esc key, then type :q! (colon, followed
by a lowercase 'q' and an exclamation mark), and press Enter to go back to the
shell.

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

ˆ Download and install Anaconda from the appropriate link - Windows,


macOS, Linux

ˆ Use all of the defaults for installation except


 Make Anaconda the default Python.

 Add Anaconda to my PATH environment variable

3 UNIX Shell - Composition


redirect to le > pipe to next command |

3.1 UNIX Shell - Summary

3.1.1 Useful resources


ˆ If you have a complicated shell expression, try using this website to explain
it: https://explainshell.com/

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

ˆ We're here to learn how to program

 You'll build functions that do specic, dened tasks

 You'll automate those functions to perform tasks over and over again
(in various combinations)

ˆ This is a way to solve problems through making a computer do work


quickly and accurately

 You'll manipulate data, which is at the heart of all research/work/coursework.

 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

ˆ We'll be using Jupyter Notebooks through JupyterLab to run Python, an


interactive tool that allows for easy viewing and sharing of Python code
results.

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.

4.2.1 Starting Jupyter


At your command line, start Jupyter notebook

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.

4.2.2 Jupyter Landing Page


4.2.3 Create a new notebook
4.2.4 Menu bar
The Menu Bar in JupyterLab, located at the top, features familiar GUI com-
mands and their keyboard shortcuts.

ˆ File: This menu contains le and directory actions like New, Open, Close,
Save, etc., and includes the Shut Down option for the JupyterLab server.

ˆ View: Contains options to modify JupyterLab's visual layout.

ˆ Run: Provides tools to execute code in notebooks and code consoles.

ˆ 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

4.2.5 Left Sidebar


File browser that displays the contents of the directory from which the Jupyter-
Lab server was initiated. Helpful to see what's in your folder without needing
to open File Explorer.

4.2.6 Cell Types


ˆ Markdown

ˆ Code

4.3 Testing Python as a calculator

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?

mass, temp = 50, 40


mass = mass * 2.0
temp = temp - 20

4.4.2 Solution to Exercise 01


What are the values in `mass` and `temp` after the following code is executed?

mass, temp = 50, 40


mass = mass * 2.0
temp = temp - 20
print(mass, temp)

4.5 Functions

Functions in code work a bit like mathematical 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

We often want the computer/code to take one of a range of possible actions,


dependent on some condition.

ˆ if something is true, do action


ˆ if something is true, do action, else do task
More precisely, we want it to take a specic action if something is true, or
some other action if that thing is not true.

4.6.1 Conditionals in Python - if


To ask Python to take dierent actions depending on some condition, we can
use an if statement.

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')

4.6.3 Conditionals in Python - elif


We can chain conditions together using the elif statement.

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.

4.6.4 Conditionals in Python - and, or, not


We can combine conditionals using boolean operators

if (1 > 0) and (-1 > 0):


print('positive and negative numbers are greater than zero')
else:
print('at least one part is false')

4.6.5 Conditionals in Python - in


Lets us test if an item is in a collection

print(1 in [1, 2, 3])


print(1 in range(3))
print(1 in range(2, 10))

4.7 List Comprehensions

We often want to loop over a list of elements and make a decision on the basis
of whether the element meets some condition.

new_list = [expression for variable in iterable]

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

This document is part of an Open Educational Resource (OER) workshop on


basic unix skills and Python. Source code and source les are available on
GitLab under free licenses.
Except where otherwise noted, the work Software Carpentry, © 2024
Vishakh Pradeep Kumar, is published under the Creative Commons license
CC BY-SA 4.0.

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.

You might also like