Beginner's Guide and Instructions to Programming in Python using the JupyterLab IDE

From PharmWiki
Jump to navigation Jump to search
  • This tutorial is based on the following website. No tutorial is perfect, so please feel free to email me. There are multiple ways to do things. Like anything, learning how to program takes practice. However, a big flaw in computer programming courses is that there is no practical aspect. Below is a summary in tabular format.
  1. Spyder IDE
  2. Repetitive Tasks with the IDE

Tutorial

  • This tutorial is based on this website. Each program builds on the other, so I recommend going in order. Because computer programs can escalate in size, I periodically write reviews that summarize the previous section and I try to keep the text of the summary to a single page. I made PDF versions of the programs. The best way to learn how to program is to hand type these programs and change them around. Therefore, I have made PDF versions of the programs in addition to the Python language versions.

Section #1-Comments, Print Statements, Variables and Concatenation

Section #1
# Topic Reference Command(s) Example

Output

Plots Python Program Comments
1 Comments Link # Style #1 for line comments

''' Style #2 for block comments '''[1]

JypL001.png None 001-Comments.ipynb I put this first because I put comments throughout

the programs, so that students understand.

2 Print Statements Link print("Some Text") or

print('Some Text')[2][3]

use of a backward slash \

JypL002.png None 002-HelloWorld.ipynb This is a classic beginning program Hello, World!.

You learn how to output characters in the console using

the print('output") Python command. You can put single

quotes within double quotes and vice-versa. The backward

slash (i.e., \) can be used to output invisible characters such

as tab

3 Variables Link variable types

casting: int(), str() and float()

type()

JypL003.png None 003-Variables.ipynb You learn about three different types of variables, namely string

(str), integer (int), and real numbers (float). You also learn how

to convert a variable using a process called casting.

4 Variables with

Concatenation

Similiar as previous, but will

demonstrate a computer concept

of concatenation.

JypL004.png None 004-Variables+Con.ipynb You learn how to concatenate your output. Concatenate

essentially means to combine. You can combine using a plus

(i.e., +) or a comma.

5 Short Review of

Exercises 1-4

All the Above, but shorter JypL005.png None 005-Short_Review.ipynb Reviews the above in a shortened and less wordy format.

Section #2-Making decisions with the Python language using if-then-else statements and comparisons.

Section #2
# Topic Reference Command(s) Example

Output

Plots Python Program Comments
6 Code Sections,

If-then-else-statements

Link if, elif, else JypL006.png None 006-If-then-else.ipynb In this part, you will learn how code is put into sections. You will also learn how Python language

makes decisions through if, elif and else commands.

7 Code Sections,

Boolean, equivalent (equals to)

True, False, == JypL007.png None 007-Booleans.ipynb In this part, you will learn about booleans. Basically, booleans are things like true or false, yes and

no. You will also learn about the Python equivalent of "equivalent to", which is double equals ==

8 Code Sections,

Comparison operators[4] (CO) and

Logical operators (LO)[5]

CO:

>,<,==, >=, <=

LO:

and, or not

JypL008.png None 008-Operators.ipynb This part teaches you about comparison operators (i.e, comparing two values) and logical

operators. We will also talk more about booleans.

9 Short Review of Exercises 1-4 and 5--8. JypL009.png None 009-Short_Review.ipynb This short review encompasses everything from exercises 1-4 and 6-8. The program

has concatenation, type() function, if-then-else statements, comparative operators and

logical operators.

Section #3-Arrays and Modules ⇒ Plotting with Matplotlib

  • I feel the best way to learn computer programming is to do something interesting and useful. You will need to know the above exercises 1-9 very well before you proceed. Computer programming builds on itself. If you prefer a slower introduction, I would recommend this tutorial. I have added a priority section to give you an idea of how you should prioritize learning Python. Some things you should know and others you can just be aware of it as you need it such as the Built-in Python Array Methods.
Section #3
# Topic Priority Reference More Info Command(s) Example

Output

Plot Output Python Program Comments
10 Basic Built-In Math

Functions

know +,-,/,*,**

be aware of %

+,-,*,/,%,** JypL010.png None 010-Basic_Math.ipynb With these exercises, you will learn basic math functions with Python.
11 List

(a.k.a. Array)

know it len()

indexing

JypL011.png None 011-Arrays.ipynb This exercise teaches you the basics of Python arrays. To plot data, you need to

know arrays because they are the basis of all datasets.

12 List

Methods

know the append() method.


Link append()

copy()

clear()

count()

extend()

index()

insert()

pop()

remove()

reverse()

sort()

JypL012.png None 012-Array_Methods.ipynb Set the Method2Try variable to the following programs:

1) append() method

