Rapid Python Programming

Chapter 1

Introduction The Python programming community is rapidly growing. More people are either migrating to python or choosing it as their first high level programming language. It’s multiple programming paradigms feature make it an easier first language to learn, because it frees you from focusing on the language itself, and more on problem solving. Python is highly portable for multiple operating systems, including embedded systems. It’s also ‘‘Open Source’’ and can be distributed freely.

This book can be used for the experienced programmer, or absolute beginner. The complexity of the language gradually progresses, but the concepts will remain simple and easy to understand. Readers are not restricted to any previous specific concept. All concepts and examples can be easily modified and re-used. You will be able to play with the advanced features of python quickly. The job market is putting a higher demand for fast turnaround on development for new products. It’s also becoming a heavy hitter in the game market for its use throughout the game development cycle such as rendering, parsing, statistics and core development. Python can be easily extended using C/C++ for speed and performance. It’s used extensively in Academia for Science, Math, Engineering, simulations, and other computation experiments. Python’s greatest selling point is the vast libraries of pre-written tools. Below is a highlight of tools which comprises approximately 10% of what actually exists.

What makes this book unique is the transition from beginner to intermediate programming. The transition will occur several times throughout the book, when introducing a new topic such as web applications and GUI. You will revisit beginning aspects of programming to simplify learning a new module, instead of assuming you are already proficient in the previous techniques discussed. Most importantly you will be instructed in the ability to convert code to other programming styles and technique; this will make it easy to adopt any style or method when re-using reusable code. Python also has an enhancement program known as PEP (Python Enhancement Program), which contains an index of potential proposals to further enhance the python programming language. Python is evolving towards 3.x which includes minor changes with regards to backwards compatibility. A chapter at the end of the book will be devoted to compatibility changes and migration.

1.1.1 History

Python was created in the late 1980s by Guido van Rossum at CWI in the Netherlands and was a successor to the ABC programming language. Python was re-designed to be a multi-paradigm programming language making it possible for programmers to adopt multiple styles of programming such as object-oriented programming, structured, functional, aspect-oriented programming and meta programming.

1.1.2 Installation

It is highly recommended that you use Linux as your primary platform to learn and use python. However you are not restricted. Nearly everything can be done in Microsoft Windows™ or Apple Macintosh™ with few troubles.

1.1.3 Windows

Visit http://www.python.org/download and you will find an installer for windows. Select all installation components. If you wish to use Python in Microsoft Windows™ on the command line, then you need to set the PATH variable appropriately. For Windows™ 2000, XP, 2003, Vista, 2008, etc... click on Control Panel -> System -> Advanced -> Environment variables. Click on the variable named PATH in the “System Variables” section, then select: Edit and add; C:\Python27 to the end of the line. Please choose proper directory name. For instance: it could be C:\Python2.7x

1.1.4 Linux

Most Linux distributions come with Python installed by default. However you may need to install additional packages such as the documentation. For example, Knoppix or Debian run the following:

sudo apt-get install python27-doc

By selecting the defaults, you will have installed python core files including documentation, such as, interactive help and manual pages. You can verify the version of python via the command line using “python –V” as shown below.

$ python –V
Python 2.7.9

1.1.5 Editors

Choosing an editor can be very crucial for the successful development of your scripts. You have to be completely comfortable with your editor and have full knowledge of how it writes files. For instance, scripts written on Microsoft Windows™ Notepad can have several issues when you port the files to a Linux or BSD operating system. Those files might have carriage return issues Control-M (^M) on each statement. You would have to manually remove them on each statement or do a file wide search and replace which can be tricky because you need an escape code to find it. The escape code is (x0D) or (\x0D). However you can avoid these issues by using a compatible editor.

1.1.6 Case Sensitive

You might have noticed Microsoft Windows™ is not case sensitive. However, on a Unix variant operating system or Linux, you will need to be more specific when executing scripts. For example, typing “python” at the command prompt would list the following: Note: To exit out of the interpreter press "Ctrl-D" followed by enter or type “exit()”.

$ python
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Uppercase Python would reveal an error as follows:
$ Python -bash: Python: command not found

If you are using python in iOS, Apple accepts the uppercase “Python”, and you will not see an error.

1.1.7 Additional Packages

You will learn how to download the following packages in later chapters as you need them:

1.1.8 Versions

