A Boolean data type is a binary variable that can have one of two possible values, 0 (False) or 1 (True). We can combine Booleans using Boolean operators AND, OR, NOT.
Order of operations
The rules of programming follow the same rules as maths in terms of the order in which operators are carried out.
They follow the rules of BODMAS:
Operators in brackets are carried out first, then orders like powers and roots, then division, multiplication, addition and, finally, subtraction.
For example, Order of Operations in programming a Celsius to Fahrenheit conversion:
-celsius = float(input('Enter temperature celsius: '))
-fahrenheit = celsius * 9/5 + 32
-print(str(celsius) + ' = ' + str(fahrenheit))
Line 1: The user is asked to input a value. This value is stored in the Celsius variable as a float.
Line 2: BODMAS – Division (9/5 = 1.8), Multiplication (1.8 * Celsius), Addition (+32).
Line 3: Program outputs the temperature in Celsius and Fahrenheit.
||Name and description
||Both operands (inputs) need to be True for the result to be True
||a = 5, b = 6: a > 3 AND b > 3 = True
||If either or both of the operands are true then the result will be True
||a = 5, b = 6: a >= 6 OR b >=6 = True
||The result will be the opposite of the operand given
||a = False, NOT a = True
||Is equal to – the left operand is equal to the right
||2 == 2 would be True
|<> or !=
||Is not equal to – the two operands are not equal
||2 != 1 would be True
||Is less than – the left operand is less than the right
The bitwise operators are similar to the logical operators, except that they work on a smaller scale -- binary representations of data.
Bitwise AND of 4-bit integers (below)
The following operators are available:
- op1 & op2 -- The AND operator compares two bits and generates a result of 1 if both bits are 1; otherwise, it returns 0.
- op1 | op2 -- The OR operator compares two bits and returns 1 if either or both of the bits are 1 and it gives 0 if both bits are 0.
- op1^ op2 -- The EXCLUSIVE-OR operator compares two bits and generates a result of 1 if the bits are complementary; otherwise, it returns 0.
~op1 -- The COMPLEMENT operator is used to invert all of the bits of the operand.
- op1 >> op2 -- The SHIFT RIGHT operator moves the bits to the right, discards the far right bit, and assigns the leftmost bit a value of 0.