2) copy() method

3) clear() method

4) count() method

5) extend() method

6) index() method

7) insert() method

8) pop() method

9) remove() method

10) reverse() method

11) sort() method

The 012-1-Append().py is only part of the 012-Array_Methods.py to show how to separate it from the larger program.

13 List Functions

range() data type

tuple() data type

know len() and

range. Be aware

of the rest

len()

min()

max()

list()

tuple()

range()

JypL013.png None 013-Array_Functions.ipynb Set the Function2Try variable to the following programs:

1) len(list1)- Gives the total length of the list.p>

2) max(list1)- Returns item from the list with max value.

3) min(list1)- Returns item from the list with min value.

4) list(tuple1)- Converts a tuple into a list.

5) range(start,stop,step) - Make a list of numbers with certain parameters

14 Introduction to

Modules-

NumPy

know np.array() and

be aware of

np.matrix().

Link Examples np.array()[6]

np.matrix()[6]

2D indexing

JypL014.png None 014-Introduction_to_Modules.ipynb In this exercise, you will learn what modules are and then NumPy

module functions np.array() and np.matrix() that you need for the

next section.

15 Python Plotting Module:

Matplotlib

Be aware of the

different plots available

in the Matplotlib

module.

Link Examples plt.plot()[7]plt.show()[7]

plt.xlabel()[7]

plt.ylabel()[7]

plt.title()[7]

plt.grid()[7]

JypL015.png Beg001a.png 015-Matplotlib_Module.ipynb Run the programs in each cell

1) Line Plot

2) Scatter Plot

3) Line and Point Plot

4) Dashed Line Plot

5) Line Plot with Axes Labels

6) Line Plot with Axes Labels and Title

7) Line Plot with Labels with Color

8) Line plot with Labels and Grid

Section #4-Loops and Loading Data

Section #4
# Topic Priority Reference More Info Command(s) Example

Output

Python Program Comments
16 Dictionaries, Arrays

and Loops

know it Link #1

Link #2

Link #3

Dictionary Data Type

for loops

while loops

JypL016.png 016-Dictionaries_Arrays_Loops.ipynb Run in each cell

1) This program will print out the list of names using the for loop 2) List of names from a simple dictionary using a for loop

3) List of names from a complex dictionary using a for loop

4) Using a for loop with a range of numbers

5) Learn about a new type of loop called a "while loop"

6) Same as 5 except a different style of doing an Increment.

Nested Loop (Loop within a Loop)

7) Uses a nested loop for a two-dimensional array

8) Uses a nested loop for a two-dimensional NumPy array

9) Uses a nested loop for a dictionary variable type. This has a similar output to #3

17 Reading, Parsing and Saving

Files

be aware of

it

split()

lower()

strip()

replace()

open()

JypL017.png 017-Reading_and_Parsing_Files.ipynb Run in each cell:

1) Display the file line by line using readlines() command.

2) Display a file all at once.

3) Same as 1, but use replace() function to remove hidden characters. Also, learn the strip() function that removes space at the beginning and end of the line.

4) Same as 2, but use replace function to remove hidden characters.

5) Similar to 4, learn how to split file into sentences using the split() function.

6) Similar to 5, but splits the file into words.

7) This is a more sophisticated program that makes a frequency. dictionary of text. Uses functions split(), lower(), strip() and replace().

8) Same as 7, but we are going to save the output using the csv module.

References and Notes

  1. You can block comments with three double quotes (i.e., ") or three single quotes (i.e., ').
  2. In Python 2.x versions, the command was print "Some Text" or print 'Some Text'
  3. Hello World is classically the first program that someone writes. See https://en.wikipedia.org/wiki/%22Hello,_World!%22_program
  4. Operator in simple language is something that does "operates" on something else.
  5. https://vegibit.com/logical-and-comparison-operators-in-python/
  6. 6.0 6.1 np is shorthand for numpy, when you import numpy as np.
  7. 7.0 7.1 7.2 7.3 7.4 7.5 plt is shorthand for matplotlib.pyplot