Python Fundamentals

Book Description

With an interesting mix of theory and practicals, explore Python and its features, and progress from beginner to being skilled in this popular scripting language

Key Features

  • A comprehensive introduction to the world of Python programming
  • Paves an easy-to-follow path for you to navigate through concepts
  • Filled with over 90 practical exercises and activities to reinforce your learning

Book Description

After a brief history of Python and key differences between Python 2 and Python 3, you'll understand how Python has been used in applications such as YouTube and Google App Engine. As you work with the language, you'll learn about control statements, delve into controlling program flow and gradually work on more structured programs via functions.

As you settle into the Python ecosystem, you'll learn about data structures and study ways to correctly store and represent information. By working through specific examples, you'll learn how Python implements object-oriented programming (OOP) concepts of abstraction, encapsulation of data, inheritance, and polymorphism. You'll be given an overview of how imports, modules, and packages work in Python, how you can handle errors to prevent apps from crashing, as well as file manipulation.

By the end of this book, you'll have built up an impressive portfolio of projects and armed yourself with the skills you need to tackle Python projects in the real world.

What you will learn

  • Use control statements
  • Manipulate primitive and non-primitive data structures
  • Use loops to iterate over objects or data for accurate results
  • Write encapsulated and succinct Python functions
  • Build Python classes using object-oriented programming
  • Manipulate files on the file system (open, read, write, and delete)

Who this book is for

Python Fundamentals is great for anyone who wants to start using Python to build anything from simple command-line programs to web applications. Prior knowledge of Python isn't required.

Publisher Resources

Download Example Code

