Skip to main content

2019 | Book

A Beginners Guide to Python 3 Programming


About this book

This textbook on Python 3 explains concepts such as variables and what they represent, how data is held in memory, how a for loop works and what a string is. It also introduces key concepts such as functions, modules and packages as well as object orientation and functional programming. Each section is prefaced with an introductory chapter, before continuing with how these ideas work in Python.

Topics such as generators and coroutines are often misunderstood and these are explained in detail, whilst topics such as Referential Transparency, multiple inheritance and exception handling are presented using examples.

A Beginners Guide to Python 3 Programming provides all you need to know about Python, with numerous examples provided throughout including several larger worked case studies illustrating the ideas presented in the previous chapters.

Table of Contents

1. Introduction

Python is a general-purpose programming language in a similar vein to other programming languages that you might have heard of such as C++, JavaScript or Microsoft’s C# and Oracle’s Java.

John Hunt
2. Setting Up the Python Environment

In this chapter we will check to see if you have Python installed on your computer. If you do not have Python installed we will step through the process of installing Python. This is necessary because when you run a Python program it looks for the python interpreter that is used to execute your program or script. Without the python interpreter installed on your machine Python programs are just text files!

John Hunt
3. A First Python Program

In this chapter we will return to the Hello World program and look at what it is doing. We will also modify it to become more interactive and will explore the concept of Python variables.

John Hunt
4. Python Strings

In the previous chapter we used strings several times, both as prompts to the user and as output from the print() function. We even had the user type in their name and store it in a variable that could be used to access this name at a later point in time. In this chapter we will explore what a string is and how you can work with and manipulate them.

John Hunt
5. Numbers, Booleans and None

In this chapter we will explore the different ways that numbers can be represented by the built-in types in Python. We will also introduce the Boolean type used to represent True and False . As part of this discussion we will also look at both numeric and assignment operators in Python. We will conclude by introducing the special value known as None .

John Hunt
6. Flow of Control Using If Statements

In this chapter we are going to look at the if statement in Python. This statement is used to control the flow of execution within a program based on some condition. These conditions represent some choice point that will be evaluated to True or False . To perform this evaluation it is common to use a comparison operator (for example to check to see if the temperature is greater than some threshold). In many cases these comparisons need to take into account several values and in these situations logical operators can be used to combine two or more comparison expressions together.

John Hunt
7. Iteration/Looping

In this section we will look at the while loop and the for loop available in Python. These loops are used to control the repeated execution of selected statements.

John Hunt
8. Number Guessing Game

In this chapter we are going to bring everything we have learned so far together to create a simple number guessing game.

John Hunt
9. Recursion

Recursion is a very powerful way to implement solutions to a certain class of problems. This class of problems is one where the overall solution to a problem can be generated by breaking that overall problem down into smaller instances of the same problem. The overall result is then generated by combining together the results obtained for the smaller problems.

John Hunt
10. Introduction to Structured Analysis

In the preceding chapters what we have seen is typical of the procedural approach to programming. In the next chapter we will begin to explore the definition of functions which allow a more modular style of programming.

John Hunt
11. Functions in Python

As discussed in the last chapter; when you build an application of any size you will want to break it down into more manageable units; these units can then be worked on separately, tested and maintained separately. One way in which these units can be defined is as Python functions.

John Hunt
12. Scope and Lifetime of Variables

We have already defined several variables in the examples we have being working with in this book. In practice, most of these variables have been what are known as global variables. That is there are (potentially) accessible anywhere (or globally) in our programs.

John Hunt
13. Implementing a Calculator Using Functions

In this chapter we will step through the development of another Python program; this time the program will be to provide a simple calculator which can be used to add, subtract, multiple and divide numbers. The implementation of the calculator is base on the Function Decomposition performed earlier in the book in the Introduction to Structured Analysis chapter.

John Hunt
14. Introduction to Functional Programming

There has been much hype around Functional Programming in recent years. However, Functional Programming is not a new idea and indeed goes right back to the 1950s and the programming language LISP. However, many people are not clear as to what Functional Programming is and instead jump into code examples and never really understand some of the key ideas associated with Functional Programming such as Referential Transparency.

John Hunt
15. Higher Order Functions

In this chapter we will explore the concept of high-order functions. These are functions that take as a parameter, or return (or both), a function. To do this we will first look into how Python represents functions in memory and explore what actually happens when we execute a Python function.

John Hunt
16. Curried Functions

Currying is a technique which allows new functions to be created from existing functions by binding one or more parameters to a specific value. It is a major source of reuse of functions in Python which means that functionality can be written once, in one place and then reused in multiple other situations.

John Hunt
17. Introduction to Object Orientation

This chapter introduces the core concepts in Object Orientation. It defines the terminology used and attempts to clarify issues associated with objects. It also discusses some of the perceived strengths and weaknesses of the object-oriented approach. It then offers some guidance on the approach to take in learning about objects.

John Hunt
18. Python Classes

In Python everything is an object and as such is an example of a type or class of things. For example, integers are an example of the int class, real numbers are examples of the float class etc.

John Hunt
19. Class Side and Static Behaviour

Python classes can hold data and behaviour that is not part of an instance or object; instead they are part of the class.

John Hunt
20. Class Inheritance

Inheritance is a core feature of Object-Oriented Programming. It allows one class to inherit data or behaviour from another class and is one of the key ways in which reuse is enabled within classes.

John Hunt
21. Why Bother with Object Orientation?

The pervious four chapters have introduced the basic concepts behind object orientation, the terminology and explored some of the motivation. This chapter looks at how object orientation addresses some of the issues that have been raised with procedural languages. To do this it looks at how a small extract of a program might be written in a language such as C, considers the problems faced by the C developer and then looks at how the same functionality might be achieved in an object-oriented language such as Python. Do not worry too much about the syntax you will be presented with; it is mostly a form of pseudo code and it should not detract from the legibility of the examples.

John Hunt
22. Operator Overloading

We will explore Operator Overloading in this chapter; what it is, how it works and why we want it.

John Hunt
23. Python Properties

Many object-oriented languages have the explicit concept of encapsulation; that is the ability to hide data within an object and only to provide specific gateways into that data. These gateways are methods defined to get or set the value of an attribute (often referred to as getters and setters). This allows more control over access to the data; for example, it is possible to check that only a positive integer above zero, but below 120, is used for a person’s age etc.

John Hunt
24. Error and Exception Handling

This chapter considers exception and error handling and how it is implemented in Python. You are introduced to the object model of exception handling, to the throwing and catching of exceptions, and how to define new exceptions and exception-specific constructs.

John Hunt
25. Python Modules and Packages

Modules and packages are two constructs used in Python to organise larger programs. This chapter introduces modules in Python, how they are accessed, how they are define and how Python finds modules etc. It also explores Python packages and sub-packages.

John Hunt
26. Abstract Base Classes

This chapter presents Abstract Base Classes (also known as ABCs) which were originally introduced in Python 2.6.

John Hunt
27. Protocols, Polymorphism and Descriptors

In this chapter we will explore the idea of an implicit contract between an object and the code that uses that object. As part of this discussion we will explore what is meant by Duck Typing. Following this we will introduce the Python concept called a protocol. We will explore its role within Python programming and look at two commonly occurring protocols; the Context Manager Protocol and the Descriptor Protocol.

John Hunt
28. Monkey Patching and Attribute Lookup

Monkey Patching is a term you might well come across when looking into the Python further or when searching the web for Python related concepts. It relates to the ability in Python to extend the functionality associated with a class/type at runtime.

John Hunt
29. Decorators

The idea behind Decorators comes from the Gang of Four Design Patterns book (so-called as there were four people involved in defining these design patterns).

John Hunt
30. Iterables, Iterators, Generators and Coroutines

There are two protocols that you are very likely to use, or will possibly need to implement at some point or other; these are the Iterable protocol and the Iterator protocols. These two closely related protocols are very widely used and supported by a large number of types.

John Hunt
31. Collections, Tuples and Lists

Earlier in this book we looked at some Python built-in types such as string , int and float as well as bools .

John Hunt
32. Sets

In the last chapter we looked at Tuples and Lists; in this chapter we will look at a further container (or collection) types; the Set type.

John Hunt
33. Dictionaries

A Dictionary is a set of associations between a key and a value that is unordered, changeable (mutable) and indexed. Pictorially we might view a Dictionary as shown below for a set of countries and their capital cities. Note that in a Dictionary the keys must be unique but the values do not need to be unique.

John Hunt
34. Collection Related Modules

The chapter introduces a feature known as a list comprehension in Python.

John Hunt
35. ADTs, Queues and Stacks

There are a number of common data structures that are used within computer programs that you might expect to see within Python’s list of collection or container classes; these include Queues and Stacks. However, in the basic collection classes these are missing.

John Hunt
36. Map, Filter and Reduce

Python provides three functions that are widely used to implement functional programming style solutions in combination with collection container types. These functions are what are known as higher-order functions that take both a collection and a function that will be applied in various ways to that collection.

John Hunt
37. TicTacToe Game

In this chapter we will explore the creation of a simple TicTacToe (or Noughts and Crosses) game using an Object Oriented approach.

John Hunt
38. Correction to: Functions in Python

The updated version of this chapter

John Hunt
A Beginners Guide to Python 3 Programming
Dr. John Hunt
Copyright Year
Electronic ISBN
Print ISBN

Premium Partner