Branching in python
num = 3
if num > 0:
print(num, "is a positive
number.")
print("This is always printed.")
num = -1
if num > 0:
print(num, "is a positive
number.")
print("This is also always printed.")
Else-if Statement
num = 3.4
if num > 0:
print("Positive number")
elif num == 0:
print("Zero")
else:
print("Negative number")
Python operators
Arithmetic operators in Python
Operator Meaning Example
Add two operands or unary
+ x + y +2
plus
Subtract right operand from
- x – y -2
the left or unary minus
* Multiply two operands x*y
Divide left operand by the right
/ x/y
one (always results into float)
Modulus - remainder of the
% division of left operand by the x % y (remainder of x/y)
right
Floor division - division that
results into whole number
// x // y
adjusted to the left in the
number line
Exponent - left operand raised
** x**y (x to the power y)
x = 15
y=4
# Output: x + y = 19
print('x + y =',x+y)
# Output: x - y = 11
print('x - y =',x-y)
# Output: x * y = 60
print('x * y =',x*y)
# Output: x / y = 3.75
print('x / y =',x/y)
# Output: x // y = 3
print('x // y =',x//y)
# Output: x ** y = 50625
print('x ** y =',x**y)
Comparison operators in Python
Operator Meaning Example
Greater that - True if left
> operand is greater than the x>y
right
Less that - True if left operand is
< x<y
less than the right
Equal to - True if both operands
== x == y
are equal
Not equal to - True if operands
!= x != y
are not equal
Greater than or equal to - True if
>= left operand is greater than or x >= y
equal to the right
Less than or equal to - True if
<= left operand is less than or x <= y
equal to the right
x = 10
y = 12
# Output: x > y is False
print('x > y is ',x>y)
# Output: x < y is True
print('x < y is ',x<y)
# Output: x == y is False
print('x == y is ',x==y)
# Output: x != y is True
print('x != y is ',x!=y)
# Output: x >= y is False
print('x >= y is ',x>=y)
# Output: x <= y is True
print('x <= y is ',x<=y)
Logical operators in Python
Operator Meaning Example
True if both the
and x and y
operands are true
True if either of the
or x or y
operands is true
True if operand is false
not (complements the not x
operand)
x = True
y = False
# Output: x and y is False
print('x and y is',x and y)
# Output: x or y is True
print('x or y is',x or y)
# Output: not x is False
print('not x is',not x)
Bitwise operators
Operator Meaning Example
& Bitwise AND x& y = 0 (0000 0000)
| Bitwise OR x | y = 14 (0000 1110)
~ Bitwise NOT ~x = -11 (1111 0101)
^ Bitwise XOR x ^ y = 14 (0000 1110)
>> Bitwise right shift x>> 2 = 2 (0000 0010)
<< Bitwise left shift x<< 2 = 42 (0010 1000)
Assignment operators in Python
Operator Example Equivatent to
= x=5 x=5
+= x += 5 x=x+5
-= x -= 5 x=x-5
*= x *= 5 x=x*5
/= x /= 5 x=x/5
%= x %= 5 x=x%5
//= x //= 5 x = x // 5
**= x **= 5 x = x ** 5
&= x &= 5 x=x&5
|= x |= 5 x=x|5
^= x ^= 5 x=x^5
>>= x >>= 5 x = x >> 5
<<= x <<= 5 x = x << 5
Identity operators in Python
Operator Meaning Example
True if the operands are
is identical (refer to the x is True
same object)
True if the operands are
is not not identical (do not refer x is not True
to the same object)
x1 = 5
y1 = 5
x2 = 'Hello'
y2 = 'Hello'
x3 = [1,2,3]
y3 = [1,2,3]
# Output: False
print(x1 is not y1)
# Output: True
print(x2 is y2)
# Output: False
print(x3 is y3)
Membership operator
Operator Meaning Example
True if value/variable is
in 5 in x
found in the sequence
True if value/variable is not
not in 5 not in x
found in the sequence
x = 'Hello world'
# Output: True
print('H' in x)
# Output: True
print('hello' not in x)