Getting Started with Python

Book Description

Harness the power of Python objects and data structures to implement algorithms for analyzing your data and efficiently extracting information

Key Features

  • Turn your designs into working software by learning the Python syntax
  • Write robust code with a solid understanding of Python data structures
  • Understand when to use the functional or the OOP approach

Book Description

This Learning Path helps you get comfortable with the world of Python. It starts with a thorough and practical introduction to Python. You'll quickly start writing programs, building websites, and working with data by harnessing Python's renowned data science libraries. With the power of linked lists, binary searches, and sorting algorithms, you'll easily create complex data structures, such as graphs, stacks, and queues. After understanding cooperative inheritance, you'll expertly raise, handle, and manipulate exceptions. You will effortlessly integrate the object-oriented and not-so-object-oriented aspects of Python, and create maintainable applications using higher level design patterns. Once you've covered core topics, you'll understand the joy of unit testing and just how easy it is to create unit tests.

By the end of this Learning Path, you will have built components that are easy to understand, debug, and can be used across different applications.

This Learning Path includes content from the following Packt products:

  • Learn Python Programming - Second Edition by Fabrizio Romano
  • Python Data Structures and Algorithms by Benjamin Baka
  • Python 3 Object-Oriented Programming by Dusty Phillips

What you will learn

  • Use data structures and control flow to write code
  • Use functions to bundle together a sequence of instructions
  • Implement objects in Python by creating classes and defining methods
  • Design public interfaces using abstraction, encapsulation and information hiding
  • Raise, define, and manipulate exceptions using special error objects
  • Create bulletproof and reliable software by writing unit tests
  • Learn the common programming patterns and algorithms used in Python

Who this book is for

If you are relatively new to coding and want to write scripts or programs to accomplish tasks using Python, or if you are an object-oriented programmer for other languages and seeking a leg up in the world of Python, then this Learning Path is for you. Though not essential, it will help you to have basic knowledge of programming and OOP.

Publisher Resources

Download Example Code

