Skip to main content

Basic maths for python

Basic Math
Python has a bunch of built-in arithmetic operators. The table below provides a brief
comparison of the short notations and their longer equivalents.
Assume that initially the following is true: a = 3.
Finding the square root of a number is also easy in Python, but it’s not a built-in language
feature. You’ll find out about it later in the book!
You can calculate the absolute value of a digit (e.g. |-3|), using abs(). For example, abs(-
3) returns 3.

Operators precedence
The order of execution of mathematical operations in Python is similar to the order used
in conventional mathematics.
In maths there are three general operator priority levels:
1. exponents and roots
2. multiplication, division and modulus
3. addition and subtraction
Exponents and roots are represented by functions of a standard library in Python and are
covered further on in this book.


All other priority levels have their own built-in Python operators.
Higher order operations are performed before the lower ones. All the operations of the
same order are executed one by one from left to right.
For example, the statement: 2 + 3 * 4 - 1 returns 13. The multiplication is performed
before the addition and subtraction.
In order to affect the order of operator execution, you should use parentheses (). For
example, (3 + 6) * 2 equals 18 and 8 / (1 + 1) equals 4.
*********************


*Code Samples
The code snippet below shows how simple maths operations are processed in the Python
interpreter.
26
>>> 10 - 32
-22
>>> 11 % 10
1
>>> 6 ** 2
36
>>> 12 / 8
1.5
>>> 12 % 8 + (12 // 8) * 8
12
>>> 6 - -16
22
>>> a = 16
>>> a += 3
>>> a
19
>>> a = 4
>>> b = 6
>>> a *= b
>>> a
24
Exercises
PROBLEM: Try to answer the following questions without Python and then use the
interpreter to verify your answers:
1. What is the result of the following calculation: 2 + 2 + 2 + 2 + 2 + 2 * 0?
2. What about this one: (2 + 2 + 3 + 4) * (4 * 4 - (2 * 6 + 4))?
ANSWER: 10 - if you answered 0 you forgot that multiplication has a higher precedence
than addition
1. 0 - if you answer was something else you forgot that operations in brackets should
be performed first - a product of the second expression in brackets - (4 * 4 - (2 * 6
+ 4)) is 0 and anything multiplied by 0 is 0 as well
*********************
********************
Exercises
PROBLEM: A car has a speed of 100 km/h. Write some code that calculates the distance
in meters (not kilometers!) that the car would travel in N hours. Set N to 10.
ANSWER:
1. N = 10
2. distance_in_kilometers = 100 * N
3. distance_in_meters = distance_in_kilometers * 1000
*********************
********************
Exercises
PROBLEM: Rewrite the code using short notations
1. N = N * J
2. i = i + j * 18
3. f = f - k / 2
ANSWER:
1. N *= J
2. i += j * 18
3. f -= k / 2
*********************
********************
Exercises
PROBLEM: Given that f(x) = |x^3 - 20|, find f(-3). Your answer should consist of two lines
of code: one which initialises x to -3, and one that calculates the result.
ANSWER: The following Python code shall do the job:
>>> x = -3
>>> abs(x ** 3 - 20)
47
*********************
********************
Exercises
PROBLEM: Try to solve the previous problem using -3 inline, rather than using the x
variable.
ANSWER:
Your solution should look like this:
>>> abs((-3) ** 3 - 20)
47
If it looks like this:
>>> abs(-3 ** 3 - 20)
47
it is wrong because negation (i.e. subtraction from zero) has lower precedence than the
exponent and thus has to be enclosed in parentheses. Even though your solution gives
the same result as the correct one, it is a pure coincidence. Try to find value of x for f(x)
= |x^2 - 20| with and without parentheses around the negation to see the difference.
*********************
********************
Exercises
PROBLEM: How can you check if an integer is odd using Python?
ANSWER: Use the modulo operator to find the remainder when you divide by 2.
If the remainder is 1, the digit is odd.
>>> 3 % 2
1
>>> 4 % 2
0

*********************

Comments

Popular posts from this blog

string opperation in python

String operations Words and sentences are collections of characters, and so are Python strings. You can access any character in a Python string using its index, either counting from the beginning or the end of the string. When you start from the beginning, you count from 0. In Python integer indices are zero based. >>> name = “My Name is Mike” >>> name[ 0 ] ‘M’ >>> name[ 1 ] ‘y’ >>> name[ 9 ] ‘s’ >>> name[ - 1 ] ‘e’ >>> name[ - 15 ] ‘M’ >>> name[ 7 ] ‘ ‘s ********************* ******************** Slicing You can take shorter substrings from inside a longer string. This operation is called ‘slicing’. >>> name[ 11 : 14 ] # from 11th to 14th, 14th one is excluded ‘Mik’ >>> name[ 11 : 15 ] # from 11th to 15th, 15th one is excluded ‘Mike’ If you want to check if a string contains a particular substring, you can use the ‘in’ operator. >>> “B...

How to install python 3.0 in your pc??

Installation There are pre-compiled packages made for easy installation on multiple operating systems such as Windows, Linux, UNIX and Mac OS to name but a few. If you head over to https://www.python.org/downloads/ you can select the right installation method for your operating system. Most Linux systems come with Python preinstalled. Version 3 of Python is used in this book. In Windows, once you have installed Python, select Python IDLE or Python command line/console from the Start menu or screen. The difference between the IDLE and the console is that the IDLE has a graphical interface, which looks similar to a text editor. In Linux, UNIX, and OS X, you can launch Python emulator from command line by typing Python. To choose a specific version of Python type PythonX where X is the version number (e.g. “Python3” for version 3). Hello World! Most of the time that you write Python code, you will be writing the script in the IDLE or any other IDE like Eclipse or rich text...

What is python variable?

Have you ever forgotten a birthday of a friend relative?computers can hold on to that information if we tell them to.computers use variables to store information? like moving boxes. Variables have content and names that tell us what’s inside. In Python, a variable can be used to store the output of a statement in the computer’s memory.                                                            A=23 The equals sign is used to assign a value to a variable. The name of the variable is called its identifier    FOR ex. Day = “Monday” Now anytime we mention the day in our code. the computer will know that we're referring to the value “Monday” inside of it. Because day stored the ...