A Beginners Guide to Python 3 Programming

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...

Full description

Saved in:
Bibliographic Details
Main Author Hunt, John
Format eBook
LanguageEnglish
Published Cham Springer Nature 2019
Springer International Publishing AG
Springer International Publishing
Springer
Edition1
SeriesUndergraduate Topics in Computer Science
Subjects
Online AccessGet full text

Cover

Loading…
Table of Contents:
  • Intro -- Preface -- Chapter Organization -- What You Need -- Using an IDE -- Downloading the PyCharm IDE -- Setting Up the IDE -- Conventions -- Example Code and Sample Solutions -- Contents -- 1 Introduction -- 1.1 What Is Python? -- 1.2 Python Versions -- 1.3 Python Programming -- 1.4 Python Libraries -- 1.5 Python Execution Model -- 1.6 Running Python Programs -- 1.6.1 Interactively Using the Python Interpreter -- 1.6.2 Running a Python File -- 1.6.3 Executing a Python Script -- 1.6.4 Using Python in an IDE -- 1.7 Useful Resources -- 2 Setting Up the Python Environment -- 2.1 Introduction -- 2.2 Check to See If Python Is Installed -- 2.3 Installing Python on a Windows PC -- 2.4 Setting Up on a Mac -- 2.5 Online Resources -- 3 A First Python Program -- 3.1 Introduction -- 3.2 Hello World -- 3.3 Interactive Hello World -- 3.4 Variables -- 3.5 Naming Conventions -- 3.6 Assignment Operator -- 3.7 Python Statements -- 3.8 Comments in Code -- 3.9 Scripts Versus Programs -- 3.10 Online Resources -- 3.11 Exercises -- 4 Python Strings -- 4.1 Introduction -- 4.2 What Are Strings? -- 4.3 Representing Strings -- 4.4 What Type Is String -- 4.5 What Can You Do with Strings? -- 4.5.1 String Concatenation -- 4.5.2 Length of a String -- 4.5.3 Accessing a Character -- 4.5.4 Accessing a Subset of Characters -- 4.5.5 Repeating Strings -- 4.5.6 Splitting Strings -- 4.5.7 Counting Strings -- 4.5.8 Replacing Strings -- 4.5.9 Finding Sub Strings -- 4.5.10 Converting Other Types into Strings -- 4.5.11 Comparing Strings -- 4.5.12 Other String Operations -- 4.6 Hints on Strings -- 4.6.1 Python Strings Are Case Sensitive -- 4.6.2 Function/Method Names -- 4.6.3 Function/Method Invocations -- 4.7 String Formatting -- 4.8 String Templates -- 4.9 Online Resources -- 4.10 Exercises -- 5 Numbers, Booleans and None -- 5.1 Introduction -- 5.2 Types of Numbers -- 5.3 Integers
  • 19 Class Side and Static Behaviour -- 19.1 Introduction -- 19.2 Class Side Data -- 19.3 Class Side Methods -- 19.3.1 Why Class-Side Methods? -- 19.4 Static Methods -- 19.5 Hints -- 19.6 Online Resources -- 19.7 Exercises -- 20 Class Inheritance -- 20.1 Introduction -- 20.2 What Is Inheritance? -- 20.3 Terminology Around Inheritance -- 20.4 The Class Object and Inheritance -- 20.5 The Built-in Object Class -- 20.6 Purpose of Subclasses -- 20.7 Overriding Methods -- 20.8 Extending Superclass Methods -- 20.9 Inheritance Oriented Naming Conventions -- 20.10 Python and Multiple Inheritance -- 20.11 Multiple Inheritance Considered Harmful -- 20.12 Summary -- 20.13 Online Resources -- 20.14 Exercises -- 21 Why Bother with Object Orientation? -- 21.1 Introduction -- 21.2 The Procedural Approach -- 21.2.1 Procedures for the Data Structure -- 21.2.2 Packages -- 21.3 Does Object Orientation Do Any Better? -- 21.3.1 Packages Versus Classes -- 21.3.2 Inheritance -- 21.4 Summary -- 22 Operator Overloading -- 22.1 Introduction -- 22.2 Operator Overloading -- 22.2.1 Why Have Operator Overloading? -- 22.2.2 Why Not Have Operator Overloading? -- 22.2.3 Implementing Operator Overloading -- 22.3 Numerical Operators -- 22.4 Comparison Operators -- 22.5 Logical Operators -- 22.6 Summary -- 22.7 Online Resources -- 22.8 Exercises -- 23 Python Properties -- 23.1 Introduction -- 23.2 Python Attributes -- 23.3 Setter and Getter Style Methods -- 23.4 Public Interface to Properties -- 23.5 More Concise Property Definitions -- 23.6 Online Resources -- 23.7 Exercises -- 24 Error and Exception Handling -- 24.1 Introduction -- 24.2 Errors and Exceptions -- 24.3 What Is an Exception? -- 24.4 What Is Exception Handling? -- 24.5 Handling an Exception -- 24.5.1 Accessing the Exception Object -- 24.5.2 Jumping to Exception Handlers -- 24.5.3 Catch Any Exception -- 24.5.4 The Else Clause
  • 8.4.6 End of Game Status -- 8.5 The Complete Listing -- 8.6 Hints -- 8.6.1 Initialising Variables -- 8.6.2 Blank Lines Within a Block of Code -- 8.7 Exercises -- 9 Recursion -- 9.1 Introduction -- 9.2 Recursive Behaviour -- 9.3 Benefits of Recursion -- 9.4 Recursively Searching a Tree -- 9.5 Recursion in Python -- 9.6 Calculating Factorial Recursively -- 9.7 Disadvantages of Recursion -- 9.8 Online Resources -- 9.9 Exercises -- 10 Introduction to Structured Analysis -- 10.1 Introduction -- 10.2 Structured Analysis and Function Identification -- 10.3 Functional Decomposition -- 10.3.1 Functional Decomposition Terminology -- 10.3.2 Functional Decomposition Process -- 10.3.3 Calculator Functional Decomposition Example -- 10.4 Functional Flow -- 10.5 Data Flow Diagrams -- 10.6 Flowcharts -- 10.7 Data Dictionary -- 10.8 Online Resources -- 11 Functions in Python -- 11.1 Introduction -- 11.2 What Are Functions? -- 11.3 How Functions Work -- 11.4 Types of Functions -- 11.5 Defining Functions -- 11.5.1 An Example Function -- 11.6 Returning Values from Functions -- 11.7 Docstring -- 11.8 Function Parameters -- 11.8.1 Multiple Parameter Functions -- 11.8.2 Default Parameter Values -- 11.8.3 Named Arguments -- 11.8.4 Arbitrary Arguments -- 11.8.5 Positional and Keyword Arguments -- 11.9 Anonymous Functions -- 11.10 Online Resources -- 11.11 Exercises -- 12 Scope and Lifetime of Variables -- 12.1 Introduction -- 12.2 Local Variables -- 12.3 The Global Keyword -- 12.4 Nonlocal Variables -- 12.5 Hints -- 12.6 Online Resources -- 12.7 Exercise -- 13 Implementing a Calculator Using Functions -- 13.1 Introduction -- 13.2 What the Calculator Will Do -- 13.3 Getting Started -- 13.4 The Calculator Operations -- 13.5 Behaviour of the Calculator -- 13.6 Identifying Whether the User Has Finished -- 13.7 Selecting the Operation -- 13.8 Obtaining the Input Numbers
  • 24.5.5 The Finally Clause
  • 5.3.1 Converting to Ints -- 5.4 Floating Point Numbers -- 5.4.1 Converting to Floats -- 5.4.2 Converting an Input String into a Floating Point Number -- 5.5 Complex Numbers -- 5.6 Boolean Values -- 5.7 Arithmetic Operators -- 5.7.1 Integer Operations -- 5.7.2 Negative Number Integer Division -- 5.7.3 Floating Point Number Operators -- 5.7.4 Integers and Floating Point Operations -- 5.7.5 Complex Number Operators -- 5.8 Assignment Operators -- 5.9 None Value -- 5.10 Online Resources -- 5.11 Exercises -- 5.11.1 General Exercise -- 5.11.2 Convert Kilometres to Miles -- 6 Flow of Control Using If Statements -- 6.1 Introduction -- 6.2 Comparison Operators -- 6.3 Logical Operators -- 6.4 The If Statement -- 6.4.1 Working with an If Statement -- 6.4.2 Else in an If Statement -- 6.4.3 The Use of elif -- 6.5 Nesting If Statements -- 6.6 If Expressions -- 6.7 A Note on True and False -- 6.8 Hints -- 6.9 Online Resources -- 6.10 Exercises -- 6.10.1 Check Input Is Positive or Negative -- 6.10.2 Test if a Number Is Odd or Even -- 6.10.3 Kilometres to Miles Converter -- 7 Iteration/Looping -- 7.1 Introduction -- 7.2 While Loop -- 7.3 For Loop -- 7.4 Break Loop Statement -- 7.5 Continue Loop Statement -- 7.6 For Loop with Else -- 7.7 A Note on Loop Variable Naming -- 7.8 Dice Roll Game -- 7.9 Online Resources -- 7.10 Exercises -- 7.10.1 Calculate the Factorial of a Number -- 7.10.2 Print All the Prime Numbers in a Range -- 8 Number Guessing Game -- 8.1 Introduction -- 8.2 Setting Up the Program -- 8.2.1 Add a Welcome Message -- 8.2.2 Running the Program -- 8.3 What Will the Program Do? -- 8.4 Creating the Game -- 8.4.1 Generate the Random Number -- 8.4.2 Obtain an Input from the User -- 8.4.3 Check to See If the Player Has Guessed the Number -- 8.4.4 Check They Haven't Exceeded Their Maximum Number of Guess -- 8.4.5 Notify the Player Whether Higher or Lower
  • 13.9 Determining the Operation to Execute -- 13.10 Running the Calculator -- 13.11 Exercises -- 14 Introduction to Functional Programming -- 14.1 Introduction -- 14.2 What Is Functional Programming? -- 14.3 Advantages to Functional Programming -- 14.4 Disadvantages of Functional Programming -- 14.5 Referential Transparency -- 14.6 Further Reading -- 15 Higher Order Functions -- 15.1 Introduction -- 15.2 Recap on Functions in Python -- 15.3 Functions as Objects -- 15.4 Higher Order Function Concepts -- 15.4.1 Higher Order Function Example -- 15.5 Python Higher Order Functions -- 15.5.1 Using Higher Order Functions -- 15.5.2 Functions Returning Functions -- 15.6 Online Resources -- 15.7 Exercises -- 16 Curried Functions -- 16.1 Introduction -- 16.2 Currying Concepts -- 16.3 Python and Curried Functions -- 16.4 Closures -- 16.5 Online Resources -- 16.6 Exercises -- 17 Introduction to Object Orientation -- 17.1 Introduction -- 17.2 Classes -- 17.3 What Are Classes for? -- 17.3.1 What Should a Class Do? -- 17.3.2 Class Terminology -- 17.4 How Is an OO System Constructed? -- 17.4.1 Where Do We Start? -- 17.4.2 Identifying the Objects -- 17.4.3 Identifying the Services or Methods -- 17.4.4 Refining the Objects -- 17.4.5 Bringing It All Together -- 17.5 Where Is the Structure in an OO Program? -- 17.6 Further Reading -- 18 Python Classes -- 18.1 Introduction -- 18.2 Class Definitions -- 18.3 Creating Examples of the Class Person -- 18.4 Be Careful with Assignment -- 18.5 Printing Out Objects -- 18.5.1 Accessing Object Attributes -- 18.5.2 Defining a Default String Representation -- 18.6 Providing a Class Comment -- 18.7 Adding a Birthday Method -- 18.8 Defining Instance Methods -- 18.9 Person Class Recap -- 18.10 The del Keyword -- 18.11 Automatic Memory Management -- 18.12 Intrinsic Attributes -- 18.13 Online Resources -- 18.14 Exercises