forked from fredhutchio/python_intro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweek1.py
203 lines (156 loc) · 6.39 KB
/
week1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/usr/bin/env python3
#### Intro to Python and Jupyter Notebooks ####
#### Before class ####
# share URL to hack.md
# check installation of python and jupyter notebooks
#### Welcome ####
# instructor introduction
# overview of fredhutch.io
# sign in
# learner introductions and motivation
# overview course philosophy, how to stay engaged
# course objectives: overview of basic functionality of python (syntax, data manipulation, visualization)
#### Objectives ####
# Today:
# Python and jupyter notebooks
# operators, functions, and data types
# sequences and dictionaries
# defining functions
#### Orientation to Python and projects ####
# ask about previous experience with Python or other programming languages
# overview course philosophy, how to stay engaged
# motivation for Python: programming language, reproducibility, open source
# many ways to interact with Python
# python in terminal
# ipython in terminal
# save script in text editor
# IDE like spyder
# notebook: web application that combines code, graphs, and text
# interactive mode in terminal, chevrons (>>>) is prompt, waiting for input
# scripting mode: save commands in file (ends in .py), execute entire file at once
# about our tools
# Anaconda: distribution (way of obtaining) Python;
# includes extra packages like ipython, spyder
# conda: package manager that comes with Anaconda, installs/updates packages
# jupyter notebook: installed with Anaconda
# setting up a project directory
# create new directory called python_project
# keep data, analyses, and text in single folder (directory)
# scripts and text files in folder relate to relative places in the directory
# suggested directories to go with project:
# data/ which may include separate directories for raw and processed data
# documents/ for outlines, drafts, and other text
# scripts/ for scripts related to data cleaning, analysis, plotting
# setting up Notebooks
# open Terminal (Mac) or Command Prompt (Windows)
# execute `jupyter notebook`
# terminal window must stay open, this is kernel (running python)
# web browser is how you interact with notebook
# navigate to project directory
# click "New" in upper right hand, then select "Python3"
# creates notebook (*.ipynb, or ipython notebook file)
# autosaves, or can save manually
# click on title to rename
# executing code in a jupyter notebook:
# enter code in cell and execute by pressing Shift + Return/enter
# output is printed directly below cell, prefaced by Out[ ]:
# add new cell with + button
# can add Markdown cells with nicely formatted text
# comments prefaced with # (not read/executed by python)
# commands and output saved in notebook
# talk about other menu options and buttons to remove/add/run cells
# example notebook: https://github.com/rasilab/machkovech_2018/blob/master/scripts/NA43_competition.ipynb
#### Operators, functions, and data types ####
# operators (mathematical calculations)
4 + 5
4+5 # spaces are optional, but easier to read
3 > 4 # comparisons, logic
# built-in data types: strings, integers, floats
number = 42
pi_value = 3.1415
text = "Fred Hutch"
# find type of each using function (type)
type(number) # integer
type(pi_value) # float
type(text) # string: characters (letters, numbers, punctuation, emoji)
# convert float to integer
int(pi_value) # decimals removed
type(pi_value) # type hasn't changed!
pi_value = int(pi_value) # convert original object
type(pi_value)
# convert integer to float
float(number) # decimals added
# see value of something (required to display output in a script)
print(text)
# demo example.py in separate window
# print and type are built in functions; there can also be methods (subset of functions) and user-defined functions
# find help on a function
help(print)
#### Sequences ####
# lists: data structure that holds sequence of elements
# surrounded by square brackets
numbers = [1, 2, 3]
# reference one part of a list
numbers[0] # indexing starts at 0
# find help on an object (can also check under help menu)
?numbers # can also use help(numbers), but may not be useful to you right now
# add number to end of list
numbers.append(4) # append() is an attribute
print(numbers)
# can use tab complete in notebook to see other options
?numbers.append # find help for attribute
# lists can be string data as well
organs = ["lung", "breast", "prostate"]
## Challenge: what google search could you use to determine a method for adding multiple values to a list?
## Challenge: how do you remove items from a list?
# tuple: list with ordered sequence of elements; cannot be modified
# surrounded by parentheses
a_tuple = (1, 2, 3)
## Challenge:
# What happens when you execute:
#numbers[1] = 5
#a_tuple[2] = 5
# Traceback is a multi-line error block printed
# includes information on what error and where in code
# comment out code error if you want to keep it for notes
# sequences can include more than just one data type
mix_tuple = ("lung", 200, "chromosome 1") # can apply to lists, but they're more often one data type
# for loop to access elements in list, tuple, or other data structure one at a time
for num in mix_tuple:
print(num)
#### Dictionaries ####
# dictionary: container holding a pair of objects, key and value
translation = {"one": 1, "two": 2}
translation["one"]
# can include lists as values
list_value = {"yes": [1, 2, 3]}
# cannot include lists as keys
#list_key = {[1, 2, 3]: "nope"}
# add items to dictionaries by assigning new value to key
rev = {1: "one", 2: "two"} # different data types can be key, value
rev[3] = "three"
rev
# access each element by key
for key in rev.keys():
print(key, "->", rev[key])
# access each element one at a time with item
for key, value in rev.items():
print(key, "->", value)
## Challenge:
# print only the values of the rev dictionary to the screen
# Reassign the second value (in the key value pair) so that it no longer reads “two” but instead “apple-sauce”
# Print the values of rev to the screen again to see if the value has changed
#### Functions ####
# define a chunk of code as function
def add_function(a, b):
result = a + b
return result
z = add_function(20, 22)
print(z)
## Challenge: define a new function called subtract_function that subtracts d from c and test on numbers of your choice
#### Wrapping up ####
# make sure work is saved
# review how to get back into notebook
# review objectives
# preview next week's objectives
# remind to sign in