Wednesday, February 24, 2021

Python shell as a simple calculator

With the three basic number types described in the previous post, it is possible to use the Python shell as a simple calculator using the following operators:

  1. + Addition
  2. - Subtraction
  3. * Multiplication
  4. / Floating-point division
  5. // Integer division
  6. % Modulus (remainder)
  7. ** Exponentiation

These are binary operators in that they act on two numbers (the operands) to produce a third (e.g. 2**3 evaluates to 8).

Python 3 has two types of division: floating-point division (/) always returns a floating-point (or complex) number result, even if it acts on integers. Integer division (//) always rounds down the result to the nearest smaller integer (“floor division”); the type of the resulting number is an int only if both of its operands are ints; otherwise it returns a float. Some examples should make this clearer:

Regular (“true”) floating-point division with (/):

>>> 2.7 / 2
1.35
>>> 9 / 2
4.5
>>> 8 / 4
2.0

The last operation returns a float even though both operands are ints.

Integer division with (//):

>>> 8 // 4
2
>>> 9 // 2
4
>>> 2.7 // 2
1.0

Note that // can perform integer arithmetic (rounding down) on floating-point numbers. The modulus operator gives the remainder of an integer division:

>>> 9 % 2
1
>>> 4.5 % 3
1.5 

Again, the number returned is an int only if both of the operands are ints.

We will discuss about operator precedence in the next post

Share:

Thursday, February 18, 2021

The Core Python Language (Numbers)

Among the most basic Python objects are the numbers, which come in three types: integers (type: int), floating-point numbers (type: float) and complex numbers (type: complex).

Integers

Integers are whole numbers such as 1, 8, 􀀀72 and 3847298893721407. In Python 3, there is no limit to their magnitude (apart from the availability of your computer’s memory). Integer arithmetic is exact. For clarity, it is possible to separate any pair of digits by an underscore character, “_.” For example, 299_792_458 is interpreted as the same number as 299792458. 

Floating-Point Numbers

Floating-point numbers are the representation of real numbers such as 1.2, -0:36 and 1:67263 . They do not, in general, have the exact value of the real number they represent, but are stored in binary to a certain precision (on most systems, to the equivalent of 15–16 decimal places). For example, the number 4/3 is stored as the binary equivalent of 1:33333333333333325931846502...., which is nearly (but not quite) the same as the infinitely repeating decimal representation of 4/3 = 1:3333..... Moreover, even numbers that do have an exact decimal representation may not have an exact binary representation: for example 1=10 is represented by the binary number equivalent to 0:10000000000000000555111512... Because of this finite
precision, floating-point arithmetic is not exact but, with care, it is “good enough” for most scientific applications.

Any single number containing a period (“.”) is considered by Python to specify a floating-point number. Scientific notation is supported using “e” or “E” to separate the significand (mantissa) from the exponent: for example, 1.67263e-7 represents the number 1:67263 X 10 to the power -7. As with integers, pairs of digits may be separated by an underscore. For example, 1.602_176_634e-34.

Complex Numbers

Complex numbers such as 4+3 j consist of a real and an imaginary part (denoted by j in Python), each of which is itself represented as a floating-point number (even if specified without a period). Complex number arithmetic is therefore not exact but subject to the same finite precision considerations as floats.
A complex number may be specified either by “adding” a real number to an imaginary one (denoted by the j suffix), as in 2.3 + 1.2j or by separating the real and imaginary parts in a call to complex, as in complex(2.3, 1.2).

Typing a number at the Python shell prompt simply echoes the number back to you:
>>> 5
5
>>> 5.
5.0
>>> 0.10

0.1
>>> 0.0001
0.0001
>>> 0.0000999

9.99e-05 

Note that the Python interpreter displays numbers in a standard way. For example:

1. The internal representation of 0.1 discussed earlier is rounded to “0.1,” which is the shortest number with this representation.

2. Numbers smaller in magnitude than 0.0001 are displayed in scientific notation.

A number of one type can be created from a number of another type with the relevant constructor:
>>> float(5)
5.0
>>> int(5.2)
5
>>> int(5.9)

5
>>> complex(3.)

(3+0j)

>>> complex(0., 3.)
3j

1. Note that a positive floating-point number is rounded down in casting it into an integer; more generally, int rounds towards zero: int(-1.4) would yield -1.

2. Constructing a complex object from a float generates a complex number with the
imaginary part equal to zero.

3. To generate a pure imaginary number, you have to explicitly pass two numbers to
complex with the first, real part, equal to zero.

We will continue with our discussion about Numbers in the next post.

Share:

Tuesday, February 16, 2021

Jupyter Notebook

There are various ways to run Python statements. We can run Python statements in a script and in the interpreter’s interactive mode. The main advantage of using interactive mode is the immediate feedback. The main disadvantage of this mode is that, if we make any mistakes in typing the statements and then execute the erroneous statement, we must rewrite the entire statement to re-execute it. It is also difficult to save it as a program. The option for saving the statements run on the interpreter can be found under the File menu option. However, all the statements and their outputs will be saved in plain text format with a .py extension. If there is any graphical output, it is displayed separately and cannot be stored along with the statements.

Owing to these limitations of interactive mode with interpreter, we will use a better tool for running the Python statements interactively in the web browser: Jupyter Notebook. Jupyter is a server program that can create interactive notebooks in a web browser.

A Jupyter notebook is a web-based notebook that is used for interactive programming with various languages, including Python, Octave, Julia, and R. It is very popular with people working in research domains. A Jupyter notebook can have code, visualizations, output, and rich text in a single file. The advantage of Jupyter Notebook over Python’s own interactive prompt is that, users can edit the code and see the new output instantly, which is not possible in Python interactive mode. Another advantage is that we have the code, rich text elements, and the code’s output (which could be in graphical or rich text format) in the same file on the disk. This makes it easy to distribute. We can save and share these notebooks over the Internet or using portable storage equipment. There are many services online that help us to store and execute Jupyter Notebook scripts on cloud servers.

We can easily install the Jupyter Notebook server program on any computer by running the following command at the command prompt:

pip3 install jupyter

Let’s explore how we can use Jupyter Notebook for writing and executing Python statements. Run the following command at the OS command prompt to launch the Jupyter Notebook server process there: 

jupyter notebook

Once the Jupyter notebook server process is launched, the command prompt window shows a server log, as displayed in Figure below:

 

It launches a web page in the default browser in the OS. If the browser window is already open, it launches the page on a new tab of the same browser window. Another way to open the page (in case you accidentally close this browser window) is to visit http://localhost:8888/ in your browser, which displays a page similar to the one shown in Figure below:

 

The token can be found in the server logs. The following is a sample server log with tokens.
To access the notebook, open this file in a browser:

file:///C:/Users/Ashwin/AppData/Roaming/jupyter/runtime/nbserver-8420-open.html
Or copy and paste one of these URLs:
http://localhost:8888/?token=e4a4fab0d8c22cd01b6530d5daced19d32d7e0c3a56f925c 

or http://
127.0.0.1:8888/?token=e4a4fab0d8c22cd01b6530d5daced19d32d7e0c3a56f925c

In this log, we can see a couple of URLs. They refer to the same page (localhost and 127.0.0.1 are the same hosts). We can either copy and paste any of these URLs directly in the address bar of the browser tab and open the Jupyter Notebook home page or we can visit http://localhost:8888/ as previously mentioned and then paste the token in the server log (in our case it is e4a4fab0d8c22cd01b6530d5daced19d32d7e0c3a56f925c) and log in, which will take us to the same home page.

Note that every instance of the Jupyter Notebook server will have its own token and the token here will not work with your Jupyter Notebook. The token is only valid for that server process. After you follow any one of the routes just explained, you will see a home page tab in the browser window that looks like Figure shown below:

 

So just install this Jupyter Notebook and play with it to understand it's functionality.

Share: