Join 36000+ teachers and students using TTIO.
Boolean operators
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:
BRACKETS/ORDERS/DIVIDE/MULTIPLY/ADD/SUBTRACT
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.
Operator | Name and description | Example |
---|---|---|
AND | Both operands (inputs) need to be True for the result to be True | a = 5, b = 6: a > 3 AND b > 3 = True |
OR | 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 |
NOT | 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 |
Bitwise Operators
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 -- The COMPLEMENT operator is used to invert all of the bits of the operand.www.teachyourselfpython.com