You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: IntroToPython/inst/extdata/presRaw/Session1.Rmd
+50-24Lines changed: 50 additions & 24 deletions
Original file line number
Diff line number
Diff line change
@@ -91,11 +91,13 @@ Guido van Rossum began working on Python in the late 1980s as a successor to the
91
91
92
92
One of the most noticeable differences comes from the emphasis on code readability:
93
93
94
-
it uses significant indentation
94
+
<divstyle="text-indent: 2em;">
95
+
it uses significant indentation.
96
+
</div>
95
97
96
98
Python 3.0, released in 2008, was a major revision not completely backward-compatible with earlier versions. Python 2.7 and older is officially unsupported but some tools require it.
As with many languages you can work with python in two main ways:
248
-
interactive or scripts.
249
-
250
-
When people think about coding they are often thinking about the interactive **console**. This is what we saw earlier when we first opened python. When you work in this way lines of code are submitted as you enter them. You often do this when you are developing an analysis and trying out parameters. This is mostly how we will work in the training.
251
246
252
-
When you want to automate something, i.e. an analysis workflow, you will write a script. You can then run this script with python and it will run every line of code for you sequentially.
253
247
254
248
---
255
249
## Python and IDEs
@@ -318,6 +312,31 @@ Once the Terminal is open, you can then just open Python, by typing `python` int
318
312
319
313
There is is an easier shortcut to do this. Once you start developing code the easiest way to open it is to use the `Shift + Enter` shortcut.
320
314
315
+
---
316
+
## Why VS code and IDEs
317
+
318
+
As VS code is an IDE it allows us to access and run python. But also do several other things from the same portal i.e.
319
+
320
+
- Explore files
321
+
- Open/Write scripts
322
+
- Shortcuts for common python utilities
323
+
- Have a live plotting window
324
+
- GitHub integration
325
+
- Advanced coding features for debugging and LLM support
326
+
327
+
We won't full dive into everything but getting familiar early is best.
328
+
329
+
---
330
+
## Interactive or scripts?
331
+
332
+
As with many languages you can work with python in two main ways:
333
+
interactive or scripts.
334
+
335
+
When people think about coding they are often thinking about the interactive **console**. This is what we saw earlier when we first opened python. When you work in this way lines of code are submitted as you enter them. You often do this when you are developing an analysis and trying out parameters. This is mostly how we will work in the training.
336
+
337
+
When you want to automate something, i.e. an analysis workflow, you will write a script. You can then run this script with python and it will run every line of code for you sequentially.
338
+
339
+
321
340
---
322
341
## Quick Recap
323
342
@@ -523,6 +542,22 @@ except Exception as e:
523
542
524
543
```
525
544
545
+
---
546
+
## A quick aside
547
+
548
+
You will run into errors coding.
549
+
550
+
DON'T PANIC.
551
+
552
+
Most of the time the error messages are very clear. And if they are not a quick google will often clear it up.
553
+
554
+
In this case we can break it down:
555
+
556
+
* ValueError - The content is invalid for the operation. The first statement is the overarching name for the error.
557
+
* invalid literal for int() - Specifically the int() function is expecting something different
558
+
* with base 10 - It is expecting base 10 numbers/integers
559
+
* 'Hello!' - This is what you gave it. We can see it doesn't match the criteria above.
560
+
526
561
---
527
562
## Concatenation
528
563
@@ -837,14 +872,7 @@ except Exception as e:
837
872
838
873
```
839
874
840
-
---
841
-
## A quick aside
842
-
843
-
You will run into errors coding.
844
875
845
-
DON'T PANIC.
846
-
847
-
Most of the time the error messages are very clear. And if they are not a quick google will often clear it up.
848
876
849
877
---
850
878
## Tuple/List coercion
@@ -1228,7 +1256,7 @@ arr
1228
1256
---
1229
1257
## Adding Dimensions
1230
1258
1231
-
It is possible to create many kinds of arrays, with differing dimensionality. They can be 1D,2D,3D etc. Typically we think about 2D arrays as this is often the rectangular data we deal with.
1259
+
Typically we think about 2D arrays as this is often the rectangular data we deal with. It is possible to create many kinds of arrays, with differing dimensionality. They can be 1D,2D,3D etc.
1232
1260
1233
1261
Here we again create a list to coerce into a array. This time we have a list of lists. Each list will become equivalent to a row in our array.
1234
1262
@@ -1265,7 +1293,7 @@ arr_2d.reshape(9,1)
1265
1293
---
1266
1294
## Indexing Arrays
1267
1295
1268
-
We can use same square brackets we used for other data objects to index our arrays. The big difference is we now have 2 dimensions. We therefore need to provide 2 indexes, separated by a comma.
1296
+
We can use the same square brackets we used for other data objects to index our arrays. The big difference is we now have 2 dimensions. We therefore need to provide 2 indexes, separated by a comma.
1269
1297
1270
1298
The first number will correspond to row. The second number will correspond to column.
1271
1299
@@ -1289,9 +1317,6 @@ arr_2d[0,1:3]
1289
1317
1290
1318
```
1291
1319
1292
-
1293
-
1294
-
1295
1320
---
1296
1321
## Logical Indexing
1297
1322
@@ -1711,7 +1736,7 @@ There are several ways to control how your code is evaluated. There are two main
1711
1736
1712
1737
Conditional branching is the evaluation of a logical to determine whether a chunk of code is executed.
1713
1738
1714
-
In Python, we use the if statement with the logical to be evaluated immediately after. The dependent code is indicated by a colon, new line and indentation.
1739
+
In Python, we use the if statement with the logical to be evaluated immediately after. The dependent code is indicated by a **colon**, **new line** and **indentation**.
1715
1740
1716
1741
```{python}
1717
1742
@@ -1742,6 +1767,7 @@ if x > y:
1742
1767
print("The value of x is",x,"which is greater than", y)
1743
1768
1744
1769
```
1770
+
1745
1771
The message is printed above because x is greater than y.
1746
1772
1747
1773
@@ -1874,7 +1900,7 @@ To help us write complex code we often use pseudocode as a starting point.
1874
1900
---
1875
1901
## Pseudocode
1876
1902
1877
-
When we write pseudocode we are trying to write out each computational step in a human readaBle way.
1903
+
When we write pseudocode we are trying to write out each computational step in a human readable way.
1878
1904
1879
1905
It is important to be specific, simple, concise and include the control structures that would be in your final code.
Given that we started at: */Users/mattpaul*, we could have also used. This uses the knowledge that we are in that start position to find where we are going.
206
+
This absolute pathway is very precise but it contains specific information about my computer. This means this will only work on this computer.
207
+
208
+
Given that we started at: */Users/mattpaul*, we could have also used a relative path. This uses the knowledge that we are in that start position to find where we are going.
Though a lot of the time we may use these NumPy approaches, there are specific functions dedicated to a variety of data types.
339
345
340
-
The most common is [pandas](https://pandas.pydata.org/docs/getting_started/index.html#getting-started). This is a specialized library for managing data frames. It also has the ability to read/write from excel spreadsheets.
346
+
The most common is [pandas](https://pandas.pydata.org/docs/getting_started/index.html#getting-started). This is a specialized library for managing data frames. These are similar to arrays but a little more flexible for multiple data types. It also has the ability to read/write from excel spreadsheets.
341
347
342
348
[BioPython](https://biopython.org/) and [BioNumPy](https://bionumpy.github.io/bionumpy/menu.html) have a range of utilities for managing biological data types i.e. fasta, fastq, etc.
343
349
@@ -367,7 +373,7 @@ if(params$isSlides == "yes"){
367
373
368
374
## Scripts
369
375
370
-
So far we have been working interactively with the console: asking it questions and getting answers back immediately.
376
+
So far we have predominately been working interactively with the console: asking it questions and getting answers back immediately.
371
377
372
378
When you want to run all your code, or if you want to start working on automation you can run a whole script instead. In this case we have all our code written out in a *.py document. This is good practice for matured analysis that you have finalized to ensure that you have everything properly documented.
373
379
@@ -426,7 +432,7 @@ More traditionally we would invoke python directly and provide the script. You w
ing/Intro_To_Python/r_course/data/susbet_dataset_args.py Male
472
+
/Users/mattpaul/Desktop/miniconda3/envs/intro_to_python/bin/python /Users/mattpaul/Documents/RU/Training/Intro_To_Python/r_course/data/subset_dataset_args.py Male
0 commit comments