Essential Python
Copyright 2021 Kevin Vans-Colina
Thank you for downloading this ebook. This book remains the copyrighted property of the author, and may not be redistributed to others for commercial or non-commercial purposes. If you enjoyed this book, please encourage your friends to download their own copy from their favorite authorized retailer. Thank you for your support.
The author assumes no responsibility for errors, emissions or damages that result from using the information and programs in this book.
Published by Sorefoot Software 29/11/21
Distributed by Smashwords.
Contact: essentialpython@gmail.com
Table of Contents
Introduction
What is Python
Getting Python
Install Python
Setting Up the Path
Running Python
Python
Python Basics
Using the Interpreter Interactively
Using the Interpreter with a Script
Python Help
Python Syntax
Identifiers in Python
Program Layout
Comments
Variables
Immutability
Numeric Variables
int (signed integers)
float (floating point real values)
Complex (complex numbers)
Strings
String Methods
String Formatting
Variable Scope and Namespaces
Data Types
Lists
Tuple
Dictionaries
Data Type Conversion
Operators
Arithmetic Operators
Comparison Operators
Assignment Operators
Bitwise Operators
Logical Operators
Membership Operators
Identity Operators
Operator Precedence
Program Flow Control
While Loop
For Loop
Offsets and Items
Continue Statement
Break Statement
Loop Statements and Else
If Statement
if / else Ternary Expression
Functions
Function Definition
Default Function Arguments
Variable Length Arguments
Anonymous Functions
Map Function
Filter Function
Reduce Function
Modules
Module Reloads
Locating Module Files
Module Data Hiding
__name__ and __main__
Classes and Objects
Class Definition
Static methods
Inheritance
Function Overloading
Overloading Operators
Class attributes
Garbage Collection
Input and Output
Screen Output
Keyboard Input
Opening Files for Reading and Writing
Writing to Files
Reading from Files
Closing Files
Binary Files
Moving around Files
File Attributes
Managing Files and Directories
Exceptions
try - except
try finally
Exception Arguments
Raising Exceptions
Date and Time
Time Module
Formatted Time
DateTime Module
Calendar Module
Example Project Software Bug Tracker
Issue Class
Project Class
Command Line Arguments
Configuration Files
Databases
SQL
XML
Simple API for XML (SAX)
Document Object Module for XML (DOM)
HTML Parsing
Tkinter- GUI
Overview
Widget Layout
Pack Manager
Place Manager
Grid Manager
Simple Form
Practical Form
Message Boxes
Dialogs
File Dialog
Menus
Resizing the form
Scroll Bars
Where Next?
Appendix 1-Interpreter Command Line Options
Appendix Reserved Words
Appendix Bug Tracker Code
Issue.py
project.py
bugTracker.py
Appendix Simple Notepad
Appendix Simple Notepad
Introduction
This book is designed to give all the essential information to get you programming in Python. The book starts by running through the basics of the language before showing you how to use some of the powerful libraries included in Python, finally it will show you how to produce full applications with form based user interfaces.
What is Python
Python is an easy to learn programming language which particularly popular with the hobbyist it is also used by professional programs to write utilities. Python is an interpreted language which makes it a good choice for rapid application development.
Python has a clear defined syntax, a small set of keywords and a simple program structure, this allows the programmer to quickly learn how to program in Python. The simple syntax means that it is also easy to read which has the advantage that source code is easy to maintain.
Python has a large standard library this means that scripts can be used across multiple platforms without having compatibility issues. There are also a vast number of freely available libraries which allow you to use Python for virtually anything. Low level modules can easily be added to the Python interpreter by writing these modules in an compiled language such as C these modules run much faster. Finally GUIs can be generated in Python which can then be ported to many other operating systems.
Python also has support for objected orientated, structured and functional programming. It can be complied as byte code for larger programs or run as an interpreted language. It has high level dynamic data types and supports dynamic type checking. Memory management is taken care of automatically by Python.
Python is available on a wide variety of platforms including
Unix (Solaris, Linux, FreeBSD, AIX, HP/UX, SunOS, IRIX)
Win 9x/ NT /2000 / XP / Vista /Windows
Macintosh
OS/2
DOS
PalmOS
Windows CE
Acorn/RISB OS
BeOS
Amiga
Java virtual machine
.NET virtual machine
Getting Python
The best place to download a copy of Python is from the official web site which is http://www.Python.org. This is also a good source of documentation, news, current source code etc. This is probably the best place to get further information on keywords and libraries.
Install Python
If the binary for your platform is available then you can download it, if there is not a version for your platform then you can download the source code and compile it manually.
If you have downloaded the binary then it can simply be installed using the installer.
Setting Up the Path
To make life easier when working with Python it is worth setting up the path variable for your platform, the path variables tells the computer where to look for executable files in this case the Python interpreter.
Note the path in the following examples may vary so you might have to check where your copy of Python was installed.
Unix / Linux Path
Csh shell
setenv PATH $PATH:/usr/local/bin/Python
Bash shell (Linux)
Export PATH= $PATH:/usr/local/bin/Python
sh or ksh shell
PATH=$PATH:/usr/local/bin/Python
Windows Path
In the command prompt type
path %path%;C:\Python
Running Python
Python can be run interactively from the interpreter, from the command line using a script or from an IDE.