Table of Contents

  1. Title Page
  2. Copyright and Credits
    1. Getting Started with Python
  3. About Packt
    1. Why subscribe?
    2. Packt.com
  4. Contributors
    1. About the authors
    2. Packt is searching for authors like you
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Conventions used
    4. Get in touch
      1. Reviews
  6. A Gentle Introduction to Python
    1. A proper introduction
    2. Enter the Python
    3. About Python
      1. Portability
      2. Coherence
      3. Developer productivity
      4. An extensive library
      5. Software quality
      6. Software integration
      7. Satisfaction and enjoyment
    4. What are the drawbacks?
    5. Who is using Python today?
    6. Setting up the environment
      1. Python 2 versus Python 3
    7. Installing Python
      1. Setting up the Python interpreter
      2. About virtualenv
      3. Your first virtual environment
      4. Your friend, the console
    8. How you can run a Python program
      1. Running Python scripts
      2. Running the Python interactive shell
      3. Running Python as a service
      4. Running Python as a GUI application
    9. How is Python code organized?
      1. How do we use modules and packages?
    10. Python's execution model
      1. Names and namespaces
      2. Scopes
      3. Objects and classes
    11. Guidelines on how to write good code
    12. The Python culture
    13. A note on IDEs
    14. Summary
  7. Built-in Data Types
    1. Everything is an object
    2. Mutable or immutable? That is the question
    3. Numbers
      1. Integers
      2. Booleans
      3. Real numbers
      4. Complex numbers
      5. Fractions and decimals
    4. Immutable sequences
      1. Strings and bytes
        1. Encoding and decoding strings
        2. Indexing and slicing strings
        3. String formatting
      2. Tuples
    5. Mutable sequences
      1. Lists
      2. Byte arrays
    6. Set types
    7. Mapping types – dictionaries
    8. The collections module
      1. namedtuple
      2. defaultdict
      3. ChainMap
    9. Enums
    10. Final considerations
      1. Small values caching
      2. How to choose data structures
      3. About indexing and slicing
      4. About the names
    11. Summary
  8. Iterating and Making Decisions
    1. Conditional programming
      1. A specialized else – elif
      2. The ternary operator
    2. Looping
      1. The for loop
        1. Iterating over a range
        2. Iterating over a sequence
      2. Iterators and iterables
      3. Iterating over multiple sequences
      4. The while loop
      5. The break and continue statements
      6. A special else clause
    3. Putting all this together
      1. A prime generator
      2. Applying discounts
    4. A quick peek at the itertools module
      1. Infinite iterators
      2. Iterators terminating on the shortest input sequence
      3. Combinatoric generators
    5. Summary
  9. Functions, the Building Blocks of Code
    1. Why use functions?
      1. Reducing code duplication
      2. Splitting a complex task
      3. Hiding implementation details
      4. Improving readability
      5. Improving traceability
    2. Scopes and name resolution
      1. The global and nonlocal statements
    3. Input parameters
      1. Argument passing
      2. Assignment to argument names doesn't affect the caller
      3. Changing a mutable affects the caller
      4. How to specify input parameters
        1. Positional arguments
        2. Keyword arguments and default values
        3. Variable positional arguments
        4. Variable keyword arguments
        5. Keyword-only arguments
        6. Combining input parameters
        7. Additional unpacking generalizations
        8. Avoid the trap! Mutable defaults
    4. Return values
      1. Returning multiple values
    5. A few useful tips
    6. Recursive functions
    7. Anonymous functions
    8. Function attributes
    9. Built-in functions
    10. One final example
    11. Documenting your code
    12. Importing objects
      1. Relative imports
    13. Summary
  10. Files and Data Persistence
    1. Working with files and directories
      1. Opening files
        1. Using a context manager to open a file
      2. Reading and writing to a file
        1. Reading and writing in binary mode
        2. Protecting against overriding an existing file
      3. Checking for file and directory existence
      4. Manipulating files and directories
        1. Manipulating pathnames
      5. Temporary files and directories
      6. Directory content
      7. File and directory compression
    2. Data interchange formats
      1. Working with JSON
        1. Custom encoding/decoding with JSON
    3. IO, streams, and requests
      1. Using an in-memory stream
      2. Making HTTP requests
    4. Persisting data on disk
      1. Serializing data with pickle
      2. Saving data with shelve
      3. Saving data to a database
    5. Summary
  11. Principles of Algorithm Design
    1. Algorithm design paradigms
    2. Recursion and backtracking
      1. Backtracking
      2. Divide and conquer - long multiplication
      3. Can we do better? A recursive approach
    3. Runtime analysis
      1. Asymptotic analysis
      2. Big O notation
        1. Composing complexity classes
        2. Omega notation (Ω)
        3. Theta notation (ϴ)
    4. Amortized analysis
    5. Summary
  12. Lists and Pointer Structures
    1. Arrays
    2. Pointer structures
    3. Nodes
    4. Finding endpoints
      1. Node
        1. Other node types
    5. Singly linked lists
      1. Singly linked list class
      2. Append operation
    6. A faster append operation
    7. Getting the size of the list
    8. Improving list traversal
    9. Deleting nodes
      1. List search
    10. Clearing a list
    11. Doubly linked lists
      1. A doubly linked list node
        1. Doubly linked list
      2. Append operation
      3. Delete operation
      4. List search
    12. Circular lists
      1. Appending elements
      2. Deleting an element
        1. Iterating through a circular list
    13. Summary
  13. Stacks and Queues
    1. Stacks
      1. Stack implementation
      2. Push operation
      3. Pop operation
        1. Peek
      4. Bracket-matching application
    2. Queues
      1. List-based queue
        1. Enqueue operation
        2. Dequeue operation
      2. Stack-based queue
        1. Enqueue operation
        2. Dequeue operation
      3. Node-based queue
        1. Queue class
        2. Enqueue operation
        3. Dequeue operation
      4. Application of queues
        1. Media player queue
    3. Summary
  14. Trees
    1. Terminology
    2. Tree nodes
    3. Binary trees
      1. Binary search trees
      2. Binary search tree implementation
      3. Binary search tree operations
        1. Finding the minimum and maximum nodes
      4. Inserting nodes
      5. Deleting nodes
      6. Searching the tree
      7. Tree traversal
        1. Depth-first traversal
          1. In-order traversal and infix notation
          2. Pre-order traversal and prefix notation
          3. Post-order traversal and postfix notation.
        2. Breadth-first traversal
      8. Benefits of a binary search tree
      9. Expression trees
        1. Parsing a reverse Polish expression
      10. Balancing trees
      11. Heaps
    4. Summary
  15. Hashing and Symbol Tables
    1. Hashing
      1. Perfect hashing functions
    2. Hash table
      1. Putting elements
      2. Getting elements
      3. Testing the hash table
      4. Using [] with the hash table
      5. Non-string keys
      6. Growing a hash table
      7. Open addressing
        1. Chaining
      8. Symbol tables
    3. Summary
  16. Graphs and Other Algorithms
    1. Graphs
    2. Directed and undirected graphs
    3. Weighted graphs
    4. Graph representation
      1. Adjacency list
      2. Adjacency matrix
    5. Graph traversal
      1. Breadth-first search
      2. Depth-first search
    6. Other useful graph methods
    7. Priority queues and heaps
      1. Inserting
      2. Pop
      3. Testing the heap
    8. Selection algorithms
    9. Summary
  17. Searching
    1. Linear Search
      1. Unordered linear search
      2. Ordered linear search
    2. Binary search
    3. Interpolation search
      1. Choosing a search algorithm
    4. Summary
  18. Sorting
    1. Sorting algorithms
    2. Bubble sort
    3. Insertion sort
    4. Selection sort
    5. Quick sort
      1. List partitioning
        1. Pivot selection
      2. Implementation
      3. Heap sort
    6. Summary
  19. Selection Algorithms
    1. Selection by sorting
    2. Randomized selection
      1. Quick select
        1. Partition step
    3. Deterministic selection
      1. Pivot selection
      2. Median of medians
      3. Partitioning step
    4. Summary
  20. Object-Oriented Design
    1. Introducing object-oriented
    2. Objects and classes
    3. Specifying attributes and behaviors
      1. Data describes objects
      2. Behaviors are actions
    4. Hiding details and creating the public interface
    5. Composition
    6. Inheritance
      1. Inheritance provides abstraction
      2. Multiple inheritance
    7. Case study
    8. Exercises
    9. Summary
  21. Objects in Python
    1. Creating Python classes
      1. Adding attributes
      2. Making it do something
        1. Talking to yourself
        2. More arguments
      3. Initializing the object
      4. Explaining yourself
    2. Modules and packages
      1. Organizing modules
        1. Absolute imports
        2. Relative imports
    3. Organizing module content
    4. Who can access my data?
    5. Third-party libraries
    6. Case study
    7. Exercises
    8. Summary
  22. When Objects Are Alike
    1. Basic inheritance
      1. Extending built-ins
      2. Overriding and super
    2. Multiple inheritance
      1. The diamond problem
      2. Different sets of arguments
    3. Polymorphism
    4. Abstract base classes
      1. Using an abstract base class
      2. Creating an abstract base class
      3. Demystifying the magic
    5. Case study
    6. Exercises
    7. Summary
  23. Expecting the Unexpected
    1. Raising exceptions
      1. Raising an exception
      2. The effects of an exception
      3. Handling exceptions
      4. The exception hierarchy
      5. Defining our own exceptions
    2. Case study
    3. Exercises
    4. Summary
  24. When to Use Object-Oriented Programming
    1. Treat objects as objects
    2. Adding behaviors to class data with properties
      1. Properties in detail
      2. Decorators – another way to create properties
      3. Deciding when to use properties
    3. Manager objects
      1. Removing duplicate code
      2. In practice
    4. Case study
    5. Exercises
    6. Summary
  25. Python Object-Oriented Shortcuts
    1. Python built-in functions
      1. The len() function
      2. Reversed
      3. Enumerate
      4. File I/O
      5. Placing it in context
    2. An alternative to method overloading
      1. Default arguments
      2. Variable argument lists
      3. Unpacking arguments
    3. Functions are objects too
      1. Using functions as attributes
      2. Callable objects
    4. Case study
    5. Exercises
    6. Summary
  26. The Iterator Pattern
    1. Design patterns in brief
    2. Iterators
      1. The iterator protocol
    3. Comprehensions
      1. List comprehensions
      2. Set and dictionary comprehensions
      3. Generator expressions
    4. Generators
      1. Yield items from another iterable
    5. Coroutines
      1. Back to log parsing
      2. Closing coroutines and throwing exceptions
      3. The relationship between coroutines, generators, and functions
    6. Case study
    7. Exercises
    8. Summary
  27. Python Design Patterns I
    1. The decorator pattern
      1. A decorator example
      2. Decorators in Python
    2. The observer pattern
      1. An observer example
    3. The strategy pattern
      1. A strategy example
      2. Strategy in Python
    4. The state pattern
      1. A state example
      2. State versus strategy
      3. State transition as coroutines
    5. The singleton pattern
      1. Singleton implementation
      2. Module variables can mimic singletons
    6. The template pattern
      1. A template example
    7. Exercises
    8. Summary
  28. Python Design Patterns II
    1. The adapter pattern
    2. The facade pattern
    3. The flyweight pattern
    4. The command pattern
    5. The abstract factory pattern
    6. The composite pattern
    7. Exercises
    8. Summary
  29. Testing Object-Oriented Programs
    1. Why test?
      1. Test-driven development
    2. Unit testing
      1. Assertion methods
      2. Reducing boilerplate and cleaning up
      3. Organizing and running tests
      4. Ignoring broken tests
    3. Testing with pytest
      1. One way to do setup and cleanup
      2. A completely different way to set up variables
      3. Skipping tests with pytest
    4. Imitating expensive objects
    5. How much testing is enough?
    6. Case study
      1. Implementing it
    7. Exercises
    8. Summary
  30. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product Information

  • Title: Getting Started with Python
  • Author(s): Fabrizio Romano, Benjamin Baka, Dusty Phillips
  • Release date: February 2019
  • Publisher(s): Packt Publishing
  • ISBN: 9781838551919