Leanpub Header

Skip to main content

Learn Logic Programming in Python

A Gentle, Illustrated Guide with PyDatalog, from Facts and Rules to Recursion, SQL and Real Applications

Learn Logic Programming in Python
This book is 100% completeLast updated on 2026-10-15

Learn to think in facts, rules and queries, using pyDatalog inside ordinary Python. A gentle, illustrated first course in logic programming, with every example run and recorded.

Minimum price

$17.00

$29.00

You pay

Author earns

$

Also available for 1 book credit with a Reader Membership

PDF
EPUB
About

About

About the Book

Most programs tell a computer how. Logic programming lets you say what is true, and then simply ask.

You state a few facts, add a few rules, and ask a question with a blank in it. The engine does the looping and the remembering. Learn Logic Programming in Python teaches that way of thinking, one small idea at a time, using pyDatalog, a small library that runs inside ordinary Python.

What you will learn: facts, queries and rules, the three ideas everything else is built from; recursion, and why the order of conditions decides whether a program works; saying what is not true, and calculating inside rules; totals, counts, rankings and cheapest routes; logic over your own Python objects and over the rows of a SQL database; two small applications (an access policy, and parts and dependencies) and a classic puzzle; where pyDatalog stops, how to debug a rule, and where to go next.

A gentle path in: every hard idea gets a story, a picture and a small example worked out by hand before the code, followed by the trap that catches almost everyone and one sentence to remember. Dara, a robot librarian, guides you and asks the questions a beginner would ask. Where a program hangs or quietly gives an empty answer, the book shows it, measures it, and tells you what to do.

Code you can trust: every listing was run with Python 3.12 and pyDatalog 0.22.4, and every printed output was captured from that run. A free companion repository holds all the examples, each with its recorded output and a script that re-runs them: github.com/omkamal/learn-logic-programming-in-python

Who it is for: if you write Python and have never written a logic program, this book is for you. You need functions, lists, dictionaries and classes, and a little SQL helps. No logic background is required.

Share this book

Categories

Author

About the Author

Omar Kamal Hosney

Omar Kamal Hosney is a senior data scientist and machine learning engineer based in Cairo, Egypt, with more than 26 years of experience across telecommunications, credit and electronic design automation. He has been Chief Data Scientist and a founding team member at Tribal Credit and Reveel, led software testing at Mentor Graphics, and worked at multinationals including Lucent and IBM.

He is a Six Sigma Black Belt, an ASQ Certified Quality Manager and a Claude Certified Architect (Foundations). He holds an MBA from City University of Seattle and a bachelor's degree in engineering from Cairo University.

Omar is also the author of A Gentle Introduction to Microservices for Python Developers, Advanced GraphRAG & Knowledge Agents, LLM Evaluation & Reliability Engineering, and Diagrams as Code. He publishes three free video courses (23 modules) and more than 30 cheatsheets at omkamal.github.io.

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub