?> Wins - Course - Python
info@wins.com.ng +234 708 477 1951

Python

Free

Python is a MUST for students and working professionals to become a great Software Engineer especially when they are working in Web Development Domain. Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language. It was created by Guido van Rossum from 1985- 1990. Like Perl, Python source code is also available under the GNU General Public License (GPL). Python is named after a TV Show called ëMonty Pythonís Flying Circusí and not after Python-the snake. Python 3.0 was released in 2008. Although this version is supposed to be backward-incompatible, later on, many of its important features have been backported to be compatible with version 2.7. This tutorial gives enough understanding of Python 3 version programming language.

First Name*
Last Name*
Phone Number*

Overview

Why Learn Python 3?

Python is a high-level, interpreted, interactive and object-oriented scripting language. Python is designed to be highly readable. It uses English keywords frequently whereas other languages use punctuation, and it has fewer syntactical constructions than other languages. 

Python is a MUST for students and working professionals to become a great Software Engineer especially when they are working in Web Development Domain. I will list down some of the key advantages of learning Python:

  • Python is Interpreted − Python is processed at runtime by the interpreter. You do not need to compile your program before executing it. This is similar to PERL and PHP.

  • Python is Interactive − You can actually sit at a Python prompt and interact with the interpreter directly to write your programs.

  • Python is Object-Oriented − Python supports Object-Oriented style or technique of programming that encapsulates code within objects.

  • Python is a Beginner's Language − Python is a great language for the beginner-level programmers and supports the development of a wide range of applications from simple text processing to WWW browsers to games.

    Characteristics of Python

    Following are important characteristics of python −

  • It supports functional and structured programming methods as well as OOP.

  • It can be used as a scripting language or can be compiled to byte-code for building large applications.

  • It provides very high-level dynamic data types and supports dynamic type checking.

  • It supports automatic garbage collection.

  • It can be easily integrated with C, C++, COM, ActiveX, CORBA, and Java.


Hello, World using Python. 

Just to give you a little excitement about Python, I'm going to give you a small conventional Python Hello World program.

print "Hello, Python!"

Applications of Python 

As mentioned before, Python is one of the most widely used languages over the web. I'm going to list a few of them here:

  • 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.

  • A broad standard library − Python's bulk of the library is very portable and cross-platform compatible with UNIX, Windows, and Macintosh.

  • Interactive Mode − Python has support for an interactive mode that allows interactive testing and debugging of snippets of code.

  • Portable − Python can run on a wide variety of hardware platforms and has the same interface on all platforms.

  • 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.

  • Databases − Python provides interfaces to all major commercial databases.

  • 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.

  • Scalable − Python provides a better structure and support for large programs than shell scripting.


Audience

This tutorial is designed for software programmers who want to upgrade their Python skills to Python 3. This tutorial can also be used to learn Python programming language from scratch. 


Prerequisites

You should have a basic understanding of Computer Programming terminologies. A basic understanding of any of the programming languages is a plus.



The __future__ module

Python 3.x introduced some Python 2-incompatible keywords and features that can be imported via the in-built __future__ module in Python 2. It is recommended using __future__ imports, if you are planning Python 3.x support for your code.

For example, if we want Python 3.x's integer division behavior in Python 2, add the following import statement.

from __future__ import division


The print Function

The most notable and most widely known change in Python 3 is how the print function is used. The use of parenthesis () with print function is now mandatory. It was optional in Python 2.

print "Hello World" #is acceptable in Python 2
print ("Hello World") # in Python 3, print must be followed by ()

The print() function inserts a new line at the end, by default. In Python 2, it can be suppressed by putting ',' at the end. In Python 3, "end =' '"append space instead of a newline.

print x,           # Trailing comma suppresses newline in Python 2
print(x, end=" ")  # Appends a space instead of a newline in Python 3


Reading Input from Keyboard

Python 2 has two versions of input functions, input() and raw_input(). The input() function treats the received data as a string if it is included in quotes '' or ", otherwise the data is treated as a number.


In Python 3, raw_input() function is deprecated. Further, the received data is always treated as a string.

In Python 2

>>> x = input('something:') 
something:10 #entered data is treated as number
>>> x
10

>>> x = input('something:')
something:'10' #entered data is treated as string
>>> x
'10'

