Learn Python Programming - Fourth Edition

Book description

This edition offers updated content covering Python 3.9 to 3.12, new chapters on type hinting and CLI applications, and expanded practical examples, making it the ideal resource for both new and experienced Python programmers

Key Features

  • Learn to create and deploy APIs and CLI applications, leveraging Python’s strengths in scripting and automation
  • Stay current with the latest features and improvements in Python, including pattern matching and the latest exception handling syntax
  • Engage with new real-world examples and projects, including competitive programming problems, to solidify your understanding of Python

Book Description

Learn Python Programming, Fourth Edition provides a comprehensive, up-to-date introduction to Python programming, covering fundamental concepts and practical applications. This edition has been meticulously updated to include the latest features from Python versions 3.9 to 3.12, new chapters on type hinting and CLI applications, and updated examples reflecting modern Python development practices. The book empowers you to take ownership of writing your software and become independent in fetching the resources you need. You will have a clear idea of where to go and how to build on what you have learned from the book

Through examples, the book explores a wide range of applications and concludes by building real-world Python projects based on the concepts you have learned. This book offers a clear and practical guide to mastering Python and applying it effectively in various domains such as data science, web development, and automation.

What you will learn

  • Install and Set Up Python on Windows, Mac, and Linux
  • Write Elegant, Reusable, and Efficient Code
  • Avoid Common Pitfalls like duplication and over-engineering
  • Use Functional and Object-Oriented Programming Approaches appropriately
  • Build APIs with FastAPI and program CLI applications
  • Understand Data Persistence and Cryptography for secure applications
  • Manipulate Data Efficiently using Python's built-in data structures
  • Package Your Applications for distribution via the Python Package Index (PyPI)
  • Solve Competitive Programming Problems with Python

Who this book is for

This book is for everyone who wants to learn Python from scratch, as well as experienced programmers looking for a reference book. Prior knowledge of basic programming concepts will help you follow along, but it’s not a prerequisite.

Table of contents

  1. Learn Python Programming, Fourth Edition: A Comprehensive, Up-to-Date, and Definitive Guide to Learning Python
  2. 1 A Gentle Introduction to Python
    1. Join our book community on Discord
    2. A proper introduction
    3. Enter the Python
    4. About Python
      1. Portability
      2. Coherence
      3. Developer productivity
      4. An extensive library
      5. Software quality
      6. Software integration
      7. Data Science
      8. Satisfaction and enjoyment
    5. What are the drawbacks?
    6. Who is using Python today?
      1. Tech industry
      2. Financial sector
      3. Technology and software
      4. Space and research
      5. Retail and e-Commerce
      6. Entertainment and media
      7. Education and learning platforms
      8. Government and non-profit
    7. Setting up the environment
    8. Installing Python
      1. Useful installation resources
      2. Installing Python on Windows
      3. Installing Python on macOS
      4. Installing Python on Linux
      5. The Python Console
      6. About virtual environments
      7. Your first virtual environment
      8. Installing third-party libraries
      9. The console
    9. How to 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
    10. How is Python code organized?
      1. How do we use modules and packages?
    11. Python's execution model
      1. Names and namespaces
      2. Scopes
    12. Guidelines for writing good code
    13. Python culture
    14. A note on IDEs
    15. A word about AI
    16. Summary
  3. 2 Built-In Data Types
    1. Everything is an object
    2. Mutability
    3. Numbers
      1. Integers
      2. Booleans
      3. Real numbers
      4. Complex numbers
      5. Fractions and Decimals
    4. Immutable sequences
      1. Strings and bytes
      2. Tuples
    5. Mutable sequences
      1. Lists
      2. Bytearrays
    6. Set types
    7. Mapping types: dictionaries
    8. Data types
      1. Dates and times
      2. The collections module
      3. Enums
    9. Final considerations
      1. Small value caching
      2. How to choose data structures
      3. About indexing and slicing
      4. About names
    10. Summary
  4. 3 Conditionals and Iteration
    1. Conditional programming
      1. The if statement
      2. A specialized else: elif
      3. Nesting if statements
      4. The ternary operator
      5. Pattern matching
    2. Looping
      1. The for loop
      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. Assignment expressions
      1. Statements and expressions
      2. Using the walrus operator
      3. A word of warning
    4. Putting all this together
      1. A prime generator
      2. Applying discounts
    5. A quick peek at the itertools module
      1. Infinite iterators
      2. Iterators terminating on the shortest input sequence
      3. Combinatoric generators
    6. Summary
  5. 4 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 parameter names
      3. Changing a mutable object
      4. Passing arguments
      5. Defining parameters
    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. Documenting your code
    11. Importing objects
      1. Relative imports
    12. One final example
    13. Summary
  6. 5 Comprehensions and Generators
    1. The map, zip, and filter functions
      1. map
      2. zip
      3. filter
    2. Comprehensions
      1. Nested comprehensions
      2. Filtering a comprehension
      3. Dictionary comprehensions
      4. Set comprehensions
    3. Generators
      1. Generator functions
      2. Going beyond next
      3. The yield from expression
      4. Generator expressions
    4. Some performance considerations
    5. Do not overdo comprehensions and generators
    6. Name localization
    7. Generation behavior in built-ins
    8. One last example
    9. Summary
  7. 6 OOP, Decorators, and Iterators
    1. Decorators
      1. A decorator factory
    2. Object-oriented programming (OOP)
      1. The simplest Python class
      2. Class and object namespaces
      3. Attribute shadowing
      4. The self argument
      5. Initializing an instance
      6. OOP is about code reuse
      7. Accessing a base class
      8. Multiple inheritance
      9. Class and static methods
      10. Private methods and name mangling
      11. The property decorator
      12. The cached_property decorator
      13. Operator overloading
      14. Polymorphism – a brief overview
      15. Data classes
    3. Writing a custom iterator
    4. Summary
  8. 7 Exceptions and Context Managers
    1. Exceptions
      1. Raising exceptions
      2. Defining your own exceptions
      3. Tracebacks
      4. Handling exceptions
      5. Exception Groups
      6. Not only for errors
    2. Context managers
      1. Class-based context managers
      2. Generator-based context managers
    3. Summary
  9. 8 Files and Data Persistence
    1. Working with files and directories
      1. Opening files
      2. Reading and writing to a file
      3. Checking for file and directory existence
      4. Manipulating files and directories
      5. Temporary files and directories
      6. Directory content
      7. File and directory compression
    2. Data interchange formats
      1. Working with JSON
    3. I/O, 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. Configuration files
      1. Common formats
    6. Summary
  10. 9 Cryptography and Tokens
    1. The need for cryptography
      1. Useful guidelines
    2. Hashlib
    3. HMAC
    4. Secrets
      1. Random Objects
      2. Token generation
      3. Digest comparison
    5. JSON Web Tokens
      1. Registered claims
      2. Using asymmetric (public key) algorithms
    6. Useful references
    7. Summary
  11. 10 Testing
    1. Testing your application
      1. The anatomy of a test
      2. Testing guidelines
      3. Unit testing
      4. Testing a CSV generator
    2. Test-driven development
    3. Summary

Product information

  • Title: Learn Python Programming - Fourth Edition
  • Author(s): Fabrizio Romano, Heinrich Kruger
  • Release date: December 2024
  • Publisher(s): Packt Publishing
  • ISBN: 9781835882948