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_expressiongets printed.- What are the
typesthat can be passed to theoutput_formatparameter?int: by default, this parameter is set toint. Meaning that whenboolean_constant_expressiongets 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_expressiongets 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
boolean_constant_expression.output_format`.
- 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 theexpressionof itsboolean_constant_expressionin the form ofint0 or 1.- This property can be safely edited to hold
int0 or 1, but the propertyvalidwould 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
validof aboolean_constant_expressionis set toTrue, theoutputgets 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 anintby default. This means that the answer of aboolean_constant_expressionwill 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_expressionwould 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 typestrfrom 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 avalidtruth_tablegets printed with its header.- By default, it is set to
True. So, a defaultvalidtruth_tablewould 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_headeris False, thetruth_tablewould 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_tableis notvalidthetruth_tablewill print without a header even ifhas_headerhas 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_formatwhich specifies the format for displaying atruth_table's outputs.- What does this parameter accept?
- By default,
output_formatis set tointmeaning 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_formatis 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 propertyinputsand establishes the keys for the propertytablewithin itstruth_table, where each key corresponds to aboolean_constant_expression. Note that the parameterexpressionof eachboolean_constant_expressionis 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'sexpressiongets replaced by itsinputsclick here.- What does this parameter accept?
This parameter accepts a
listofstr, each consisting solely of the characters '0' or '1'. The length of eachstrmust be equal to the number of variables in theexpressionof thetruth_table.- By default, this parameter holds an empty
list. When this is the case, alistof 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
listis provided the keys oftruth_table.tableare going to match thelist. Note that this includes the order, meaning that thetruth_tablewill 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 aboolthat is used to decide whether the header of avalidtruth_tablewill be printed whentruth_tablegets printed.- By default, it is set to
Truemeaning that avalidtruth_tablewould 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_headeris false, atruth_tablewould 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_tableis notvalidthetruth_tablewill print without a header even ifhas_headerhas 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_tablegets printed.- What are states that
output_formatcan 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_tablewill 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 typedictthat is supposed to hold all theboolean_constant_expressions of thetruth_table.- When
truth_table.validis set toTrue:- Each key in the
dictis of typestrand is the same size as the number of variables that are in thetruth_table. expression. - The
dictwill holdboolean_constant_expression, where theexpressionof eachboolean_constant_expressionwill be thetruth_table'sexpressionbut with its variables being replaced by theboolean_constant_expressiontruth_table.table's key in alphabetical order. To learn about which characters are considered variables or to learn about how atruth_table'sexpressiongets replaced by itsinputs` click here.
- Each key in the
- When the
truth_table.tablegets modified:- The property
validis set toFalse. - The
truth_table'sinputsget 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.expressionchanges:- The
truth_tablegets regenerated with the newexpressionand 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.validis set toTrue.
- The
- When the
-
truth_table.inputs: This property returns alistof objects of typestrthat correlates to thetruth_table.table's keys.- When
truth_table.inputschanges, ifinputsget set to an emptylist, thetruth_tablegets 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
inputsis changed or set to a non-emptylist, thetruth_tablewill be regenerated with the new or updatedinputs. To learn about what characters are considered variables? or to learn about how atruth_table'sexpressiongets replaced by itsinputsclick here.- Note that the order in which the
truth_table.tablestores 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.validis set toTrue.
- Example:
- When
-
truth_table.valid: is of typebool. It is used to determine whether thetruth_table.tablehas been edited manually.- When
truth_table.validis set toTrue: thetruth_tablegets 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