>>> x = raw_input("something:")
something:10 #entered data is treated as string even without ''
>>> x
'10'

>>> x = raw_input("something:")
something:'10' #entered data treated as string including ''
>>> x
"'10'"

In Python 3

>>> x = input("something:")
something:10
>>> x
'10'

>>> x = input("something:")
something:'10' #entered data treated as string with or without ''
>>> x
"'10'"

>>> x = raw_input("something:") # will result NameError
Traceback (most recent call last):
   File "<pyshell#3>", line 1, in 
  <module>
   x = raw_input("something:")
NameError: name 'raw_input' is not defined


Integer Division

In Python 2, the result of the division of two integers is rounded to the nearest integer. As a result, 3/2 will show 1. In order to obtain a floating-point division, numerator or denominator must be explicitly used as a float. Hence, either 3.0/2 or 3/2.0 or 3.0/2.0 will result in 1.5

Python 3 evaluates 3 / 2 as 1.5 by default, which is more intuitive for new programmers.


Unicode Representation

Python 2 requires you to mark a string with u if you want to store it as Unicode.

Python 3 stores strings as Unicode, by default. We have Unicode (UTF-8) strings, and 2-byte classes: byte and byte arrays.


xrange() Function Removed

In Python 2 range() returns a list, and xrange() returns an object that will only generate the items in the range when needed, saving memory.

In Python 3, the range() function is removed, and xrange() has been renamed as range(). In addition, the range() object supports slicing in Python 3.2 and later.

raise exception

Python 2 accepts both notations, the 'old' and the 'new' syntax; Python 3 raises a SyntaxError if we do not enclose the exception argument in parenthesis.

raise IOError, "file error" #This is accepted in Python 2
raise IOError("file error") #This is also accepted in Python 2
raise IOError, "file error" #syntax error is raised in Python 3
raise IOError("file error") #this is the recommended syntax in Python 3


Arguments in Exceptions

In Python 3, arguments to exception should be declared with 'as' keyword.

except Myerror, err: # In Python2
except Myerror as err: #In Python 3


next() Function and .next() Method

