Python has two basic modes: normal and interactive. The normal mode is the mode where the scripted and finished .py files are run in the Python interpreter. Interactive mode is a command line shell which gives immediate feedback for each statement, while running previously fed statements in active memory. As new lines are fed into the interpreter, the fed program is evaluated both in part and in whole. To start interactive mode, simply type “python” without any arguments.
We’ll initially use the interactive mode to explore some of the basics of the python language. One of the benefits of working with an interpreted language is that you can test bits of code in interactive mode before you put them in a script. But there are differences between interactive mode and script mode
Instead of Python exiting when the program is finished, you can use the -i flag to start an interactive session. This can be very useful for debugging and prototyping.
python -i hello.py
To start Python’s interactive help, type “help()” at the prompt.
You will be presented with a greeting and a quick introduction to the help system. Notice that the prompt will change from “>>>” (three right angle brackets) to “help>”. You can access the different portions of help simply by typing in modules, keywords, or topics. You can obtain help on a given topic simply by adding a string in quotes, such as help(“object”).
The variables a and b simultaneously get the new values 0 and 1. On the last line this is used again, demonstrating that the expressions on the right-hand side are all evaluated first before any of the assignments take place. The right-hand side expressions are evaluated from the left to the right.
>>> a, b = 5, 6 >>> print(a + b) 11 >>> 2**8 256
2**8 can be read as 2 power of 8, or in more familiar math notation 28
Strings and string concatenation
In the next example the word alphabet is a variable, while the letters of the alphabet (in quotes) are a string (of characters if you like). Anything inside quotes is a string. The built in function len() determines the length of the string. A list of the built in python functions can be found here/p>
>>> alphabet = "abcdefghijklmnopqrstuvwxyz" >>> print("The alphabet consists of ",len(alphabet)," letters") The alphabet consists of 26 letters
String concatenation just means joining strings together. In the next example a variable called myname is assign a string value of “Alan”
>>> myname = "Alan" >>> print("hello " + myname) hello Alan >>>
In interactive mode, the last printed expression is assigned to the variable _. This means that when you are using Python as a desk calculator, it is easier to continue calculations.
In this example, two variables have been defined, i.e. vat and price, and in this case they represent numbers. These numbers have then been multiplied together, and the answer is held by the special variable _ (underscore). The original price has then been added to the variable _ to give the overall price + VAT.
>>> vat = 0.20 >>> price = 50.8 >>> price * vat 10.16 >>> price + _ 60.959999999999994 >>> round(_, 2) 60.96
In the above example the answer is not quite what we expected, due to a small error in the binary arithmetic. Since the expected to be to just two decimal places another built in function called round() has been used get get an answer to just 2 decimal places.
This _ variable should be treated as read-only by the user. Don’t explicitly assign a value to it, as you would then create an independent local variable with the same name, masking the behaviour of built-in variable, and potentially confusing anyone looking at your code.
Since a variable can represent either a number or a string, you might wonder what happens if I multiply a number by a string. The quickest way to find out is to try it!
>>> myname = "Alan" >>> price * myname Traceback (most recent call last): File "
", line 1, in TypeError: can't multiply sequence by non-int of type 'float' >>>
The interpreter is smart enough to know you can’t do that, and tells you so.!
Python programs are nothing more than text files, and they may be edited with a standard text editor program, but an editor customised for python is better. Fortunately the Linux text editor gedit can be for just that purpose, as shown here no need to go hunting for yet another text editor.
Lets use the last example above and, with one minor adjustment enter the following commands into the gedit text editor. Exit the interactive python shell by typing ctl-D or quit() to return to the command line.
Open the gedit text editor and type the following commands. In this example a built in function has been used called round(). Clicking in the link will show you a list of other functions some of which will be used in other examples.
vat = 0.2 price = 50.8 total = round(price * vat,2) print(total)
Save the file as tax.py. Now from the command line type
> python3 tax.py 10.16
As you can see the program has run and printed the answer, but that’s not very helpful, unless you know what the program is doing.
In the book the hitch hikers guide to the galaxy to learn the Answer to the Ultimate Question of Life, The Universe, and Everything from the supercomputer, Deep Thought, specially built for this purpose. It takes Deep Thought 7½ million years to compute and check the answer, which turns out to be 42.
When asked by Arthur Dent, why 42? Deep though replies
6 multiplied by 9. But says Arthur
6 x 9 is 54!
Exactly says Deep thought.
The answer to our little program is equally meaningless without some explanation.
There is a footnote to this little story. Some readers of the book noticed that 613 * 913 = 4213. Douglas Adams the author of the book was asked many times why he chose the number 42 and many theories were proposed, including that 42 is 101010 in binary code, that light refracts off water by 42 degrees to create a rainbow, that light requires 10−42 seconds to cross the diameter of a proton. On 3 November 1993, he gave an answer saying It was a joke. It had to be a number, an ordinary, smallish number, and I chose that one. Binary representations, base thirteen, Tibetan monks are all complete nonsense. I sat at my desk, stared into the garden and thought, 42 will do. I typed it out.
This just scratches the the surface of what you can do with the interactive mode. When your code starts to be become more than just a few lines, the normal is much more convenient for debugging etc.