Foreword
- Who is this book for?
- About the author
- Keep in touch
Introduction
- What is Python
- History of Python
- Zen of Python
1Getting started with Python
- 1.1Installing Python
- 1.1.1Windows
- 1.1.2Linux
- 1.2Running IDLE
- 1.3Simple maths in Python
- 1.4Variables
- 1.4.1Calculations with variables
- 1.4.2Changing the value of a variable
- 1.4.3Variable names
- 1.5Strings
- 1.5.1Strings in variables
- 1.6Console shortcuts
2Hello world
- 2.1Writing a program
- 2.1.1Running the IDLE console window
- 2.1.2Opening an edit window
- 2.1.3Typing in your code
- 2.1.4Type, don’t copy and paste
- 2.1.5Running your code
- 2.1.6Seeing the result
- 2.2In case of errors
- 2.3Print statements - outputting text
- 2.4Sequencing
- 2.5Input statements - getting user input
- 2.5.1Inputting a number
- 2.6Selection
- 2.6.1Indentation
- 2.7Iteration
- 2.8Comments
3Variables and values
- 3.1Creating and using variables
- 3.2Variables can store any type of data
- 3.3Naming variables
- 3.3.1Legal variable names
- 3.3.2Reserved words
- 3.3.3Choosing variable names
- 3.3.4Naming conventions
- 3.3.5Unicode characters
- 3.4Variables point to values
- 3.5None
- 3.6Multiple assignments
4Numbers and maths
- 4.1Types of number
- 4.1.1Integers
- 4.1.2Floating point numbers
- 4.1.3Finding the type
- 4.2Basic maths operations
- 4.2.1Addition
- 4.2.2Int and float results
- 4.2.3Subtraction
- 4.2.4Multiplication
- 4.2.5Division
- 4.2.6Power
- 4.2.7Floor divide
- 4.2.8Modulo
- 4.2.9Unary operators
- 4.2.10Augmented assignment
- 4.3Precedence and brackets
- 4.3.1Statements and expressions
- 4.4Built-in maths functions
- 4.4.1abs()
- 4.4.2min() and max()
- 4.4.3int()
- 4.4.4float()
- 4.5math module
- 4.6Limitations of floats
- 4.7More advanced topics
- 4.7.1Scientific notation for floats
- 4.7.2Complex numbers
5Strings
- 5.1Creating strings
- 5.2String type
- 5.3Operations on strings
- 5.3.1Methods
- 5.4Converting between strings and other data types
- 5.5How to use special characters in a string
- 5.5.1Quote characters
- 5.5.2Unicode characters
- 5.5.3Newline characters
- 5.5.4The escape character \
6Loops
- 6.1For loops
- 6.1.1The loop variable
- 6.2The range function
- 6.2.1Changing the start value
- 6.2.2Using a step value
- 6.3For loop example - printing a times table
- 6.4While loops
- 6.5While loop example - getting user input
- 6.6Nested loops
7Input and output
- 7.1Input prompts
- 7.2Inputting numbers
- 7.3Print
- 7.4Print separators
8If statements
- 8.1if statements
- 8.1.1Example if statement
- 8.2Comparison operators
- 8.3Else statement
- 8.4Elif statement
- 8.5Compound conditions
- 8.6Comparison operator chaining
- 8.7Precedence
9Lists
- 9.1What is a list?
- 9.1.1Lists vs arrays
- 9.2Creating lists
- 9.2.1Empty lists
- 9.2.2The list function
- 9.3The list type
- 9.4Accessing list elements
- 9.4.1Reading an element
- 9.4.2Changing an element
- 9.5Operations on lists
- 9.6Looping over a list
- 9.6.1Doing calculations in a loop
- 9.7List functions
- 9.7.1Finding the length of the list
- 9.7.2Finding the
minamaxvalues - 9.7.3Adding up the values in a list
- 9.8List methods
- 9.8.1Adding elements
- 9.8.2Searching for elements in a list
- 9.8.3Removing elements
- 9.8.4Other methods
- 9.9Two-dimensional lists
- 9.9.1Ragged arrays
- 9.10Aliasing
- 9.10.1Reassigning variables
- 9.10.2Does aliasing affect numbers?
- 9.11Examples
- 9.11.1Finding every element of a given value
- 9.11.2Creating a 2-dimensional list of zeros
10Using functions
- 10.1Why use functions?
- 10.2Built-in functions
- 10.2.1repr
- 10.2.2chr and ord
- 10.2.3help and dir
- 10.3Importing modules
- 10.3.1Simple import statement
- 10.3.2Importing individual functions
- 10.3.3Aliasing a module
- 10.3.4Importing all functions (don’t do this)
- 10.4How to call functions
- 10.4.1Optional parameters
- 10.4.2Variable number of arguments
- 10.4.3Named optional parameters
- 10.5Defining your own functions
- 10.5.1Printing @s
- 10.5.2Printing more @s
- 10.5.3Returning a value
11More loops
- 11.1The break operator
- 11.1.1Break in a for loop
- 11.1.2Break in a while loop
- 11.2The continue operator
- 11.3Using else with a loop
- 11.4Nested loop statements
- 11.4.1Printing times tables
- 11.5Modifying a for loop
- 11.6Looping in reverse order
- 11.6.1Using range() to count backwards - ugly
- 11.6.2reversed()
- 11.7sorted()
- 11.8Looping over multiple items
- 11.8.1zip
- 11.8.2How zip() works
- 11.9More about zip()
- 11.10Accessing the loop count using enumerate
- 11.11Looping over selected items
- 11.11.1Using filter
- 11.11.2The advantage of using filter
12Programming logic
- 12.1Boolean types
- 12.2Comparison operators and their opposites
- 12.3Boolean operations and De Morgan’s Laws
- 12.4Ternary operators
- 12.5Comparing containers
- 12.5.1Strings
- 12.5.2Lists
- 12.5.3Other containers
- 12.6Membership testing
- 12.7Identity testing
- 12.8Truthy values
- 12.8.1Numbers
- 12.8.2Strings
- 12.8.3Lists
- 12.8.4Other collections
- 12.8.5None
- 12.8.6Other objects
- 12.9Short circuit evaluation
- 12.9.1Short-circuiting with the or operator
- 12.9.2Short-circuiting with the and operator
- 12.9.3Short-circuiting to avoid errors
- 12.9.4The value of an or/and expression
- 12.9.5Getting a true or false value
13Slices
- 13.1Slicing a list
- 13.2Using negative indices
- 13.3Delete or replace a slice of a list
- 13.4Slicing tuples and strings
- 13.5Loop over a slice
- 13.6Using steps
14More strings
- 14.1Raw strings
- 14.2Multi-line strings
- 14.3Looping over a string
- 14.4Joining strings
- 14.5Splitting a string
- 14.6Formatting strings
- 14.7The in operator
- 14.8Other string methods
- 14.8.1Changing case
- 14.8.2Dealing with whitespace
- 14.8.3Padding
- 14.8.4Search
- 14.8.5Splitting strings
- 14.8.6Categorising strings
15Tuples
- 15.1What is a tuple?
- 15.2Creating a tuple
- 15.3Accessing elements
- 15.4Packing and unpacking tuples
- 15.4.1Packing a tuple
- 15.4.2Unpacking a tuple
- 15.4.3Real-life examples
- 15.5Tuple vs list operations
- 15.5.1Tuple slices
- 15.5.2Adding elements to a tuple
- 15.5.3Finding elements
- 15.5.4Removing elements
- 15.5.5Looping
- 15.5.6In case of emergency
- 15.6Pros and cons of immutability
16Exceptions
- 16.1Program errors
- 16.2What are exceptions
- 16.3Exception types
- 16.3.1ImportError
- 16.3.2IndexError
- 16.3.3TypeError
- 16.3.4ValueError
- 16.3.5ZeroDivisionError
- 16.4Catching exceptions
- 16.4.1Only catch exceptions you can handle
- 16.4.2Accessing the exception message
- 16.5Using else with exceptions
- 16.6Using finally with exceptions
- 16.7Throwing exceptions
17Working with files
- 17.1Using files
- 17.1.1Opening the file
- 17.1.2Reading and writing data
- 17.1.3Closing the file
- 17.2Reading data
- 17.2.1Code for reading a file
- 17.2.2Reading lines from a file
- 17.2.3Looping over the lines in a file
- 17.3Writing data
- 17.3.1Code to write a file
- 17.3.2The write function
- 17.4Using with statements
- 17.5CSV data
- 17.5.1Reading CSV data
- 17.5.2Trying the code
- 17.5.3Formatting the output
- 17.5.4Writing CSV data
- 17.5.5Understanding the code
- 17.5.6Trying the code
18More books from this author
- 18.1Numpy Recipes
- 18.2Computer Graphics in Python with Pycairo
- 18.3Functional Programming in Python