# 1. Introduction to Jupyter¶

In this lecture we demonstrate:

- what is Jupyter, how to navigate through Jupyter notebook and how to use Jupyter to evaluate Python expressions;
- how to use variables in Jupyter and how to execute small Python programs; and
- what are libraries and how to use functions provided by standard libraries.

## 1.1. Jupyter does arithmetic¶

Jupyter is an interactive environment in which you can enter some text (just as the text you are reading right now), evaluate arithmetic expressions, run Python programs, analyze data, represent data as tables and diagrams, and much, much more.

Each Jupyter *notebook* consists of *cells*, and each cell holds some text, a math expression or a Python program. For the moment we shall refrain from explaining how to enter text into Jupyter notebook cells. Instead, we shall focus on evauating expressions and running Python programs.

When you enter some expression or a Python program into a cell, the cell can be *evaluated* by clicking the **Run** button:

or pressing [CTRL]+[ENTER]

For example:

```
3 * 19
```

```
(12 + 51) * 14
```

```
2**(5**3)
```

Let us recall that two asterisks in Python stand for exponentiation.

## 1.2. Jupyter can execute Python commands¶

Sometimes it is convenient to name values, in particular in case we are talking about some complicated numbers such as $\pi$ or some complicated expressions. We can later refer to those values by simply typing the we have given them. For example, the estimated population of the Earth on July 1st, 2019 is 7,714,576,923. Python command

`WorldPopulation_2019 = 7714576923`

will introduce a new variable `WorldPopulation_2019`

into the system and assign to it the value `7714576923`

. Let's recall: names of variables in Python *must* start with a letter and may contain letters, digits and the special symbol `_`

(underscore).

```
WorldPopulation_2019 = 7714576923
```

After running this cell you get no response from the system. The Jupyter has simply memorized that the value of the variable `WorldPopulation_2019`

is 7,714,576,923. It is estimated that 27.8% of world population lives in cities. Therefore, this many people live in cities:

```
WorldPopulation_2019 * 27.8 / 100
```

Let us recall: the word *percent* comes from Latin *pro centum* which means "in a hundred". This is why
$$
47\% = \frac{47}{100} = \text{fourty-seven percent}.
$$

For examples, there are 856 students in a scholl and 25% of them are A students. How many A students are there in the school?

*Answer.* There are 214 A students in the school because
$$
856 \cdot 25\% = 856 \cdot \frac{25}{100} = 214.
$$

There are 326 B students in the same school. What percentage does that make?

*Answer.* Assume this makes $x\%$ of all the students in the school. Then
$$
856 \cdot x\% = 856 \cdot \frac{x}{100} = 326.
$$
Solving for $x$ gives:
$$
x = \frac{326 \cdot 100}{856} \approx 38.08.
$$
Therefore, approximately $38.08\%$ of students in the school are B students.

Let us solve another problem.

**Exercise.** Mary wanted to buy a pair of trousers whose price was \$6,799.99 but her Mom said that that was too much. Mary waited for the sales season and next time she went to the shop the trousers were 25% off. That, together with some lower lip trembling, persuaded Mary's Mom to buy her the trousers. At the cashier the were pleasantly surprised to learn that they were eligible for an additional 3% discount on already discounted trousers. What was the final price of the trousers?

```
price = 6799.99
discount1 = price * 25 / 100
new_price = price - discount1
discount2 = new_price * 3 / 100
new_price - discount2
```

The first four instructions assign some values to some variables. The last line in the above cell contains an expression. Since we are in an *interactive environment*, expressions are evaluated immediately and the result of running the cell is the value of the last expression in the cell.

Instead, we could have written the code like this:

```
price = 6799.99
discount1 = price * 25 / 100
new_price = price - discount1
discount2 = new_price * 3 / 100
print("The price after all the discounts is $", new_price - discount2, sep="")
```

This time the `print`

instruction *prints* the value of the expression togehter with some decorating text while the system *returns nothing as the result of running the cell* (there is no response of the system in the form of `Out[ ]:`

).

While working with interactive environments it is convenient to accept the following manner: we use `print`

