Introduction To Python
Introduction To Python
Introduction To Python
INTRODUCTION TO PYTHON
Instructor: Vincent Richards
Office: CIS Dept.
Office hrs.: MW(9:30-10:50, 5:00-6:20), TUTH (9:30-10:50, 5:00-6:20) by appt
E-mail: vincent.richards@ncu.edu.jm
Tel#: 963-7285
Website: http://lms.ncu.edu.jm
2
What is Python?
• Python is a general-purpose interpreted, interactive,
object-oriented, and high-level programming
language. It was created by Guido van Rossum
during 1985- 1990. Like Perl, Python source code is
also available under the GNU General Public
License (GPL).
3
Characteristics of Python
The Following are important characteristics
of Python Programming −
•It supports functional and structured programming
methods as well as OOP.
Characteristics of Python
• It provides very high-level dynamic data types and
supports dynamic type checking.
Applications of Python
As mentioned before, Python is one of the most
widely used language over the web. A few of them
are:
•Easy-to-learn − Python has few keywords, simple
structure, and a clearly defined syntax. This allows
the student to pick up the language quickly.
•Easy-to-read − Python code is more clearly defined
and visible to the eyes.
•Easy-to-maintain − Python's source code is fairly
easy-to-maintain.
10
Audience
• This Python tutorial is designed for software
programmers who need to learn Python
programming language from scratch.
• Prerequisites
Getting Python
• The most up-to-date and current source code,
binaries, documentation, news, etc., is available on
the official website of Python
https://www.python.org/
• You can download Python documentation from
https://www.python.org/doc/. The documentation is
available in HTML, PDF, and PostScript formats.
17
Installing Python
• Python distribution is available for a wide variety of
platforms. You need to download only the binary
code applicable for your platform and install Python.
• If the binary code for your platform is not available,
you need a C compiler to compile the source code
manually. Compiling the source code offers more
flexibility in terms of choice of features that you
require in your installation.
18
Windows Installation
• Here are the steps to install Python on Windows
machine.
• Open a Web browser and go to
https://www.python.org/downloads/.
• Follow the link for the Windows installer python-
XYZ.msi file where XYZ is the version you need to
install.
20
Windows Installation
• To use this installer python-XYZ.msi, the Windows
system must support Microsoft Installer 2.0. Save
the installer file to your local machine and then run it
to find out if your machine supports MSI.
• Run the downloaded file. This brings up the Python
install wizard, which is really easy to use. Just
accept the default settings, wait until the install is
finished, and you are done.
21
Macintosh Installation
• Recent Macs come with Python installed, but it may
be several years out of date. See
http://www.python.org/download/mac/ for
instructions on getting the current version along with
extra tools to support development on the Mac. For
older Mac OS's before Mac OS X 10.3 (released in
2003), MacPython is available.
• Jack Jansen maintains it and you can have full
access to the entire documentation at his website −
http://www.cwi.nl/~jack/macpython.html. You can
find complete installation details for Mac OS
installation.
22
Setting up PATH
• Programs and other executable files can be in many
directories, so operating systems provide a search
path that lists the directories that the OS searches
for executables.
• The path is stored in an environment variable, which
is a named string maintained by the operating
system. This variable contains information available
to the command shell and other programs.
23
Setting up PATH
• The path variable is named as PATH in Unix or Path
in Windows (Unix is case sensitive; Windows is not).
• In Mac OS, the installer handles the path details. To
invoke the Python interpreter from any particular
directory, you must add the Python directory to your
path.
24
Running Python
There are three different ways to start Python −
• Interactive Interpreter
• You can start Python from Unix, DOS, or any other system
that provides you a command-line interpreter or shell window.
• Enter python the command line.
• Start coding right away in the interactive interpreter.
• $python # Unix/Linux
or
• python% # Unix/Linux
or
• C:> python # Windows/DOS
28
Python Identifiers
• A Python identifier is a name used to identify a variable,
function, class, module or other object. An identifier starts
with a letter A to Z or a to z or an underscore (_) followed by
zero or more letters, underscores and digits (0 to 9).
• Python does not allow punctuation characters such as @, $,
and % within identifiers. Python is a case sensitive
programming language.
Thus, Manpower and manpower are two different identifiers
in Python.
38
Python Identifiers
Here are naming conventions for Python identifiers −
• Class names start with an uppercase letter. All other
identifiers start with a lowercase letter.
• Starting an identifier with a single leading underscore
indicates that the identifier is private.
• Starting an identifier with two leading underscores indicates a
strongly private identifier.
• If the identifier also ends with two trailing underscores, the
identifier is a language-defined special name.
39
Reserved Words
The following list shows the Python keywords. These are
reserved words and you cannot use them as constant or
variable or any other identifier names. All the Python
keywords contain lowercase letters only.
40
Reserved Words
41
For example −
if True:
print "True"
else:
print "False"
However, the following block generates an error −
if True:
print "Answer"
print "True"
else:
print "Answer"
print "False"
Therefore, in Python all the continuous lines indented with
same number of spaces would form a block.
43
Quotation in Python
• Python accepts single ('), double (") and triple (''' or """)
quotes to denote string literals, as long as the same type of
quote starts and ends the string.
• The triple quotes are used to span the string across multiple
lines. For example, all the following are legal −
word = 'word'
sentence = "This is a sentence."
paragraph = """This is a paragraph. It is
made up of multiple lines and sentences."""
45
Comments in Python
• A hash sign (#) that is not inside a string literal begins a
comment. All characters after the # and up to the end of the
physical line are part of the comment and the Python
interpreter ignores them.
• #!/usr/bin/python
• # First comment
• print "Hello, Python!" # second comment
46
Comments in Python
• A hash sign (#) that is not inside a string literal begins a
comment. All characters after the # and up to the end of the
physical line are part of the comment and the Python
interpreter ignores them.
• #!/usr/bin/python
• # First comment
• print "Hello, Python!" # second comment
Comments in Python
• You can type a comment on the same line after a statement
or expression −
• # This is a comment.
• # This is a comment, too.
• # This is a comment, too.
• # I said that already.
48
Comments in Python
• You can type a comment on the same line after a statement
or expression −
• Following triple-quoted string is also ignored by Python
interpreter and can be used as a multiline comments:
'''
This is a multiline
comment.
'''
49