1.
Introduction to Python
Chapter Outline
• Introduction • Different Types • Applications • Python
of Python Installations
• Installing in • Installing in • Installing in • How to run
windows Ubuntu Mac Python
Objectives
• Learn the History of Python.
• Learn different types of Python.
• Learn applications of python.
• Learn how to install python in different platforms.
• Learn different execution methods.
Introduction
The term Python originates from the surreal British comedy group Monty
Python, not from the snake. Python operators are lovingly called
Pythonistas, and mutually Monty Python and serpentine references
usually pep-per Python classes and records. In 1980s Guido Van Rossum
invented Python Programming Language. In other words, the most popular
language such as C, C++, Java and C#, Python Tries to offer a modest but
syntax is very powerful. Python is used for software growth companies and
industries like Google, Yahoo, CERN, Industrial Light and Magic, and NASA
etc. Most well skilled programmers are involved in this area and did great
things with Python. Beauty of Python is access for beginners and allows
them to handle such a beautiful and motivating complications more easily
than other programs.
Python is an easy to learn, and a high-level, interpreted, interactive, and
object-oriented scripting language. Python is developed to use highly
understandable. Uses like English Keywords habitually however the
additional languages use Punctuations.
Why Python is interpreted?
o Python is treated as runtime by the Interpreter. No Compilation
is required before execute your program. It’s like PERL and PHP.
Why Python is Interactive?
o Python supports to write your own programs by using Python
Prompt and interact with Interpreter.
Why Python is Object-Oriented?
o Python supports Object-Oriented Model or Techniques program
that encapsulates code within objects.
Why Python is preferable for Beginner’s Language?
o Python is such a beautiful language for fundamental level
programmers and supports the expansion of a widespread series
of applications from modest text processing to WWW browsers to
games.
Python is also powerful language for current computer programming
language. It accepts round about parallels like FORTRAN, any of the previous
programming languages, but it is more additional influential than FORTRAN.
Python allows you to use variables without declaring them and it depend on
indentation as a control structure. We are not enforced to declare classes in
Python (like Java) but you are free to do so when it is useful.
Features:
High level data types: Strings, Lists, Dictionaries, etc.
Supports Control Structures: If, If-else, If-elif-else, While, and
powerful iterator (for).
Several Levels of Organizational Structure: Functions, Classes,
Modules and packages.
Compile on the fly to byte Code- Source Code is compiles to byte code
without a separator compile step. Source Code segments are called as
“Pre-Compiled” to byte code files.
Object-Oriented – Python offers a reliable way to use objects: like the
whole thing is an object. Especially is it very simple to declare object
types (called classes in object-oriented programming).
Extension in C and C++ -- Extension Segments and extension lead
and its categories can be written by hand. There are some utensils are
available that supports along with this, i.e., SWIG, SIP, Pyrex.
Jython is also one of the kind of Python it “supports with Java”.
To get: Jython Project refer the website
http://www.jython.org/Project/.
Why Python is Super?
Python is a general purpose language, sometimes referred to utilitarian and
which is designed to be simple to read and write. The language is used in
various areas, like web development, deployment, scientific modelling,
server and administrative tools and more.
But surprisingly, many of the developers don’t love this python as their
primary language even though it is so easy to use and learn they select after
first language. It is also one of the reason to make popular. In addition, the
rise in the use of the Django framework for web development, and decline
the popularity of PHP has also contributed t python’s success.
Here below listed some the reason to become the python popular:
1. Python has a Healthy, Active and Supportive Community.
2. Python has some great corporate sponsors.
3. Python supports Big Data & Cloud Data.
4. Python has amazing libraries.
5. Python has reliable and efficient and accessible.
PYTHON INSTALLATIONS
Python will work on Windows, Macintosh, Unix in all operating Systems. Most
of them will use windows, Unix and rest of them they prefer Mac. In
windows versions, Python will work on all areas that is Windows—XP, Vista,
Windows 7. The more important feature is focus on bit that is 32-bit or 64-
bit based on your computer. While installation you need to focus on this bits.
When you want to download Python Software go to http:
www.python.org/download/ and download the suitable Python 3 Windows
installer for your construction. Your choice is sometimes looks like this:
Python 3.1 Windows installer (Windows binary –does not include source)
Python 3.1 Windows AMD64 installer (Windows AMD64 Binary – does
not include source)
Here I am not giving the full direction while installation because mostly small
reconstruction of Python ensue all the time so I am not suggesting and need
to be answerable for you misplaced significant bring up-to-date. You have a
duty to be ready and update your software based on the version
required!!!!.
Here I am giving the some directions to install the latest Version (Be careful
versions may continuously change to need to download the latest version)
with in a simple steps along with printscreens.
Step1: Once download your file, double click on 3.6.1.exe file. Then Click on
Run button and launch the Python installer
Step2: Here Installer wants to ask to install two methods one is Install all
packages and second one is Customize installation. Mostly no Customization
is not required so go to Install Now Option. (Before click on Install Now
option you need to check Install launcher for all users(recommend), Add
Python 3.6 to PATH)
Step3: Installation Process is ready to install in your computer.
Step4: Installation Process shows in Progress bar
Step5: After completion of Successful installation shows the result in pop-up
window Setup was Successful.
Step6: After Successful Completion Python console window will be appear
like this…
Step7: If you want to run the sample program for testing whether the
console working or not just use mathematical calculation and see the result.
Step8: If you want to change the color of the Background just Right click on
the console go to properties there you can Select Screen Background, and
Screen Text also.
Step9: Then your Screen appear like this. (By Changing Screen background
and Screen Text).
Step10: Finally you can use this console for small programs. If you want to
use some more critical program I suggest use IDLE (Python 3.6 32-bit)
Step11: To get IDLE (Python 3.6 32-bit) Go to Start → All programs →
Select IDLE (Python 3.6 32-bit).
Step12: After click on IDLE the window appear like this… (here also
changed the Selected background, and Selected text colors) normally it
shows white background with Black text color.
Step13: Simple mathematical Operation and see the result
How to Run Programs in Python Language?
There are six different methodologies to run the Python Programs.
Method 1: Directly Open Console window (for Reference see Step 10).
Method 2: Use IDLE (Python 3.6 32-bit) version for all programs (for
Reference see Step 13).
Method 3: Go to run prompt give cmd there cmd window will appear, then
there itself just type “Python” Keyword and itself it will open and you can
use it.
Method 4: Go to run prompt directly there itself give “Python” keyword
and itself it will open you can use it
Methods 5: Prefer Notepad for code and to execute use Command Prompt.
Method 6: Access Python through command prompt through different
directory (eg: Your Python software is installed in C: Directory but you can
access through from D: directory also. Not only this D: Directory from other
directories like E: or F: Directory etc., as per your system available
directories) but this Method 6 work through cmd window only. To get this
window Goto→Window Start Menu → run → cmd → After open the window
then you can change the directory and execute. For better clear
understanding and execution see below Method 6 installation process along
with their printscreen output.
Method 1
Directly Use Python Console window for Python Code.
Method 2
Double Click on IDLE (Python 3.6 32-bit) for Python Code.
Method 3
Step1: Directly goto run prompt and give cmd
Step2: It will show cmd.exe window. There itself just you can type
“Python” Keyword.
Step3: Here in cmd.exe just you can type “Python” keyword along
with small mathematical operation and then you can see the output
below.
Method 4
Step1: Use Run Prompt and then type Python.
Step2: Your Python.exe file will be open and ready to use
Method 5:
Here you can follow how to execute the code in Notepad through Command
Window.
Write the code in notepad and Save it as filename.py extension and save in
any folder. After that Open the Command Window and give the python
filename .py and then execute. Below the steps will explain how to execute
the code by method 5
Step1: Open a New Text document file and write a simple code.
Step2: Save as that txt file along with code with filename .py extension
Step3: Give Filename (example test1.py)
Step4: File name test1.py and place that file in folder (ex: PYTHON TEST)
Step5: Your file is saved as test1.py as per your location. Now it’s time to
execute the code. To execute go to RUN prompt.
Step6: In RUN command give cmd to execute the window.
Step7: Your Window will appear after you gave cmd command.
Step8: Through command window Go to program file location (if necessary
use Dos Commands to go particular file location (eg., cd filename etc))
Step9: After you entered into the direct file location then give python
filename. py (eg. Python test1.py)
Step10: See below output as you written the code in notepad.
Method 6
Step1: Go to Run prompt and give cmd command.
Step2 : Your output looks like this
Step 3: Now you can go to D: directory and give command “python”. By
default python will be installed in C: drive, but you can access python from
D: drive also. Look at see
Step 4: Give Python command directly from D: drive and execute the
programs
Step5: access keywords from this window (for testing)
Step 6: Access python from other drive E: and see the output.
Above those Six methodologies will work and execute successfully. The thing
is for beginners mostly use Console Window and IDLE for easy to use. If the
programmers have enough knowledge on Python Language mostly they
prefer to work on this method 5 to write the code in Notepad and it will be
executed in Command Window. It’s a better practice instead of using the
Console Window and IDLE. I used to prefer Command Window to execute
the code from the Notepad and also I suggested to beginners use method 5
for better practice.
Installing Mac OS X
Altogether up-to-date Macintosh computers usage of Intel Chip (Like most
windows PCs). In previous they recycled powerfulPC Chips. Need not
necessary to know the differences and its functionalities just one Mac Python
installer supports for all Macs.
For further information refer www.python.org/download/ and download the
Mac installer. It drives the named somewhat similar Python 3.1 Mac Installer
Disk Image, though the version number may different. Be present sure when
you download the file with latest version.
Step1: Browser should mechanically support the disk image and not built up
a Locator space to display you the substances. Disk Image has huge amount
of text files likes (Build.txt, Licence.txt, ReadMe.txt, and Python.mpkg) you
should double click on python.mpkg.
Step 2: In the first page give a description of python. Click the continue
button for continue.
Step 3: In the next page it shows important python requirements Mac OS X
10.3 or well ahead. If you are running Mac OX X 10.2, you must certainly
renovate. Then click Continue Button to advance.
Step 5: Accept license and select continue button.
Step 6: Agree the terms and conditions and continue
Step 7: for Installation click on Install Button. By default it will be installed in
C: drive, If you want to change the drive location select Change Install
Location and Install the software.
Step 8: If you choose custom installation select Options before installing
Python Framework: for use python
GUI Application: Includes IDLE, graphical python shell
Unix Command-line Tools: Command line 3 Application
Python Documentation: Having plentiful data on docs.python.org
Shell profile updater: It Update the shell Profile
Fix System Python: shows your Mac to usage of Python 3 as the evasion
Python for all handwritings counting with built-in System scripts from Apple.
Step9: Installing process progress bar.
Step 10: Successfully Installed
Step 11: Python Shell looks like this.
Python Keywords
In Python shell programming windows supports some of keywords are
mentioned below:
False def if raise
None del import return
True elif in try
and else is while
as except lambda with
assert finally nonlocal yield
break for not class
from or continue global
pass
To get the keywords from shell (IDLE Python 3.6 32-bit (in windows))
and know the explanation for each keyword and syntax just type the
commands in shell console.
Step 1 → help()
Step 2 → Keywords
PrintScreen Output
Versions
Starting from 1994 there are a lot of changes and version are available for
using python programmers. Below are mentioned the changes of version are
their year.
Python 1.0 January 1994
Python 1.5 December 31, 1997
Python 1.6 September 5, 2000
Python 2.0 October 16, 2000
Python 2.1 April 17, 2001
Python 2.2 December 21, 2001
Python 2.3 July 29, 2003
Python 2.4 November 30, 2004
Python 2.5 September 19, 2006
Python 2.6 October 1, 2008
Python 2.7 July 3, 2010
Python 3.0 December 3, 2008
Python 3.1 June 27, 2009
Python 3.2 February 20, 2011
Python 3.3 September 29, 2012
Python 3.4 March 16, 2014
Python 3.5 September 13, 2015
Python 3.6 December 23, 2016
Simple Calculations in Python
>>> 3.14 * 5
Output : 15.700000000000001
PrintScreen Output
Need not necessary to declare variable or something like in C, C++
programming language. In python simply assign the values directly and get
the result immediately like R Programming. See the below output from R
Console window.
Seems like both are the same methodologies easy to learn and implement.
Using R software you can import the python code and execute in R.
Some the mathematical supported operators we can use in python language.
Here are mentioned below for your information
Operator Example Explication
+, -,*,/ Add, Substract,
Multiply, Divide
% Modulo 25%5 = 0 25/2 =5, remainder = 0
84%5 = 4 84/5 = 16, remainder=4
** exponent 2**10 = 1024
// Floor division 84//5 = 16 84/5 = 16, remainder = 4
Other Simple example using math function:
Code:
>>> from math import *
>>> diameter = 5
>>> perimeter = 2 * pi * diameter
>>> perimeter
Output:
31.41592653589793
PrintScreen Output:
Applications of Python:
Generally, Python is a great support for backend for webpage improvement,
artificial intelligence, data analysis and scientific computing. Here are the list
of areas where python is using widely. They are ….
• Systems Programming
• GUI Programming
• Internet Scripting
• Component Integration
• Database Programming
• Rapid Prototyping
• Numeric & Scientific programming
• Gaming
• Image, Data mining, and Robotics.
Systems Programming: In Systems Programming Python works like an
interface for operating systems services to variety of model for scripting
convenient, supportable system-administration utensils and conveniences.
GUI Programming: Python maintenances GUI Applications that can be
developed and executed by system calls, libraries and windows systems,
such as Windows MFC, Macintosh and the X Window system of UNIX.
Internet Scripting: In this methodology Python works with regular Internet
modules and access Python program to make a widespread changes of
networking jobs along with client and server model.
Component Integration: Capability to extend by an fixed in C and C++
system varieties it beneficial as a flexible glue languages for scripting the
performance of other systems and its mechanisms.
Database Programming: Provides an interface to all major commercial
databases and for traditional database demands, there are python interface
gives support to all commonly used relation databases system like Sybase,
Oracle, Informix, ODBC, MySQL, PostgreSQL, SQLite, and more.
Rapid Prototyping: In python programs, modules written in Python and C
look the same. But it’s likely to prototype systems in Python. Primarily, and
then change designated modules to a compiled language such as C or C++
for delivery.
Numeric & Scientific Programming: Used heavily in Numeric
Programming. The NumPy(a high-performance programming extension for
Python) turns Python into a stylish and easy-to-use numeric programming
tool. The Popular SciPy and ScientificPytong extension provide extra libraries
of scientific programming tools and use NumPy as a core component.
Gaming: Supports working with Multimedia is possible for Pygame, cgkit,
Pyglet, PySoy, Panda3D, and other interpreter will work with most efficient
gaming programming works in Python Language.
Image, DataMining and Robotics: works with image processing with PIL
and its latest Pillo fork, PyOpenGl, Blender, Maya and some others will
support to use for images and Data Mining with the Orange framework, the
patter Bundle, Scrapy, and Custom code will support the functionalities. For
Robotics control programming like use PyRo toolkit.
Remember while working with Python Code:
• Hash Mark (#) used to indicate a PYTHON COMMENT.
• New Line Character (\n) is the standard line SEPARATOR.
• Back Slash (\) carry on a LINE.
• Semicolon (;) add a DUAL DECLARATIONS of Lines.
• Colon (:) Splits a HEADER LINE from its Suite.
Suite:
Suite is a group of separate declaration of creations used for only one block
of code is called suite in Python Programming Language. Multifaceted
statements like if, while, def & Class are those which necessitate a Starting
line & a suite. Header lines starts with the declaration & lay off followed by
colon(:) with single or additional lines of code.
Sample Syntax:
if expression:
Suite
elfi expression:
suite
else:
suite
How Python Works?
Whenever you are working in python the running program (Python Code)
works as a Script or interactively in a shell, the Python translator organizes a
countless transaction of work to bring out the guidelines in your software
package. This effort can be cracked into a sequence of ladders, as shown
below…
SYNTAX CHEKCER and
Code TRANSLATOR
COde Errors
Byte Code
PYTHON VIRTUAL MACHINE
User Inputs (PVM) Other Error
message
Outputs
Fig: Steps in Interpreting a Python Program
The translator/Interpreter reads a Python expression or statement, also
called them as source code, and it confirms that is sound shaped. In this
methods, the interpreter acts as a severe English Teacher who discards
somewhat judgment that it does not follow to the sentence structure to
creation of rules, or Syntax, of the language. As soon as translators come
across such an mistake, it pauses conversion with an fault communication.
If a python expression is well formed, the interpreter than translates it to an
corresponding form in a low-level linguistic call byte code. When translator
turns into a script, it totally decodes it to byte code. This byte code is
transfer to different software module, called the Python Virtual Machine
(PVM), where it is performed. If additional error arises for the duration of
this process, implementation also stops with an error memo.
Python Scripts (Programs)
Whenever want to organize to do more than a slight calculations, it is
recommended to transcribe a script ( a program in python). It can be used
in IDLE, then python editor.
The best option is too Geany, a slight freeware editor with composition
coloring, since which you can straight away jump into your script.
To write and run a program in IDLE:
• Go to Menu File → Open New Window
• Write Script
• Go to File → Save (your file name with extension .py, for sample
firstprogram.py)
• To Run program: Press <F5> or Go to Menu → Run → Run Module.
Take care
• In python white spaces are Necessary…
• The indentation of a source code is too significant
• A program that is not mentioned indented gives the output with errors
or does not what you require.
• Python is case sensitive
for sample “m” as well as “M” are dualistic dissimilar variables.
Summary
- Python is an easy to learn, and a high-level, interpreted, interactive,
and object-oriented scripting language.
- It is treated as runtime by the Interpreter. No compilation is required
before execute your program. It’s Like PERL and PHP.
- It Supports to write your own programs by using Python Prompt and
interact with Interpreter.
- The provisions of Object-Oriented Model as well as Techniques
program that summarize code within objects.
- Python is a such a beautiful linguistic for fundamental level
programmers and provisions the expansion of a widespread series of
requests.
- Python is a general purpose language, sometimes referred to
utilitarian and which is designed to be simple to read and write.
- Jython is a java implementation that combines expressive power with
clarity using java with Python.
- CPython is also an interpreter, and also supports code with C language
and use python interpret to execute the program.
- PyPy aims to establish a good platform for collective conversion, and
support framework for reproducing implementations for dynamic
languages.
- Iron Python is used to implement the Python Software design linguistic
for .Net Framework and Mono.
- Stackless Python or simply called as Stackless, it is also a Python
programming Language interpreter, name is as evades dependent on
C request stack for its individual stack.
-
Quote
Man Needs his Difficulties Because they are Necessary to enjoy SUCCESS.
---APJ Abdul kalam.