A Python library designed for performing Boolean arithmetic. It includes classes for, evaluating Boolean constant expressions and generating truth tables from a given Boolean expression.
This class creates a way to store and solve Boolean expressions that only contain constants.
Code:
from Boolean_Constant_Expression import boolean_constant_expression
expression = boolean_constant_expression("1 * 0 + 1")
print(expression)
Output:
1 * 0 + 1 = 1
- '1' stands for true. It indicates the affirmative or a positive state in logical expression.
- '0' stands for false. It indicates the negative or opposite state in a logical expression.
- '!' represents the logical NOT operation, which inverts the truth value of the operand immediately following it.
- '+' symbol represents the logical OR operation. This operation evaluates to True ('1') if at least one of the operands is True.
- '*' symbol represents the logical AND operation. This operation evaluates to True ('1') if both operands are true.
- "()" like in standard arthritic, in Boolean arithmetic, the parentheses "()" are used to group expressions.
boolean_constant_expression()
creates boolean_constant_expression
from a given expression
, which the answer for is stored in output
.
expression
: this parameter accepts a Boolean constant expression in the form of astr
, such as "1 + 1", "1 + (0 * 1)", or "0 + !(1 + 0)".- This can later be accessed or changed using the property
boolean_constant_expression.expression
- This can later be accessed or changed using the property
output_format
: this parameter determines how the answer is going to look when theboolean_constant_expression
gets printed.- What are the
types
that can be passed to theoutput_format
parameter?int
: by default, this parameter is set toint
. Meaning that whenboolean_constant_expression
gets printed the answer would appear as a '1' or a '0'.- Example:
from Boolean_Constant_Expression import boolean_constant_expression equ = boolean_constant_expression("0 + !(1 + 0)") print(equ)
0 + !(1 + 0) = 0
- Example:
bool
: when this parameter is set tobool
, the answer ofboolean_constant_expression
gets printed as "True" or "False".- Example:
from Boolean_Constant_Expression import boolean_constant_expression equ = boolean_constant_expression("0 + !(1 + 0)", output_format = bool) print(equ)
0 + !(1 + 0) = False
- Example:
- Note that this parameter can be manipulated or accessed later by the property
- What are the
-
boolean_constant_expression.expression
: in aboolean_constant_expression
, it is the Boolean constant expression that gets solved.- This property can be updated with any Boolean constant expression formatted as a
str
. Updating this property will trigger a recalculation of theoutput
.- Example:
from Boolean_Constant_Expression import boolean_constant_expression equ = boolean_constant_expression("1 * !(0 + 1)") print(equ) equ.expression = "1 * !(0 + 0)" print(equ)
1 * !(0 + 1) = 0 1 * !(0 + 0) = 1
- Example:
- This property can be updated with any Boolean constant expression formatted as a
-
boolean_constant_expression.output
: holds the answer for theexpression
of itsboolean_constant_expression
in the form ofint
0 or 1.- This property can be safely edited to hold
int
0 or 1, but the propertyvalid
would be set to False.- Example:
from Boolean_Constant_Expression import boolean_constant_expression equ = boolean_constant_expression("1 * 1") print("Valid: ", equ.valid) print(equ) equ.output = 0 print() print("Valid: ", equ.valid) print(equ)
Valid: True 1 * 1 = 1 Valid: False 1 * 1 = 0
- Example:
- This property can be safely edited to hold
-
boolean_constant_expression.valid
: shows whether the output has been manually changed.- When
valid
of aboolean_constant_expression
is set toTrue
, theoutput
gets recalculated using its currentexpression
- Example:
from Boolean_Constant_Expression import boolean_constant_expression equ = boolean_constant_expression("1 * 1") print("Valid: ", equ.valid) print(equ) equ.output = 0 print() print("Valid: ", equ.valid) print(equ) equ.valid = True print() print("Valid: ", equ.valid) print(equ)
Valid: True 1 * 1 = 1 Valid: False 1 * 1 = 0 Valid: True 1 * 1 = 0
- Example:
- When
-
boolean_constant_expression.output_format
: in aboolean_constant_expression
, it decides the format of the answer when printed.- What types can this property hold?
int
: this property is initialized as anint
by default. This means that the answer of aboolean_constant_expression
will be printed as either a '1' or a '0'.- Example:
from Boolean_Constant_Expression import boolean_constant_expression equ = boolean_constant_expression("1 * 1") print(equ)
1 * 1 = 1
- Example:
bool
: this property can also be set tobool
, when this is the case, the answer of aboolean_constant_expression
would print as "True" or "False".- Example:
from Boolean_Constant_Expression import boolean_constant_expression equ = boolean_constant_expression("1 * 1") equ.output_format = bool print(equ)
1 * 1 = True
- Example:
- What types can this property hold?
This class creates truth tables for a Boolean algebraic expression.
Code:
from Truth_Table import truth_table
table = truth_table("A + !(B * C)")
print(table)
Output:
in | A + !(B * C) | out
-----|--------------|-----
000 | 0 + !(0 * 0) | 1
001 | 1 + !(0 * 0) | 1
010 | 0 + !(0 * 1) | 1
011 | 1 + !(0 * 1) | 1
100 | 0 + !(1 * 0) | 1
101 | 1 + !(1 * 0) | 1
110 | 0 + !(1 * 1) | 0
111 | 1 + !(1 * 1) | 1
The variables in expression
are any character in the expression
that has an ASCII value that is between 65-122 (A-Z or a-z).
The characters for each input
in inputs
, replaces the variables in expression
depending on the alphabetical order of the variables in expression
. Meaning that in the expression
"B + A", the input
"01", would replace the variable 'A' with 0 and the variable 'B' with 1 which would be "1 + 0".
truth_table()
generates a truth_table
from a given expression
.
expression
: intakes the Boolean algebraic expression of typestr
from which the truth table will be generated which will be stored in the propertyexpression
.- e.g. "A + B", "A * C"
-
has_header
: this parameter passes its value to the propertyhas_header
, which determines whether avalid
truth_table
gets printed with its header.- By default, it is set to
True
. So, a defaultvalid
truth_table
would print with its header.- Example:
from Truth_Table import truth_table expression = "a * !b" table = truth_table(expression) print(table)
in | a * !b | out ----|--------|----- 00 | 0 * !0 | 0 01 | 1 * !0 | 1 10 | 0 * !1 | 0 11 | 1 * !1 | 0
- Example:
- When
has_header
is False, thetruth_table
would be printed without a header.- Example:
from Truth_Table import truth_table expression = "a * !b" table = truth_table(expression, has_header = False) print(table)
00 | 0 * !0 | 0 01 | 1 * !0 | 1 10 | 0 * !1 | 0 11 | 1 * !1 | 0
- Example:
- Note that when the
truth_table
is notvalid
thetruth_table
will print without a header even ifhas_header
has been set toTrue
.- For example:
from Truth_Table import truth_table from Boolean_Constant_Expression import boolean_constant_expression expression = "a * !b" table = truth_table(expression) print("valid: ", table.valid) print("has_header: ", table.has_header) print(table) print() table.table["01"] = boolean_constant_expression("1 + !1") print("valid: ", table.valid) print("has_header: ", table.has_header) print(table)
valid: True has_header: True in | a * !b | out ----|--------|----- 00 | 0 * !0 | 0 01 | 0 * !1 | 0 10 | 1 * !0 | 1 11 | 1 * !1 | 0 valid: False has_header: True 00 | 0 * !0 | 0 01 | 1 + !1 | 1 10 | 1 * !0 | 1 11 | 1 * !1 | 0
- For example:
- By default, it is set to
-
output_format
: this parameter initiates propertyoutput_format
which specifies the format for displaying atruth_table
's outputs.- What does this parameter accept?
- By default,
output_format
is set toint
meaning that the outputs will either be a '1' or a '0'.- Example:
from Truth_Table import truth_table expression = "A + !(B * C)" table = truth_table(expression) print(table)
in | A + !(B * C) | out -----|--------------|----- 000 | 0 + !(0 * 0) | 1 001 | 1 + !(0 * 0) | 1 010 | 0 + !(0 * 1) | 1 011 | 1 + !(0 * 1) | 1 100 | 0 + !(1 * 0) | 1 101 | 1 + !(1 * 0) | 1 110 | 0 + !(1 * 1) | 0 111 | 1 + !(1 * 1) | 1
- Example:
- If the
output_format
is set tobool
, in atruth_table
, the outputs will be printed as "True" or "False".- Example:
from Truth_Table import truth_table expression = "A + !(B * C)" table = truth_table(expression, output_format = bool) print(table)
in | A + !(B * C) | out -----|--------------|----- 000 | 0 + !(0 * 0) | True 001 | 0 + !(1 * 0) | True 010 | 1 + !(0 * 0) | True 011 | 1 + !(1 * 0) | True 100 | 0 + !(0 * 1) | True 101 | 0 + !(1 * 1) | False 110 | 1 + !(0 * 1) | True 111 | 1 + !(1 * 1) | True
- Example:
- By default,
- What does this parameter accept?
-
inputs
: this parameter initiates the propertyinputs
and establishes the keys for the propertytable
within itstruth_table
, where each key corresponds to aboolean_constant_expression
. Note that the parameterexpression
of eachboolean_constant_expression
is formed by replacing the variables of thetruth_table
'sexpression
, in alphabetical order, with its corresponding key. To learn about which characters are considered variables or to learn about how atruth_table
'sexpression
gets replaced by itsinputs
click here.- What does this parameter accept?
This parameter accepts a
list
ofstr
, each consisting solely of the characters '0' or '1'. The length of eachstr
must be equal to the number of variables in theexpression
of thetruth_table
.- By default, this parameter holds an empty
list
. When this is the case, alist
of all combinations will take its place.- Example:
from Truth_Table import truth_table expression = "B * !(A + C) + A" table = truth_table(expression) print(table)
in | B * !(A + C) + A | out -----|------------------|----- 000 | 0 * !(0 + 0) + 0 | 0 001 | 0 * !(0 + 1) + 0 | 0 010 | 0 * !(1 + 0) + 1 | 1 011 | 0 * !(1 + 1) + 1 | 1 100 | 1 * !(0 + 0) + 0 | 1 101 | 1 * !(0 + 1) + 0 | 0 110 | 1 * !(1 + 0) + 1 | 1 111 | 1 * !(1 + 1) + 1 | 1
- Example:
- When a
list
is provided the keys oftruth_table.table
are going to match thelist
. Note that this includes the order, meaning that thetruth_table
will print in the order of thelist
.- Example:
from Truth_Table import truth_table expression = "B * !(A + C) + A" tests = ["011", "000", "100", "100"] table = truth_table(expression, inputs = tests) print(table)
in | B * !(A + C) + A | out -----|------------------|----- 011 | 1 * !(0 + 1) + 0 | 0 000 | 0 * !(0 + 0) + 0 | 0 100 | 0 * !(1 + 0) + 1 | 1 100 | 0 * !(1 + 0) + 1 | 1
- Example:
- By default, this parameter holds an empty
- What does this parameter accept?
This parameter accepts a
-
truth_table.has_header
: is abool
that is used to decide whether the header of avalid
truth_table
will be printed whentruth_table
gets printed.- By default, it is set to
True
meaning that avalid
truth_table
would print like this:in | B * !(A + C) + A | out -----|------------------|----- 011 | 1 * !(0 + 1) + 0 | 0 000 | 0 * !(0 + 0) + 0 | 0 100 | 0 * !(1 + 0) + 1 | 1 100 | 0 * !(1 + 0) + 1 | 1
- When
has_header
is false, atruth_table
would be printed like this:011 | 1 * !(0 + 1) + 0 | 0 000 | 0 * !(0 + 0) + 0 | 0 100 | 0 * !(1 + 0) + 1 | 1 100 | 0 * !(1 + 0) + 1 | 1
- Note that when the
truth_table
is notvalid
thetruth_table
will print without a header even ifhas_header
has been set toTrue
.- For example:
from Truth_Table import truth_table from Boolean_Constant_Expression import boolean_constant_expression expression = "a * !b" table = truth_table(expression) print("valid: ", table.valid) print("has_header: ", table.has_header) print(table) print() table.table["01"] = boolean_constant_expression("1 + !1") print("valid: ", table.valid) print("has_header: ", table.has_header) print(table)
valid: True has_header: True in | a * !b | out ----|--------|----- 00 | 0 * !0 | 0 01 | 0 * !1 | 0 10 | 1 * !0 | 1 11 | 1 * !1 | 0 valid: False has_header: True 00 | 0 * !0 | 0 01 | 1 + !1 | 1 10 | 1 * !0 | 1 11 | 1 * !1 | 0
- For example:
- By default, it is set to
-
truth_table.output_format
: holds the output format that will be used when thetruth_table
gets printed.- What are states that
output_format
can hold?- By default, it is set to
int
. When this is the case the outputs for each output will either be a '1' or a '0'.- Example:
from Truth_Table import truth_table expression = "(a * b)" table = truth_table(expression) print(table)
in | (a * b) | out ----|---------|----- 00 | (0 * 0) | 0 01 | (0 * 1) | 0 10 | (1 * 0) | 0 11 | (1 * 1) | 1
- Example:
bool
: when set tobool
, the outputs printed by thetruth_table
will appear as True or False.- Example:
from Truth_Table import truth_table expression = "(a * b)" table = truth_table(expression) table.output_format = bool print(table)
in | (a * b) | out ----|---------|----- 00 | (0 * 0) | False 01 | (1 * 0) | False 10 | (0 * 1) | False 11 | (1 * 1) | True
- Example:
- By default, it is set to
- What are states that
-
truth_table.table
: returns an object of typedict
that is supposed to hold all theboolean_constant_expression
s of thetruth_table
.- When
truth_table.valid
is set toTrue
:- Each key in the
dict
is of typestr
and is the same size as the number of variables that are in thetruth_table. expression
. - The
dict
will holdboolean_constant_expression, where the
expressionof each
boolean_constant_expressionwill be the
truth_table's
expressionbut with its variables being replaced by the
boolean_constant_expression's key in alphabetical order. To learn about which characters are considered variables or to learn about how a
truth_table's
expressiongets replaced by its
inputs` click here.
- Each key in the
- When the
truth_table.table
gets modified:- The property
valid
is set toFalse
. - The
truth_table
'sinputs
get set to the keys of the modifiedtruth_table.table
.
- The property
- When
-
truth_table.expression
: holds the Boolean algebraic expression of thetruth_table
.- When the
truth_table.expression
changes:- The
truth_table
gets regenerated with the newexpression
and all possibleinputs
.- Example:
from Truth_Table import truth_table expression = "(a * b)" tests = ["01", "11"] table = truth_table(expression, inputs = tests) print(table) print() table.expression = "C * !(A + B)" print(table)
in | (a * b) | out ----|---------|----- 01 | (1 * 0) | 0 11 | (1 * 1) | 1 in | C * !(A + B) | out -----|--------------|----- 000 | 0 * !(0 + 0) | 0 001 | 0 * !(1 + 0) | 0 010 | 1 * !(0 + 0) | 1 011 | 1 * !(1 + 0) | 0 100 | 0 * !(0 + 1) | 0 101 | 0 * !(1 + 1) | 0 110 | 1 * !(0 + 1) | 0 111 | 1 * !(1 + 1) | 0
- Example:
truth_table.valid
is set toTrue
.
- The
- When the
-
truth_table.inputs
: This property returns alist
of objects of typestr
that correlates to thetruth_table.table
's keys.- When
truth_table.inputs
changes, ifinputs
get set to an emptylist
, thetruth_table
gets regenerated with all possibleinputs
.- Example:
from Truth_Table import truth_table expression = "A + B * C" tests = ["000", "010", "100"] table = truth_table(expression, inputs = tests) print(table) print() table.inputs.clear() print(table)
in | A + B * C | out -----|-----------|----- 000 | 0 + 0 * 0 | 0 010 | 0 + 1 * 0 | 0 100 | 0 + 0 * 1 | 0 in | A + B * C | out -----|-----------|----- 000 | 0 + 0 * 0 | 0 001 | 1 + 0 * 0 | 1 010 | 0 + 1 * 0 | 0 011 | 1 + 1 * 0 | 1 100 | 0 + 0 * 1 | 0 101 | 1 + 0 * 1 | 1 110 | 0 + 1 * 1 | 1 111 | 1 + 1 * 1 | 1
- if
inputs
is changed or set to a non-emptylist
, thetruth_table
will be regenerated with the new or updatedinputs
. To learn about what characters are considered variables? or to learn about how atruth_table
'sexpression
gets replaced by itsinputs
click here.- Note that the order in which the
truth_table.table
stores its items and prints them can be manipulated by the order ofinputs
.- Example:
from Truth_Table import truth_table expression = "A + B * C" table = truth_table(expression) print(table) print() table.inputs = tests = ["000", "010", "000", "011", "011"] print(table)
in | A + B * C | out -----|-----------|----- 000 | 0 + 0 * 0 | 0 001 | 0 + 1 * 0 | 0 010 | 1 + 0 * 0 | 1 011 | 1 + 1 * 0 | 1 100 | 0 + 0 * 1 | 0 101 | 0 + 1 * 1 | 1 110 | 1 + 0 * 1 | 1 111 | 1 + 1 * 1 | 1 in | A + B * C | out -----|-----------|----- 000 | 0 + 0 * 0 | 0 010 | 1 + 0 * 0 | 1 000 | 0 + 0 * 0 | 0 011 | 1 + 1 * 0 | 1 011 | 1 + 1 * 0 | 1
- Example:
- Note that the order in which the
truth_table.valid
is set toTrue
.
- Example:
- When
-
truth_table.valid
: is of typebool
. It is used to determine whether thetruth_table.table
has been edited manually.- When
truth_table.valid
is set toTrue
: thetruth_table
gets recreated with the currentinputs
- Example:
from Truth_Table import truth_table from Boolean_Constant_Expression import boolean_constant_expression expression = "A * B" tests = ["01", "11"] table = truth_table(expression, inputs = tests) print("Valid: ", table.valid) print(table) print() table.table["10"] = boolean_constant_expression("1 + 1") print("Valid: ", table.valid) print(table) print() table.valid = True print("Valid: ", table.valid) print(table)
in | A * B | out ----|-------|----- 01 | 0 * 1 | 0 11 | 1 * 1 | 1 Valid: False 01 | 0 * 1 | 0 11 | 1 * 1 | 1 10 | 1 + 1 | 1 Valid: True in | A * B | out ----|-------|----- 01 | 0 * 1 | 0 11 | 1 * 1 | 1 10 | 1 * 0 | 0
- Example:
- When