Lesson 2 Getting Started#

This lesson is modified from A taste of Python from geo-python.

Objectives#

By the end of this lesson, you will be able to

  • Use jupyter notebook

  • Define and update python variables

  • Use and combine functions in python

  • Import a python module or library

  • Apply these concepts to write simple python code

1. A glimpse of Python#

1.1 Getting started with Jupyter Notebook#

This is a Jupyter Notebook. The contents are divided into cells, which can contain Markdown-formatted text, Python code, or raw text. You can execute a snippet of code in a cell by pressing Shift + Enter. Try to insert a cell and execute a code.

1.2 Math operations#

We will start our Python lesson by learning a bit of the basic operations you can perform using Python.

Python can be used as a simple calculator. Remember, you can press Shift + Enter to execute the code in the cells below. Try it out by typing 1 + 1 or 5 * 7 and see what you get.

1+1
2

If you want to edit and re-run some code, simply make changes to the cell and press Shift + Enter to execute the modified code.

The list of basic arithmetic operations that can be done by default in Python is in the table below.

Operation

Symbol

Example syntax

Returned value

Addition

+

2 + 2

4

Subtraction

-

4 - 2

2

Multiplication

*

2 * 3

6

Division

/

4 / 2

2

Exponentiation

**

2**3

8

Try to do the following calcluation $\(9 - \frac{5^3}{5 \times 25}\)$ Ans: 8

9-5**3/(5*25)
8.0

For anything more advanced such as sin(30°), we need to use some functions in a module or library.

1.3.1 Functions#

You can use Python for more advanced math by using a function.

Functions are pieces of code that perform a single action such as printing information to the screen (e.g., the print() function). Functions exist for a huge number of operations in Python.

For example, try out the print() function to display Hi on screen

print('Hi')
Hi

Another example, try min() or max() function to return the minimum or maximum of this list [4, 1, 8]

A= [4, 1 , 8]

print(min(A))
print(max(A))
1
8

Let’s try out a few simple examples using functions to find the sine or square root of a value.
You can type sin(3) or sqrt(4) into the cells below to test this out.

sin(3)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[5], line 1
----> 1 sin(3)

NameError: name 'sin' is not defined

Wait, what? Python can’t calculate square roots or do basic trigonometry? Of course it can, but we need one more step.

For a specialized sin(30°), we need to load a specialized module or library.

1.3.2 Modules and libraries#

Alibrary is a collection of modules that can include a variety of related functions.
A module is a file or files(.py) that is imported by one import and used to perform a related block of tasks.
A function is a reusable piece of code that performs a single action. .

For example, to evaluate sin(90°), we need to load a specialized module or library.

For math operations, one module is called math and it can be loaded by typing import math. Try that below.

import math

Now that we have access to functions in the math module, we can use it by typing the module name, a period (dot), and the the name of the function we want to use. For example, try to find out the square root of 4 using math.sqrt(4)

math.sqrt(4)
2.0

Try another example. the function math.sin(x) returns the sine of x radians.

Now find sin(1.5)

math.sin(1.5)
0.9974949866040544

Note that modules may also contain constants such as math.pi.
Type this in the cell below to see the value of the contant math.pi.

math.pi
3.141592653589793

Check your understanding#

Use the empty Python cell below to calculate the sine of pi `sin(π)

math.sin(math.pi)
1.2246467991473532e-16

Now, how to find sin(90°) ?

You need to not only the sin() function, but also a function that converts from degrees to radians.

Check the documentation of the math module to find the function the converts from degrees to radians.

math.sin(math.radians(90))
1.0

1.3.3 Combining functions#

Functions can also be combined. You have done this already above.

The print() function displays values within the parentheses as text on the screen. Below, try printing the value of the square root of four.

print(math.sqrt(4))
2.0

You can also combine text with other calculated values using the print() function.

For example, print('Two plus two is', 2+2)

print('2 + 2 = ' , 2+2)
2 + 2 =  4

2. Jupter Lab and Jupter NoteBook#

Python for Scientific Computing provides a quick introduction to Jupyter.

Check the JupyterLab User Guide for more details

3. Markdown#