In Python 2, next() as a method of generator object, is allowed. In Python 2, the next() function, to iterate over generator object, is also accepted. In Python 3, however, next(0 as a generator method is discontinued and raise.


Attribute Error.

gen = (letter for letter in 'Hello World') # creates generator object
next(my_generator) #allowed in Python 2 and Python 3
my_generator.next() #allowed in Python 2. raises AttributeError in Python 3


2to3 Utility

Along with Python 3 interpreter, 2to3.py script is usually installed in the tools/scripts folder. It reads Python 2.x source code and applies a series of fixers to transform it into a valid Python 3.x code.

Here is a sample Python 2 code (area.py):

def area(x,y = 3.14): 
   a = y*x*x
   print a
   return a

a = area(10)
print "area",a

To convert into Python 3 version:

$2to3 -w area.py

Converted code :

def area(x,y = 3.14): # formal parameters
   a = y*x*x
   print (a)
   return a

a = area(10)
print("area",a)


Python 3 - Environment Setup

Python 3 is available for Windows, Mac OS and most of the flavors of Linux operating system. Even though Python 2 is available for many other OSs, Python 3 support either has not been made available for them or has been dropped.


Local Environment Setup

Open a terminal window and type "python" to find out if it is already installed and which version is installed.


Getting Python

Windows platform

Binaries of the latest version of Python 3 (Python 3.5.1) are available on this download page

The following different installation options are available.

  • Windows x86-64 embeddable zip file
  • Windows x86-64 executable installer
  • Windows x86-64 web-based installer
  • Windows x86 embeddable zip file
  • Windows x86 executable installer
  • Windows x86 web-based installer

Note − In order to install Python 3.5.1, minimum OS requirements are Windows 7 with SP1. For versions 3.0 to 3.4.x Windows XP is acceptable.


Linux platform

Different flavors of Linux use different package managers for the installation of new packages.

On Ubuntu Linux, Python 3 is installed using the following command from the terminal.

$sudo apt-get install python3-minimal

Installation from source

Download Gzipped source tarball from Python's download URL − https://www.python.org/ftp/python/3.5.1/Python-3.5.1.tgz

Extract the tarball
tar xvfz Python-3.5.1.tgz
Configure and Install:
cd Python-3.5.1
./configure --prefix = /opt/python3.5.1
make  
sudo make install


Mac OS

Download Mac OS installers from this URL − https://www.python.org/downloads/mac-osx/

  • Mac OS X 64-bit/32-bit installer − python-3.5.1-macosx10.6.pkg
  • Mac OS X 32-bit i386/PPC installer − python-3.5.1-macosx10.5.pkg

Double click this package file and follow the wizard instructions to install.

The most up-to-date and current source code, binaries, documentation, news, etc., is available on the official website of Python −


Python Official Websitehttps://www.python.org/

You can download Python documentation from the following site. The documentation is available in HTML, PDF and PostScript formats.


Python Documentation Websitewww.python.org/doc/


Setting up PATH

Programs and other executable files can be in many directories. Hence, the operating systems provide a search path that lists the directories that it searches for executables.

The important features are −

  • 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.

  • 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.


Setting Path at Unix/Linux

To add the Python directory to the path for a particular session in Unix −

  • In the csh shell − type setenv PATH "$PATH:/usr/local/bin/python3" and press Enter.

  • In the bash shell (Linux) − type export PYTHONPATH=/usr/local/bin/python3.4 and press Enter.

  • In the sh or ksh shell − type PATH = "$PATH:/usr/local/bin/python3" and press Enter.

Note − /usr/local/bin/python3 is the path of the Python directory.


Setting Path at Windows

To add the Python directory to the path for a particular session in Windows −

  • At the command prompt − type path %path%;C:\Python and press Enter.

Note − C:\Python is the path of the Python directory


Python Environment Variables

Here are important environment variables, which are recognized by Python −

Sr.No. Variable & Description
1

PYTHONPATH

It has a role similar to PATH. This variable tells the Python interpreter where to locate the module files imported into a program. It should include the Python source library directory and the directories containing Python source code. PYTHONPATH is sometimes preset by the Python installer.

2

PYTHONSTARTUP

It contains the path of an initialization file containing Python source code. It is executed every time you start the interpreter. It is named as .pythonrc.py in Unix and it contains commands that load utilities or modify PYTHONPATH.

3

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

Here is the list of all the available command-line options −

Sr.No. Option & Description
1

-d

provide debug output

2

-O

generate optimized bytecode (resulting in .pyo files)

3

-S

do not run import site to look for Python paths on startup

4

-v

verbose output (detailed trace on import statements)

5

-X

disable class-based built-in exceptions (just use strings); obsolete starting with version 1.6

6

-c cmd

run Python script sent in as cmd string

7

file

run Python script from given file

Script from the Command-line

A Python script can be executed at the command line by invoking the interpreter on your application, as shown in the following example.

$python  script.py          # Unix/Linux
or
python% script.py           # Unix/Linux
or 
C:>python script.py         # Windows/DOS

Note − Be sure the file permission mode allows execution.


Integrated Development Environment

You can run Python from a Graphical User Interface (GUI) environment as well, if you have a GUI application on your system that supports Python.

  • Unix − IDLE is the very first Unix IDE for Python.

  • WindowsPythonWin is the first Windows interface for Python and is an IDE with a GUI.

  • Macintosh − The Macintosh version of Python along with the IDLE IDE is available from the main website, downloadable as either MacBinary or BinHex'd files.

If you are not able to set up the environment properly, then you can take the help of your system admin. Make sure the Python environment is properly set up and working perfectly fine.

Note − All the examples given in subsequent chapters are executed with Python 3.4.1 version available on Windows 7 and Ubuntu Linux.

We have already set up Python Programming environment online, so that you can execute all the available examples online while you are learning theory. Feel free to modify any example and execute it online.

It is used in Windows to instruct Python to find the first case-insensitive match in an import statement. Set this variable to any value to activate it.

4

PYTHONHOME

It is an alternative module search path. It is usually embedded in the PYTHONSTARTUP or PYTHONPATH directories to make switching module libraries easy.



PYTHONCASEOK




Clems Abak

Web Developer

Clems Abak is a Web Developer with 15 years experience and has developed critical web applications that been used in the industry across the world.

This course has no review yet

0 ITEMS
₦ 0