pyChapt-1
pyChapt-1
, Basmath
Unit-I
History of Python:-
Python was developed by Guido van Rossum in the late eighties and early nineties
at the National Research Institute for Mathematics and Computer Science in the
Netherlands.
Python is derived from many other languages, including ABC, Modula-3, C, C++,
Algol-68, SmallTalk, and UNIX shell and other scripting languages. Guido van
Rossum publishes first python 0.9.0 in February-20, 1991.
Python is copyrighted. Like Perl, Python source code is now available under the
GNU General Public License (GPL).
Interactive mode and script mode. In interactive mode, you type Python programs
and the interpreter prints the result:
>>> 1 + 1
2
The chevron, >>>, is the prompt the interpreter uses to indicate that it is ready.
If you type 1 + 1, the interpreter replies 2.
Alternatively, you can store code in a file and use the interpreter to execute the
contents of the file, which is called a script.
By convention, Python scripts have names that end with py.
Python Versions:-
Features of python:-
2. Easy-to-read: Python code is more clearly defined and visible to the eyes.
6. Python is free: python is free ware open source software. You can get the
entire system for free over the internet. There are no restrictions on copying it.
7. A broad standard library: Python's bulk of the library is very portable and
cross-platform compatible on UNIX, Windows, and Macintosh.
8. Interactive Mode: Python has support for an interactive mode, which allows
interactive testing and debugging of snippets of code.
9. Portable: Python can run on a wide variety of hardware platforms and has the
same interface on all platforms.
10. Extendable: You can add low-level modules to the Python interpreter. These
modules enable programmers to add to or customize their tools to be more
efficient.
12. GUI Programming: Python supports GUI applications that can be created
and ported to many system calls, libraries and windows systems, such as
Windows MFC, Macintosh, and the X Window system of Unix.
13. Scalable: Python provides a better structure and support for large programs
than shell scripting.
14. Python is powerful: Python provides all the simplicity and ease of use of a
scripting language along with more advanced programming tools typically
found in systems devilment language.
Python Interpreter:-
An interpreter reads a high-level program and executes it, meaning that it does what
the program says. It processes the program a little at a time, alternately reading lines
and performing Computations
Python, for example, is used mainly as an interpreted language, but compilers for it
are available. Interpreted languages are better suited for dynamic, explorative
development which many people feel is ideal for beginning programmers.
There are two ways to use the Python interpreter: shell mode (Interactive mode) and
script mode.
In shell mode, you type Python statements into the Python shell and the interpreter
immediately prints the result.
The python shell script starts with >>>, which is the Python prompt. The interpreter
uses the prompt to indicate that it is ready for instructions.
If we type print 1 + 1 the interpreter will reply 2 and give us another prompt.
The Python download requires about 30 Mb of disk space; keep it on your machine,
in case you need to re-install Python. When installed, Python requires about an
additional 90 Mb of disk space.
Downloading
The file named python-3.6.2.exe should start downloading into your standard
download folder. This file is about 30 Mb so it might take a while to download
fully if you are on a slow internet connection (it took me about 10 seconds
over a cable modem).
2. Move this file to a more permanent location, so that you can install Python
(and reinstall it easily later, if necessary).
3. Feel free to explore this webpage further; if you want to just continue the
installation, you can terminate the tab browsing this webpage.
4. Start the Installing instructions directly below.
Installing
2. Click Run.
Ensure that the Install launcher for all users (recommended) and the Add
Python 3.6 to PATH checkboxes at the bottom are checked.
3. Highlight the Install Now (or Upgrade Now) message, and then click it.
A User Account Control pop-up window will appear, posing the question Do
you want the allow the following program to make changes to this
computer?
A new Python 3.6.2 (32-bit) Setup pop-up window will appear with a Setup
Progress message and a progress bar.
Verifying
In python there are two modes, “INTERACTIVE MODE” and “SCRIPT MODE”.
The interactive mode is a command-line shell that gives immediate feedback for
each statement, while running previously fed statements inside the active memory.
So once new lines are fed into the interpreter, the fed program is evaluated each
partly and as a full program. Interactive mode is beneficial after you simply wish to
execute basic Python commands or just wish to undertake numerous syntaxes and
easy functionalities of python.
Thus, open a command prompt in WINDOWS to call a python shell to run the
interactive mode and thereafter type “Python” and press enter.
The >>> indicates that the Python shell is ready for execution in the interactive
mode.
We can perform a number of simple tasks or run a few simple programs or test some
Python functions, using the interactive mode.
To write long Python codes or programs having multiple files, the interactive mode is
not what I would recommend using on the first hand. Script mode would be a better
choice in such scenarios.
Using Script mode is quite easy, you have to write your code in a text file and save
the particular file with a ‘.py’ extension. .py stands for PYTHON. You can use any
available text editor.
In a standard Python shell, you can simply click “FILE” then choose “NEW” or
press “ctrl+N” to open a blank script where you can input your code. Saving the
code is quite is by pressing “Ctrl+S”. Similarly running a script in it is as easy as
pressing the key “F5”.
The syntax of the programs remains the same. “IDLE” is the popular GUI based
Python shell to write programs and perform all the functions mentioned above.
Python IDLE is the Python Integration Development Environment (IDE) that comes
with the Python distribution by default.
In short, the Python IDLE helps you experiment with Python quickly in the trial-and-
error manner.
The following shows you step by step how to launch the Python IDLE and use it to
execute the Python code:
Now, you can enter the Python code after the cursor >>> and press Enter to execute
it.
For example, you can type the code print('Hello, World!') and press Enter, you’ll see
the message Hello, World! immediately on the screen: