Internal working In Python - ProLearner00
INTERNAL WORKING IN PYTHON
Before you start working in Python, you need to install
Python on your computers. There are multiple Python distributions available
today. ◆ Default installation available from www.python.org is called CPython
installation and
comes with Python interpreter, Python IDLE (Python GUI) and
Pip (package installer). There are many other Python distributions available
these days. Anaconda Python distribution is one such highly recommended
distribution that comes preloaded with
many packages and libraries (e.g., NumPy, SciPy, Panda
libraries etc.).
Many popular IDEs are also available e.g., Spyder IDE,
PytCharm IDE etc. Of these, Spyder IDE is already available as a part of
Anaconda Python distribution.
To install any of these distributions, PLEASE REFER TO
APPENDIX A. We shall learn to work with both these distribution types [but my
personal favourite is Anaconda ;) - not the reptile, the Python distribution
:)]
Once you have Python installed on your computers, you are
ready to work on it. You can work
in Python in following different ways:
(i) in Interactive mode (also called Immediate Mode)
(ii) in Script mode
Working in Default CPython
Distribution
The default distribution, CPython, comes with
Python interpreter, Python IDLE (GUI based) and pip (package installer). To
work in interactive as well as script mode, you need to open Python IDLE.
Working in Interactive Mode (Python IDLE)
Interactive mode of working means you type the command - one command at a time, and the Python executes the given command there and then and gives you output. In interactive mode, you type the command in front of Python command prompt >>>. For example, if you type 2 +5 in front of Python prompt, it will give you result as 7:(ii) It will open Python Shell [see Fig. 1.1(b)] where you'll see the Python prompt (three signs ie., >>>).
(iii) Type commands in front of this Python prompt and Python will immediately give you the result. [see Fig. 1.1(c)]
For example, to print string
"Hello" on the screen, you need to type the following in front of
Python prompt (>>>)
>>> print
("Hello")
And Python interpreter will
immediately display string Hello below the command. To display, you just need
to mention name or expression [Fig. 1.1(c)] in front of the prompt.
Figure 1.1(c) shows you some
sample commands that we typed in Python shell and the output returned by Python
interpreter.
Working in Script Mode (Python IDLE)
Interactive mode proves very
useful for testing code: you type the commands one by one and get the result or
error one by one.
What if you want to save all the
commands in the form of program file and want to see all output lines together
rather than sandwiched between successive commands? With interactive mode, you
cannot do so, for:
Interactive mode does not save the
commands entered by you in the form of a program. The output is sandwiched
between the command lines [see Fig. 1.1(c)].
The solution to above problems is
the Script mode. To work in a script mode, you need to do the following:
Step 1: Create Module / Script /
Program File
Firstly, you have to create and
save a module / Script / Program file.
To do so, follow these
instructions:
(i) Click Start button All
Programs → Python 3.6.x→ IDLE. [Fig. (ii) Click File → New in IDLE Python
Shell. [Fig. 1.2(a)]
1.2(a)]
(iii) In the New window that
opens, type the commands you want to save in the form of a program (or script).
[Fig. 1.2(b)]
For instance, for the simple Hello
World program, you'll need to type following line :
print ("Hello World!")
(iv) Click File → Save and then save the file with an
extension .py. The Python programs has .py extension [Fig. 1.2(c)]. For
instance, we gave the name to our program as
Hello.py.
Now your program would be saved on the disk and the saved
file will have .py extension.
Step 2: Run Module / Script / Program File
After the program/script file is created, you can run it by
following the given instructions:
(i) Open the desired program/script file that you created in
previous Step 1 by using IDLE's File→→ Open command. If the program / script
file is already open, you can directly move to next instruction.
(ii) Click Run → Run Module command [Fig. 1.3(a)] in the
open program / script file's window.
You may also press F5 key.
Comments
Post a Comment