only when we have to display several values at once or if we wish to provide an explanation, as we did in the last example.

## 1.3. Libraries¶

In modern programming languages, and Python is one of them, we can do incredible things simply because they all come with a myriad of *functions* that someone has already prepared for us. This makes our lives much easier: most things that an average (and many above-average) users need have already been provided. We just have to find them!

Since we are talking about thousands of functions, literaly, they have all been organized and packed into *libraries of functions*. For example, the library of maths functions is, unsurprisingly, called `math`

. There we can find functions such as `sqrt`

(which computes the *square root*), `sin`

(which computes the *sine* of an angle) and `cos`

(which computes the *cosine* of an angle), but also constants such as `pi`

(which is, of course, an *approximation* of $\pi$).

For example, the following computes the circumference of a circle:

```
from math import pi
r = float(input("Enter radius: "))
obim = 2 * r * pi
print("The circumference is:", obim)
```

The first line in this example demonstrates how to import a particular element from a library:

`from math import pi`

instructs the environment to locate the library `math`

and import `pi`

from it.

Here comes another example. We are going to write a Python program that computes the hypotenuse $c$ of a right triangle given its legs $a$ and $b$. Recall that, by Pythagoras, $c = \sqrt{a^2 + b^2}$.

We clearly need the function `sqrt`

that computes the square root of a number. It's located in the library `math`

, of course.

```
from math import sqrt
a = float(input("a = "))
b = float(input("b = "))
c = sqrt(a**2 + b**2)
print("c =", c)
```

The last two examples were complete computer programs: we got the input from the user using the `input`

command, we processed the data, and then displayed the results using `print`

.

However, working in the interactive environment makes it possible for us to work with pieces of code that we can modify and execute as many times as we like. We can easily *experiment with data*, which is an important part of understanding *modern data analysis*. Instead of using the `input`

command, we assign values to variables directly in the program, run the analysis, change the values assigned to variables, run the analysis again, and repeat the process as many times as needed.

This philosophy applied to the last example (computing the hypotenuse of a right triangle) gives the following code:

```
from math import sqrt
a, b = 3, 4
sqrt(a**2 + b**2)
```

Assignment `a, b = 3, 4`

means that `a`

becomes 3, while `b`

becomes 4. The output of running the cell is the value of the expression `sqrt(a**2 + b**2)`

.

## 1.4. Exercises¶

**Exercise 1.**

*(a)* The price of a book was 24.60 EUR, and then the bookstore reduced the prices of all the books by 20%. What is the new price of the book?

*(b)* After the price reduction, the price of another book in the same bookstore was 14.80 EUR. What was the price of the book before the reduction?

**Exercise 2.** It is estimated that on July 1st, 2019 the population of China was 1,420,062,022. It is also estimated that the population of China increases by 0.35% per year. Estimate the population of China in 2025 under the assumtion that these parameters will not change.

```
```

**Exercise 3*.** It is estimated that on July 1st, 2019 the population of China was 1,420,062,022 and the population of India was 1,368,737,513. It is also estimated that the population of China increases by 0.35% per year, while the population of India increases by 1.08% per year. In how many years is India going to overtake China?

```
```

**Exercise 4.** Write a Python program that computes the area of a circle given its radius.

```
```

**Exercise 5.** The distance between points $A(x_1, y_1)$ and $B(x_2, y_2)$ given by their coordinates in the Euclidean plane can be computed using the formula

Write a Python program that reads coordinates of two points and computes and then prints their distance.

```
```

**Exercise 6.** The *factorial* of a positive integer $n$ is the product of all the integers from 1 to $n$:

Compute 200! and find the number of zeros it ends with. (Help: the library `math`

contains the function `factorial`

that might be helpful.)

```
```

**Exercise 7*.** Using the piece of Python code provided in the cell below find at least one more pair of positive integers $a$ and $b$ such that the hypotenuse of a right triangle with legs $a$ and $b$ is also an integer. Instead of `3, 4`

enter another pair of integers and run the cell. Play with numbers!

```
from math import sqrt
a, b = 3, 4
sqrt(a**2 + b**2)
```