Python 2 - Basic Operators
Python 2 - Basic Operators
Basic Operators
Arithmetic Operators
Comparison (Relational) Operators
Assignment Operators
Logical Operators
Bitwise Operators
Membership Operators
Python Arithmetic Operators
- Subtraction Subtracts right hand operand from left hand operand. a – b = -10
Floor Division - The division of operands
where the result is the quotient in which
9//2 = 4 and 9.0//2.0 = 4.0, -11//3 = -4, -
// the digits after the decimal point are
11.0//3 = -4.0
removed. But if one of the operands is
negative, the result is floored, i.e.,
rounded away from zero (towards
negative infinity) −
Python Comparison Operators
~+-
2 Complement, unary plus and minus (method names for the last
two are +@ and -@)
* / % //
3
Multiply, divide, modulo and floor division
+-
4
Addition and subtraction
>> <<
5
Right and left bitwise shift
&
6
Bitwise 'AND'
^|
7
Bitwise exclusive `OR' and regular `OR'
<= < > >=
8
Comparison operators
<> == !=
9
Equality operators
= %= /= //= -= += *= **=
10
Assignment operators
is is not
11
Identity operators
in not in
12
Membership operators
not or and
13
Logical operators
Python - Decision Making
if...else statements
2 An if statement can be followed by an optional else statement, which executes
when the boolean expression is FALSE.
nested if statements
3
You can use one if or else if statement inside another if or else if statement(s).
Python - IF Statement
if expression:
statement(s)
var2 = 0
if var2:
print ("2 - Got a true expression value")
print (var2)
if expression:
statement(s)
else:
statement(s)
Flow Diagram
The elif Statement
if expression1:
statement(s)
elif expression2:
statement(s)
elif expression3:
statement(s)
else:
statement(s)
Core Python does not provide switch or case
statements as in other languages, but we can use
if..elif... statements to simulate switch case.
Python - Nested IF Statements
if expression1:
statement(s)
if expression2:
statement(s)
elif expression3:
statement(s)
else
statement(s)
elif expression4:
statement(s)
else:
statement(s)
Python - Loops
for loop
2 Executes a sequence of statements multiple times and abbreviates the code that
manages the loop variable.
nested loops
3
You can use one or more loop inside any another while, or for loop.
Python - while Loop Statements
while expression:
statement(s)
statement(s) may be a single statement or a
block of statements with uniform indent.
The condition may be any expression, and true is
any non-zero value.
The loop iterates while the condition is true.
When the condition becomes false, program
control passes to the line immediately following
the loop.
In Python, all the statements indented by the
same number of character spaces after a
programming construct are considered to be part
of a single block of code.
Python uses indentation as its method of
grouping statements.
Flow Diagram
a key point of the while loop is that the loop
might not ever run.
When the condition is tested and the result is
false, the loop body will be skipped and the first
statement after the while loop will be executed.
Example
count = 0
while (count < 9):
print ('The count is:', count)
count = count + 1
print ("Good bye!")
Output
The count is: 0
The count is: 1
The count is: 2
The count is: 3
The count is: 4
The count is: 5
The count is: 6
The count is: 7
The count is: 8
Good bye!
The block here, consisting of the print and
increment statements, is executed repeatedly until
count is no longer less than 9.
With each iteration, the current value of the index
count is displayed and then increased by 1.
The Infinite Loop
flag = 1
while (flag): print ('Given flag is really true!')
print ("Good bye!")
The above example goes into an infinite loop and you need
to press CTRL+C keys to exit.
Python - for Loop Statements
Output
0
1
2
3
4
Example
for letter in 'Python': # traversal of a string sequence
print ('Current Letter :', letter)
print()
1. break statement
2. continue statement
3. pass statement
break statement
Continue
Flow Diagram
Example
for letter in 'Python': # First Example
if letter == 'h':
Continue
print ('Current Letter :', letter)
var = 10 # Second Example
while var > 0:
var = var -1
if var == 5:
Continue
print ('Current variable value :', var)
print ("Good bye!")
Output
Current Letter : P
Current Letter : y
Current Letter : t
Current Letter : o
Current Letter : n
Current variable value : 9
Current variable value : 8
Current variable value : 7
Current variable value : 6
Current variable value : 4
Current variable value : 3
Current variable value : 2
Current variable value : 1
Current variable value : 0
Good bye!
Python - pass Statement
Pass
Example
for letter in 'Python':
if letter == 'h':
Pass
print ('This is pass block')
print ('Current Letter :', letter)
print ("Good bye!")
Output
Current Letter : P
Current Letter : y
Current Letter : t
This is pass block
Current Letter : h
Current Letter : o
Current Letter : n
Good bye!