A Ternary expression is an expression that has 3 terms, where a binary expression has two terms an unary expression has one term and a boolean expression has only two states, 1 or 0, yes or no, true of false.
For logic circuits boolean is usually expressed as 1 or 0. On or Off though for performance variances are typically used in reality versus theory.
The C programming languages accept 0 as false and non zero value as true. In Python True is an Instance of the bool class and so is False eg:
# Checking the type of the objects
print(type(True)) # Output: <class 'bool'>
print(type(False)) # Output: <class 'bool'>
# Checking if they are instances of the bool class
print(isinstance(True, bool)) # Output: True
The concept of boolean logic is that there are two states and only two states. Boolean does not deal with maybes or possibilities. Not that a possibility could not be narrowed down by a chain of boolean states though no one boolean expression can consider more than true or false, yes or no, 0 or 1, etc ...
a binary operator is one that deals with two expression or in mathematics two terms. For instance 4 + 4 is where + is a binary operator with two terms or what we call operands. A = 5 is a binary expression. Now do not get confused because an expression may also in some cases be called a term. Though it really is not that complicated. If an operator has one operand it is unary such as -1 or +1 or ++sum, etc. If it has two operands it is binary 1+2 or A+6 or abs(A) / 2, yes in many languages and in math a function's return value would be used in this expression as one of the terms for this binary expression. Ternary expressions are operators that have 3 operands, in this case it typically takes a combination of operators to define the expression such as: C code:
int isGreater = 0;
/* condition ? value_if_true : value_if_false; */
isGeater = A > B ? 1 : 0
Python code:
# value_if_true if condition else value_if_false
is_greater = 1 if A > B else 0
In python the if operator will cause is_greater to be True or False unlike C's convention of 0 false any non zero value is true.
These are trivial examples meant for explanation and certainly not how you would code it in real life. The C code could be reduced to a simple binary expression isGreater = A > B; and would be preferred in production code but that would not illustrate a ternary operator in its simplest form either .
@unusnemo Well explained in detail, booleans exist in python as well where the whitespace on an expression does not matter but the boolean did the difference for desired effect.
Yes as explained in python boolean values are True and False which are an Instance of the bool class. This is actually significantly more powerful and less error prone than the C invocation of boolean logic.
Ashwin Dixit
in reply to yalocalyeomanvendor • • •yalocalyeomanvendor
in reply to Ashwin Dixit • • •Unus Nemo
in reply to yalocalyeomanvendor • •@yalocalyeomanvendor
A Ternary expression is an expression that has 3 terms, where a binary expression has two terms an unary expression has one term and a boolean expression has only two states, 1 or 0, yes or no, true of false.
For logic circuits boolean is usually expressed as 1 or 0. On or Off though for performance variances are typically used in reality versus theory.
The C programming languages accept 0 as false and non zero value as true.
In Python True is an Instance of the bool class and so is False eg:
The concept of boolean logic is that there are two states and only two states. Boolean does not deal with maybes or possibilities. Not that a possibility could not be narrowed down by a chain of boolean states though no one boolean expression can consider more than true or false, yes or no, 0 or 1, etc ...
a binary operator is one that deals with two expression or in mathematics two terms. For instance 4 + 4 is where + is a binary operator with two terms or what we call operands. A = 5 is a binary expression. Now do not get confused because an expression may also in some cases be called a term. Though it really is not that complicated. If an operator has one operand it is unary such as -1 or +1 or ++sum, etc. If it has two operands it is binary 1+2 or A+6 or abs(A) / 2, yes in many languages and in math a function's return value would be used in this expression as one of the terms for this binary expression. Ternary expressions are operators that have 3 operands, in this case it typically takes a combination of operators to define the expression such as:
C code:
Python code:
In python the if operator will cause is_greater to be True or False unlike C's convention of 0 false any non zero value is true.
These are trivial examples meant for explanation and certainly not how you would code it in real life. The C code could be reduced to a simple binary expression
.
isGreater = A > B;and would be preferred in production code but that would not illustrate a ternary operator in its simplest form eitherlike this
yalocalyeomanvendor and Plan-A̵̛͈̬̥̿͋̓͛̕ like this.
yalocalyeomanvendor reshared this.
yalocalyeomanvendor
in reply to Unus Nemo • • •Unus Nemo likes this.
Unus Nemo
in reply to yalocalyeomanvendor • •@yalocalyeomanvendor
Yes as explained in python boolean values are True and False which are an Instance of the bool class. This is actually significantly more powerful and less error prone than the C invocation of boolean logic.
yalocalyeomanvendor likes this.
yalocalyeomanvendor reshared this.
yalocalyeomanvendor
in reply to Unus Nemo • • •Unus Nemo likes this.
Unus Nemo
in reply to Ashwin Dixit • •You are showing your old school BASICs now
yalocalyeomanvendor likes this.
yalocalyeomanvendor
in reply to Unus Nemo • • •Unus Nemo likes this.