At the beginning of this project all applications were compiled using python version 2.7. Before completion of this project all applications were compiled and tested with Python version 2.8. The code will work in Python 3.x with minor changes such as: print statement. Almost everything here is backwards compatible and will translate easily into Python 3 if you familiarize yourself with the minor syntactical changes. Don’t let the version of python distract you. The changes are often very minor. See the two following examples below:

1.1.9 Installation and Configuration

It is highly recommended that you choose an Operating System that you are proficient using. It is assumed that you have a clear understanding of your Operating System and how to execute programs via command line.

1.1.10 Python Interactive Prompt

For Microsoft Windows™users, Python interactive interpreter can be executed via the DOS prompt or through the ↓“IDLE”. (Interactive Development Environment) program.

To start the interactive interpreter enter "python" at the command or shell prompt in Windows or xterm in Linux or Terminal in iOS. The following information should appear:

knoppix@Microknoppix:~$ python
Python 2.5.2 (r252:60911, Jan 4 2009, 17:40:26)[GCC 4.3.2]
on linux2 Type "help", "copyright", "credits" or "license"
for more information.

You can immediately begin typing your program >>> print ’I am learning python’ I am learning python >>>

Please note that python is case sensitive. Print will give you an invalid syntax error.

>>> Print ’hello’
File "<stdin>", line 1
Print ’hello’

SyntaxError: invalid syntax >>>

To exit out of the interpreter press "Ctrl-D" followed by enter or type “exit()”.

1.1.11 Making a Python Script

You can create a script using an editor. If you are using Microsoft Windows™ I suggest you use IDLE or editpad to create your script. You may need to download editpad from: http://www.editpadpro.com. If you are using Linux, I suggest using ↓VIM as your editor or Sublime for iOS. Open your editor, create a new file and type the following into the editor:

print ’I am learning python’

Save the file as first.py Once the file is saved you can type the following on the command line or terminal:

knoppix@Microknoppix:~$ python first.py
I am learning python

A proper script would include the path to the interpreter, a description, the name of the file and other files that might be involved. The path to the interpreter is not needed while using python on a windows™ operating system.

# program name first.py
print ’I am learning python’

Edit the file with the information in the figure above, then Save and Close the file. If you are on a Linux system, you will need to change the file permissions to make it an executable program.

knoppix@Microknoppix:~$ chmod a+x fist.py

You can then execute the program as follows:

knoppix@Microknoppix:~$ ./first.py
I am learning python

Notice the #! hash or number symbol and exclamation. It’s known in Unix or Linux as “explode” and “bang” or “sha-bang” which is the key to execute the shell or interpreter. Anything after the first line is considered either code or comments. Comments are designated using the hash (#) symbol. On Windows™ the path to the Python interpreter will be, for example, c:\python27.x\python.exe. You can also omit (#!) on first line.

Python’s built-in interpreter also has a nice interactive feature known as help(). To start the interactive interpreter enter "python" at the command or shell prompt:

knoppix@Microknoppix:~$ python
Python 2.5.2 (r252:60911, Jan 4 2009, 17:40:26) [GCC 4.3.2] on
linux2 Type "help", "copyright", "credits" or "license"
for more information.
>>> help()
Welcome to Python 2.7! This is the online help utility.
If this is your first time using Python, you should definitely
check out the tutorial on the Internet at
Enter the name of any module, keyword, or topic to get help on
writing Python programs and using Python modules. To quit
this help utility and return to the interpreter, just type "quit".
To get a list of available modules, keywords, or topics, type
"modules", "keywords", or "topics". Each module also comes
with a one-line summary of what it does; to list the modules
whose summaries contain a given word such as "spam", type
"modules spam".

To exit out of help mode type ’q’ and it will put you back into the interpreter mode >>>. With this interactive help you can learn about almost anything in Python, for example:

>>> help(’str’)

Will print out the documentation for “str” or string. To get a list of modules, or keywords, type ’modules’. It will take a minute to compile but will provide a list of modules. If you wish to learn about a specific module, pick one of the two formats shown below.

>>> help(’module-name’) or
help> module-name.

1.1.12 Summary

In this chapter you have learned how to use the interactive interpreter, how to create and execute scripts, and how to activate the interactive help module.

1.1.13 Exercises

  1. Go through the examples above using the interactive interpreter.
  2. Create a script to print out a list of all modules and redirect that list to a file called “python-modules.txt”.
  3. Create another script to print the print module and redirect to file called “python-print.txt”.

To post a comment you need to login first.