Skip to main content
This tutorial introduces the basic concepts of Z3 through simple constraint solving examples. You’ll learn how to create variables, add constraints, and check satisfiability.

Simple Arithmetic Constraints

Let’s start with a basic example that solves arithmetic constraints using real numbers.
1

Import Z3

First, import the Z3 library to access all solver functions:
This imports all Z3 classes and functions, including Real, Solver, and constraint operators.
2

Create variables

Create variables that represent unknown values you want to find:
Real('x') creates a real-valued variable named ‘x’. Z3 also supports Int, Bool, and other types.
3

Create a solver and add constraints

Create a solver instance and add your constraints:
The add() method accepts multiple constraints. Here we’re saying:
  • The sum of x and y must be greater than 5
  • x must be greater than 1
  • y must be greater than 1
4

Check satisfiability and get a model

Ask Z3 to find a solution:
The check() method returns:
  • sat - constraints are satisfiable (solution exists)
  • unsat - no solution exists
  • unknown - solver couldn’t determine
If satisfiable, model() returns concrete values that satisfy all constraints.

Complete Example

Expected output:
The actual values may vary - Z3 finds any solution that satisfies the constraints, not necessarily a specific one.

Integer Constraints

Z3 can also solve integer programming problems.
1

Create integer variables

2

Add constraints

This finds three non-negative integers that sum to 10, where a is greater than b.
3

Find a solution

Expected output:

Boolean Satisfiability (SAT)

Z3 can solve classic SAT problems using boolean variables.
Expected output:

Checking Unsatisfiability

Sometimes constraints have no solution:
Expected output:
When check() returns unsat, calling model() will raise an exception since no solution exists.

Key Concepts

  • Int('name') - Integer variables
  • Real('name') - Real number variables
  • Bool('name') - Boolean variables
  • BitVec('name', size) - Bit-vector variables
  • Arithmetic: +, -, *, /, %
  • Comparison: ==, !=, <, <=, >, >=
  • Boolean: And(), Or(), Not(), Implies()
  • Special: Distinct() - ensures all arguments are different
  • sat - Satisfiable (solution found)
  • unsat - Unsatisfiable (no solution exists)
  • unknown - Solver timed out or couldn’t determine

Next Steps

Sudoku Solver

Apply these concepts to build a complete Sudoku solver

Optimization Problems

Learn to find optimal solutions using Z3’s Optimize solver