print('hello world')
hello world
Students will gain an introduction to programming in python, working in interactive notebooks, and learning how to leverage outside resources for coding help. Moreoever students will gain a basic understanding of variables, data types, and working with simple expressions for comparison and computation.
Python is a general purpose programming language that is widely used in scientific computing, image analysis, machine learning etc. It allows you to specify a set of instructions, written as a script or a program, to execute some task of interest.
The purpose of this series of python workshops is not to give you an extensive in depth overview of everything you can do in python. However, we do aim to give you all the skills and terminology necessary to learn how to learn to code in python. Google, stack overflow, and github issues) are invaluable tools you can use to your advantage for (1) gaining coding help and (2) learning how to write code from reading code.
We will be using the online server Google Collab for learning python. This server creates an isolated “environment” for you to work in. Using your Google account (or you will have to create one) open up Google Collab in your web browser. Start a “new notebook” to create a new python file.
There are many types of files that you can use for Python coding. The most important for you now are : * .py : This is a python script file. All things in this file have to be python code. You would use this file for a complete analysis. * .ipynb : This is a Jupyter Notebook file. Jupyter is a user interface to seemlessly code in python. This worksheet was made in a Jupyter Notebook!
“The Jupyter Notebook is an incredibly powerful tool for interactively developing and presenting data science projects. A notebook integrates code and its output into a single document that combines visualizations, narrative text, mathematical equations, and other rich media. The intuitive workflow promotes iterative and rapid development, making notebooks an increasingly popular choice at the heart of contemporary data science, analysis, and increasingly science at large.” - dataquest
All of our lessons will be presented in Jupyter notebooks due to their interactive nature (.ipynb file extension). They consist of two main attributes, a kernel
and cells
. * A kernel
interprets and executes the code. Here we are using the kernel for python; however, you can specify a kernel for another language like R. * A cell
is a container for either text or code to be executed.
To run the python code in a cell, you just hit shift + enter. Try it with the code below.
Similar to an author writing, a programmer will adopt a style of coding. This can be as small as how many new lines you have between code lines, to the way you comment your code, and how you name your variables. Matching a standard style of code will help make your code more readable and more reproducible. If you continue your coding journey, we recommend the PEPS Style Guide for Python. Go ahead and look through this resource, but focus on the Commenting, Code Lay-out, and Naming Convention sections.
One of the most important things you can do to learn and to help other programmers read your code, is using meaningfull comments. Comments will begin with a # sign within a code chunk. You can have an inline comment, to provide information for a speicifc like of code, or a block of comments to describe a section. You want these comments to be concise but informative.
At the begining of every file, use a block of comments to outline the python file youre making. This will also be good practice for documentation for any public code you publish… Like this:
Examples of input would be the datasets you need, other files that are used in this script, ect. You also may have figures as outputs, or processed data. List all of these at the top of your code, so that you can find the information quickly when you return. If youre using a notebook file, like you will be in this class, feel free to add this in a “Markdown” chunk instead.
Your inline comments should be added to describe an important line of code. Look at the formatting below:
When you are naming somehting in your code, like a variable here, it is important to follow a few rules: * always start the name with a letter (no numbers or special characters) * never start a name with an underscore (this is reserved for official Python variables nad functions) * avoid long variable names * use a specific naming style (we will use “snake case” which is all lowercase, and has an underscore seperating words)
# Good naming style :
apples = ['granny_smith','machintosh','golden_delicious']
granny_smith = 'green'
machintosh = 'red'
golden_delicious = 'yellow'
# Bad naming style :
list_ofApples=['gs','MACIN','_golden_']
gs= 'green'
MACIN ="Red'
_Golden='y'
Cell In[4], line 12 MACIN ="Red' ^ SyntaxError: unterminated string literal (detected at line 12)
Finally, when naming your files, find a format that works well for you, but follow these few rules : * if you start your files with a number, and you expect to have more than 10 (or more than 100) add a zero to the begining of the file name. Computers will treat the number in the name as part of the alphabet, so if you don’t do this, the computer orders them incorrectly * WRONG : 1_facs_data.fcs, 10_facs_data.fcs, 11_facs_data.fcs … 2_facs data.fcs * RIGHT : 01_facs_data.fcs, 02_facs_data.fcs, ….. 10_facs_data.fcs * RIGHT : 001_facs_data.fcs, 02_facs_data.fcs, ….. 010_facs_data.fcs … 100_facs_data.fcs