A Byte of Python

A Byte of Python

Swaroop C H

www.byteofpython.info

Version 1.20

This book is released under the Creative Commons Attribution-NonCommercial-ShareAlike License 2.0 .

Abstract

This book will help you to learn the Python programming language, whether you are new to computers or are an experienced programmer.

To read the latest version of this book, please see www.swaroopch.com/notes/Python.


Table of Contents

Preface
Who This Book Is For
History Lesson
Status of the book
Official Website
License Terms
Feedback
Something To Think About
1. Introduction
Introduction
Features of Python
Summary
Why not Perl?
What Programmers Say
2. Installing Python
For Linux/BSD users
For Windows Users
Summary
3. First Steps
Introduction
Using the interpreter prompt
Choosing an Editor
Using a Source File
Output
How It Works
Executable Python programs
Getting Help
Summary
4. The Basics
Literal Constants
Numbers
Strings
Variables
Identifier Naming
Data Types
Objects
Output
How It Works
Logical and Physical Lines
Indentation
Summary
5. Operators and Expressions
Introduction
Operators
Operator Precedence
Order of Evaluation
Associativity
Expressions
Using Expressions
Summary
6. Control Flow
Introduction
The if statement
Using the if statement
How It Works
The while statement
Using the while statement
The for loop
Using the for statement
The break statement
Using the break statement
The continue statement
Using the continue statement
Summary
7. Functions
Introduction
Defining a Function
Function Parameters
Using Function Parameters
Local Variables
Using Local Variables
Using the global statement
Default Argument Values
Using Default Argument Values
Keyword Arguments
Using Keyword Arguments
The return statement
Using the literal statement
DocStrings
Using DocStrings
Summary
8. Modules
Introduction
Using the sys module
Byte-compiled .pyc files
The from..import statement
A module's __name__
Using a module's __name__
Making your own Modules
Creating your own Modules
from..import
The dir() function
Using the dir function
Summary
9. Data Structures
Introduction
List
Quick introduction to Objects and Classes
Using Lists
Tuple
Using Tuples
Tuples and the print statement
Dictionary
Using Dictionaries
Sequences
Using Sequences
References
Objects and References
More about Strings
String Methods
Summary
10. Problem Solving - Writing a Python Script
The Problem
The Solution
First Version
Second Version
Third Version
Fourth Version
More Refinements
The Software Development Process
Summary
11. Object-Oriented Programming
Introduction
The self
Classes
Creating a Class
object Methods
Using Object Methds
The __init__ method
Using the __init__ method
Class and Object Variables
Using Class and Object Variables
Inheritance
Using Inheritance
Summary
12. Input/Output
Files
Using file
Pickle
Pickling and Unpickling
Summary
13. Exceptions
Errors
Try..Except
Handling Exceptions
Raising Exceptions
How To Raise Exceptions
Try..Finally
Using Finally
Summary
14. The Python Standard Library
Introduction
The sys module
Command Line Arguments
More sys
The os module
Summary
15. More Python
Special Methods
Single Statement Blocks
List Comprehension
Using List Comprehensions
Receiving Tuples and Lists in Functions
Lambda Forms
Using Lambda Forms
The exec and eval statements
The assert statement
The repr function
Summary
16. What Next?
Graphical Software
Summary of GUI Tools
Explore More
Summary
A. Free/Libré and Open Source Software (FLOSS)
B. About
Colophon
About the Author
C. Revision History
Timestamp

List of Tables

5.1. Operators and their usage
5.2. Operator Precedence
15.1. Some Special Methods

List of Examples

3.1. Using the python interpreter prompt
3.2. Using a Source File
4.1. Using Variables and Literal constants
5.1. Using Expressions
6.1. Using the if statement
6.2. Using the while statement
6.3. Using the for statement
6.4. Using the break statement
6.5. Using the continue statement
7.1. Defining a function
7.2. Using Function Parameters
7.3. Using Local Variables
7.4. Using the global statement
7.5. Using Default Argument Values
7.6. Using Keyword Arguments
7.7. Using the literal statement
7.8. Using DocStrings
8.1. Using the sys module
8.2. Using a module's __name__
8.3. How to create your own module
8.4. Using the dir function
9.1. Using lists
9.2. Using Tuples
9.3. Output using tuples
9.4. Using dictionaries
9.5. Using Sequences
9.6. Objects and References
9.7. String Methods
10.1. Backup Script - The First Version
10.2. Backup Script - The Second Version
10.3. Backup Script - The Third Version (does not work!)
10.4. Backup Script - The Fourth Version
11.1. Creating a Class
11.2. Using Object Methods
11.3. Using the __init__ method
11.4. Using Class and Object Variables
11.5. Using Inheritance
12.1. Using files
12.2. Pickling and Unpickling
13.1. Handling Exceptions
13.2. How to Raise Exceptions
13.3. Using Finally
14.1. Using sys.argv
15.1. Using List Comprehensions
15.2. Using Lambda Forms