#### 1 Starting Python/SymPy

You need the following lines to create the settings required for the exercises below.
```from __future__ import division
import sys
from sympy import *
sys.displayhook = pprint
```
Read these instructions for more details on how to setup your account and use Python on Socweb.

Note: the last line "sys.displayhook = pprint" is optional. It tries to print the formulas in a nicer way, but this only works if the character set of the client window is correctly configured. So if the formulas have strange characters in them, don't use "sys.displayhook = pprint".

#### 2 Using Python/SymPy in the interactive mode

Enter the following lines one at a time. You can either type them in or copy and paste from here.
```6 * 12
n1 = 4
n2 = 5
n1 + n2
result = n1 * n2
result
```
Thus, you can use python like a calculator!

If you want to correct or change a previous line, you can go back to that line using the uparrow key.
Try this: change n1 + n2 to n1 + n2 + 1.

There are several operators related to division:

 division 21 / 10 == 2.1 integer division 21 // 10 == 2 remainder 21 % 10 == 1

Note: the "/" operator only works in this manner because you executed earlier: "from __future__ import division".

Two equal signs (==) are used to test for equality.

#### 3 Examples of simple calculations

In some cases SymPy does not provide a numerical result, but only transforms the expressions. For example,
sqrt(2) = 2**(1/2) (or   √ 2 = 21/2).
If you want to see a numerical result use N(). Try the following:
```2**3
pi
N(pi)
sqrt(2)
N(sqrt(2))
sqrt(2)*pi
N(sqrt(2)*pi)
a = Rational(1,2)
b = Rational(3,4)
a + b
a + b == 5/4
```
By the way, the last four lines show that
```1   3   1 * 2   3   5
- + - = ----- + - = -
2   4     4     4   4
```
Hopefully, you still remember this from School!

#### 4 Arithmetical operator precedence

What is 3**2*4 + 2/5?

The precedence is as follows:

 1 exponentiation ** 2 multiplication, division, integer division, remainder *, /, //, % 3 addition, subtraction +, - 4 comparison ==, <, <=, >, >=, !=

Try the following expressions and observe how precedence is applied:

```1 + 2 * 3 + 4
1 + (2 * 3) + 4
(1 + 2) * (3 + 4)
2**3 + 1
2**(3 + 1)
4 == 3 + 1
(4 == 3) + 1
```

#### 5 Using variables

Programming variables (n1 = 4; result = n1 + 1) are different from mathematical variables ( x = Symbol('x'), y = Symbol('y')). Programming variables have a current value assigned. They can be assigned a new value at any time. Mathematical variables express an unknown constant. For example, try the expressions below. Don't worry if you don't remember the mathematical details for all of these from School. The point of these exercises is to show how CAS software can solve in seconds, what might take a long time to achieve with pen and paper.
```x = Symbol('x')
y = Symbol('y')
x+y+x-y
((x+y)**2).expand()            ### expands expressions
(x+y+x-y).subs(x,13)           ### substitutes a value
together(1/x + 1/y)            ### combines several fractions into one
diff(x**3 + x**2 + 3 * y, x)   ### differentiates an expression
integrate(6*x**5, x)
solve(x**2 -1, x)              ### solves algebraic equations
solve([x + 5*y - 2, -3*x + 6*y - 15], [x, y])
Integral(x**2, x)              ### prints an integral function
```

#### 6 Python Exercises

1) A magician asks a person to think of a number, then add 3 to that number. Then multiply the result by 2, subtract 4, subtract twice the original number, finally add 3. Then the magician correctly guesses that the result is 5.
Write a formula for this and test it with Sympy. You don't need solve() for this because the result is deterministic. Can you simplify the formula with pen and paper so that it reduces to 5?

2) Solve the equation: 7x + 3(x + 1) = 2x/4 - 1
Hint: you need to insert the correct operators and transform the equation so that one side says 0. You then apply the solve() function to the side of the equation which does not say 0.

3) Which of the following are equal?
x**2 - 1
(x + 1) * ( x + 1)
(x - 1) * ( x + 1)
x**2 + 2* x + 1
Hint: python will not automatically expand the expressions. In order to compare expressions (with ==), you need to use the expand() function.

4) a) Substitute x = 1 in x * (x**2 +5), then expand the expression;
b) substitute x = x + 1 in x * (x**2 +5) then expand the expression.

(The background for this exercise is mathematical induction which is a method of proof by showing that a) a fact holds for 1 and b) if it holds for x, it also holds for x +1. In this case, the goal is to prove that x * (x**2 +5) is divisible by 6 if x is any integer. The proof is successful if the result from a) is a multiple of 6 and all summands in the result from b) are either divisible by 6 or by x because it was assumed that x is divisible by 6.)

#### 7 Web Exercise

5) Apart from the prime numbers discussed in the lecture, there are many other number sequences studied in mathematics and some other fields. Luckily, there are resources on the web that can be used to identify the sequences. Use the Encyclopedia of Integer Sequences to identify the following sequences. (Don't read all the details on that site and don't worry if you don't understand everything.)

For the first two sequences and the last one, you could try to guess what principle is used to build them before you look them up. These sequences are built using a simple construction.

0, 1, 1, 2, 3, 5, 8, 13

1, 1, 2, 6, 24, 120

70, 836, 4030, 5830, 7192, 7912, 9272

1, 3, 7, 9, 13, 15, 21, 25, 31

4, 3, 3, 5, 4, 4, 3, 5