Table of Contents

  1. Preface
    1. About the Book
      1. About the Authors
      2. Learning Objectives
      3. Audience
      4. Approach
      5. Minimum Hardware Requirements
      6. Software Requirements
      7. Conventions
      8. Installation
      9. Installing the Code Bundle
      10. Additional Resources
  2. Introducing Python
    1. Introduction
      1. Python 2 Versus Python 3
    2. Working with the Python Interactive Shell
      1. Exercise 1: Checking our Python Installation
      2. Exercise 2: Working with the Python Interpreter
      3. Activity 1: Working with the Python Shell
    3. Writing and Running Simple Scripts
      1. Exercise 3: Creating a Script
      2. Running a File Containing Invalid Commands
      3. Exercise 4: Passing User Arguments to Scripts
      4. Activity 2: Running Simple Python Scripts
    4. Python Syntax
    5. Variables
      1. Values
      2. Exercise 5: Checking the Type of a Value
      3. Type Conversion
      4. Exercise 6: Assigning Variables
      5. Exercise 7: Using Variables
      6. Multiple Assignment
      7. Activity 3: Using Variables and Assign Statements
      8. Naming Identifiers and Reserved Words
      9. Exercise 8: Python Keywords
      10. Python Naming Conventions
      11. Activity 4: Variable Assignment and Variable Naming Conventions
    6. User Input, Comments, and Indentations
      1. User Input from the Keyboard
      2. Passing in a Prompt to the input Function
      3. Using Different Input Data Types in your Program
      4. Exercise 9: Fetching and Using User Input
      5. Comments
      6. Indentation
      7. Exercise 10: The Importance of Proper Indentation
      8. Activity 5: Fixing Indentations in a Code Block
      9. Activity 6: Implementing User Input and Comments in a Script
    7. Summary
  3. Data Types
    1. Introduction
    2. Numerical Data
      1. Types of Numbers
      2. Exercise 11: Converting Between Different Types of Number Systems
      3. Operators
      4. Order of Operations
      5. Activity 7: Order of Operations
      6. Activity 8: Using Different Arithmetic Operators
    3. Strings
      1. String Operations and Methods
      2. Indexing
      3. Slicing
      4. Activity 9: String Slicing
      5. Length
      6. String Formatting
      7. String Methods
      8. Activity 10: Working with Strings
      9. Escape Sequences
      10. Exercise 12: Using Escape Sequences
      11. Activity 11: Manipulating Strings
    4. Lists
      1. List Operations
      2. Exercise 13: List References
      3. Activity 12: Working with Lists
    5. Booleans
      1. Comparison Operators
      2. Logical Operators
      3. Membership Operators
      4. Activity 13: Using Boolean Operators
    6. Summary
  4. Control Statements
    1. Introduction
    2. Control Statements
      1. Program Flow
      2. Control Statement
    3. The if Statement
      1. Exercise 14: Using the if Statement
      2. Activity 14: Working with the if Statement
    4. The while Statement
      1. Exercise 15: Using the while Statement
      2. Exercise 16: Using while to Keep a Program Running
      3. Activity 15: Working with the while Statement
    5. while Versus if
    6. Loops
    7. The for Loop
      1. Exercise 17: Using the for Loop
      2. Using else
    8. The range Function
      1. Activity 16: The for loop and the range Function
    9. Nesting Loops
      1. Exercise 18: Using Nested Loops
      2. Activity 17: Nested Loops
    10. Breaking Out of Loops
      1. The break Statement
      2. The continue Statement
      3. The pass Statement
      4. Activity 18: Breaking out of Loops
    11. Summary
  5. Functions
    1. Introduction
    2. Built-In Functions
    3. User-Defined Functions
      1. Calling a Function
      2. Global and Local Variables
      3. Exercise 19: Defining Global and Local Variables
      4. Function Return
      5. Using main()
    4. Function Arguments
      1. Required Arguments
      2. Keyword Arguments
      3. Default Arguments
      4. Variable Number of Arguments
      5. Activity 19: Function Arguments
    5. Anonymous Functions
      1. Exercise 20: Creating a Lambda Function
      2. Activity 20: Using Lambda Functions
    6. Summary
  6. Lists and Tuples
    1. Introduction
    2. List Syntax
    3. List Methods
      1. list.append(item)
      2. list.extend(iterable)
      3. list.insert(index, item)
      4. list.remove(item)
      5. list.pop([index])
      6. list.clear()
      7. list.index(item [, start [, end]])
      8. list.count(item)
      9. list.sort(key=None, reverse=False)
      10. list.reverse()
      11. list.copy()
      12. Activity 21: Using the List Methods
    4. List Comprehensions
    5. Tuple Syntax
      1. Exercise 21: Creating a Tuple
    6. Accessing Tuple Elements
      1. Indexing
      2. Exercise 22: Accessing Tuple Elements Using Indexing
      3. Slicing
      4. Exercise 21: Creating a Tuple
    7. Accessing Tuple Elements
      1. Indexing
      2. Exercise 23: Accessing Tuple Elements Using Slicing
    8. Tuple Methods
      1. Activity 22: Using Tuple Methods
    9. Summary
  7. Dictionaries and Sets
    1. Introduction
    2. Working with Dictionaries
      1. Activity 23: Creating a Dictionary
      2. Exercise 24: Adding Data to a Dictionary
      3. Exercise 25: Reading Data from a Dictionary
      4. Exercise 26: Iterating Through Dictionaries
      5. Checking for the Existence of Particular Keys
    3. Additional Dictionary Attributes
      1. dict.update()
      2. dict.clear() and dict.pop()
      3. dict.copy()
      4. dict.popitem()
      5. dict.setdefault()
      6. dict.fromkeys()
      7. Activity 24: Arranging and Presenting Data Using Dictionaries
    4. Ordered Dictionaries
      1. Activity 25: Combining Dictionaries
    5. The Basics of Sets
      1. Exercise 27: Creating Sets
      2. Exercise 28: Adding Data to a Set
      3. Exercise 29: Reading Data from a Set
      4. Activity 26: Building a Set
      5. Exercise 30: Removing Data from a Set
    6. Set Operations
      1. Union
      2. Intersection
      3. Difference
      4. Subsets
      5. Equality
      6. Update Methods
    7. Frozen Sets
      1. Activity 27: Creating Unions of Elements in a Collection
    8. Summary
  8. Object-Oriented Programming
    1. Introduction
    2. A First Look at OOP
    3. OOP in Python
      1. Defining a Class in Python
      2. Exercise 31: Creating a Class
      3. Instantiating an Object
      4. Exercise 32: Instantiating a Person Object
      5. Adding Attributes to an Object
      6. The __init__ Method
      7. Exercise 33: Adding Attributes to a Class
      8. Activity 28: Defining a Class and Objects
    4. Methods in a Class
      1. Defining Methods in a Class
      2. Exercise 34: Creating a Method for our Class
      3. Passing Arguments to Instance Methods
      4. Exercise 35: Passing Arguments to Instance Methods
      5. Exercise 36: Setting Instance Attributes within Instance Methods
      6. Activity 29: Defining Methods in a Class
    5. Class Versus Instance Attributes
      1. Exercise 37: Declaring a Class with Instance Attributes
      2. Class Attributes
      3. Exercise 38: Extending our Class with Class Attributes
      4. Exercise 39: Implementing a Counter for Instances of a Class
      5. Activity 30: Creating Class Attributes
    6. Class Versus Instance Methods
      1. Exercise 40: Creating Instance Methods
      2. Class Methods
      3. Exercise 41: Testing our Factory Method
      4. Exercise 42: Accessing Class Attributes from within Class Methods
      5. Encapsulation and Information Hiding
      6. Activity 31: Creating Class Methods and Using Information Hiding
    7. Class Inheritance
      1. Exercise 43: Implementing Class Inheritance
      2. Overriding __init__()
      3. Exercise 44: Overriding the __init__ Method to Add an Attribute
      4. Commonly Overridden Methods
      5. Activity 32: Overriding Methods
    8. Multiple Inheritance
      1. Exercise 45: Implementing Multiple Inheritance
      2. Activity 33: Practicing Multiple Inheritance
    9. Summary
  9. Modules, Packages, and File Operations
    1. Introduction
    2. Defining Modules
      1. Exercise 46: Creating Modules
    3. Imports and Import Statements
      1. Exercise 47: Importing Modules
    4. Modules and Packages
      1. The Module Search Path
      2. Standard Python Modules
      3. Activity 34: Inspecting Modules
      4. Packages
      5. Absolute Imports
      6. Relative Imports
      7. Activity 35: Listing the Resources Defined in a Package or Module
      8. Activity 36: Using Resources in a Module
    5. File Operations
    6. The file Object
      1. The file Object Methods
    7. Reading and Writing to Files
      1. The open() Method
      2. Exercise 48: Creating, Reading, and Writing to Files
      3. The with Context Manager
      4. Activity 37: Performing File Operations
    8. Handling Structured Data
      1. Working with CSV Data
      2. Activity 38: Working with Files
      3. Working with JSON Data
    9. Summary
  10. Error Handling
    1. Introduction
      1. Errors and Exceptions in Python
      2. How to Raise Exceptions
    2. Built-In Exceptions
      1. SyntaxError
      2. ImportError
      3. KeyError
      4. TypeError
      5. AttributeError
      6. IndexError
      7. NameError
      8. FileNotFoundError
      9. Activity 39: Identifying Error Scenarios
    3. Handling Errors and Exceptions
      1. Exercise 49: Implementing the try…except Block
      2. Exercise 50: Implementing the try…except…else Block
      3. Exercise 51: Implementing the finally Keyword
      4. Activity 40: Handling Errors
    4. Custom Exceptions
      1. Implementing Your Own Exception Class
      2. Activity 41: Creating Your Own Custom Exception Class
    5. Summary
  11. Appendix A
    1. Chapter 1: Introducing Python
      1. Activity 1: Working with the Python Shell
      2. Activity 2: Running Simple Python Scripts
      3. Activity 3: Using Variables and Assign Statements
      4. Activity 4: Variable Assignment and Variable Naming Conventions
      5. Activity 5: Fixing Indentations in a Code Block
      6. Activity 6: Implementing User Input and Comments in a Script
    2. Chapter 2: Data Types
      1. Activity 7: Order of Operations
      2. Activity 8: Using Different Arithmetic Operators
      3. Activity 9: String Slicing
      4. Activity 10: Working with Strings
      5. Activity 11: Manipulating Strings
      6. Activity 12: Working with Lists
      7. Activity 13: Using Boolean Operators
    3. Chapter 3: Control Statements
      1. Activity 14: Working with the if Statement
      2. Activity 15: Working with the while Statement
      3. Activity 16: The for loop and range Function
      4. Activity 17: Nested Loops
      5. Activity 18: Breaking out of Loops
    4. Chapter 4: Functions
      1. Activity 19: Function Arguments
      2. Activity 20: Using Lambda Functions
    5. Chapter 5: Lists and Tuples
      1. Activity 21: Using the List Methods
      2. Activity 22: Using Tuple Methods
    6. Chapter 6: Dictionaries and Sets
      1. Activity 23: Creating a Dictionary
      2. Activity 24: Arranging and Presenting Data Using Dictionaries
      3. Activity 25: Combining Dictionaries
      4. Activity 26: Building a Set
      5. Activity 27: Creating Unions of Elements in a Collection
    7. Chapter 7: Object-Oriented Programming
      1. Activity 28: Defining a Class and Objects
      2. Activity 29: Defining Methods in a Class
      3. Activity 30: Creating Class Attributes
      4. Activity 31: Creating Class Methods and Using Information Hiding
      5. Activity 32: Overriding Methods
      6. Activity 33: Practicing Multiple Inheritance
    8. Chapter 8: Modules, Packages, and File Operations
      1. Activity 34: Inspecting Modules
      2. Activity 35: Listing the Resources Defined in a Package or Module
      3. Activity 36: Using Resources in a Module
      4. Activity 37: Performing File Operations
      5. Activity 38: Working with Files
    9. Chapter 9: Error Handling
      1. Activity 39: Identifying Error Scenarios
      2. Activity 40: Handling Errors
      3. Activity 41: Creating Your Own Custom Exception Class

Product Information

  • Title: Python Fundamentals
  • Author(s): Ryan Marvin, Mark Ng'ang'a, Amos Omondi
  • Release date: October 2018
  • Publisher(s): Packt Publishing
  • ISBN: 9781789807325