from __future__ import division import sys from sympy import * sys.displayhook = pprintRead 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".
6 * 12 n1 = 4 n2 = 5 n1 + n2 result = n1 * n2 resultThus, 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.
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/4By the way, the last four lines show that
1 3 1 * 2 3 5 - + - = ----- + - = - 2 4 4 4 4Hopefully, you still remember this from School!
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
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
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.)
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