You are on page 1of 2

Module 3

PYTHON OPERATORS Operator Description


Operators and True if both statements are true
- used to perform operations or True if one of the statements is true
Arithmetic operators Not Reverse the result
- for mathematical operations Operator Description
Assignment operators Is True if both variables are the same object
- to assign values to variables Is not True if both variables are not same object
Comparison operators
- to compare two values MEMBERSHIP OPERATORS
Logical operators OPERATOR DESCRIPTION
- to combine conditional statements in True if a sequence with the specified value
Identity operators is present in the object
- used to compare the objects, not if they are equal, Not in True if a sequence with the specified value
but if they are actually the same object, with the is not present in the object
same memory location
Membership operators Module 4
- to test if a sequence is presented in an object CONDITIONAL STATEMENTS AND LOOPING
Bitwise operators
- to compare binary numbers conditional statement
ARITHMETIC OPERATORS - is a statement that can be written in the form “If P
Operator Name then Q,”
- where P and Q are sentences.
+ Addition - measures a condition will generate true or false
- Subtraction statements depending on the processing of input
* Multiplication and output data.
/ Division P – hypothesis
% Modulus Q – conclusion
** Exponentiation
IF STATEMENT
ASSIGNMENT OPERATORS - starts with using the (if) keyword.
Operator Interpretation - indention is important in establishing conditional
= x=5 statements to define the scope of the code.
+= x=x+3 - If there is no indention within the if statement,
-= x=x-3 there will be an error in the program.
*= x=x*3 - Don’t forget the colon ( : ) symbol after the
/= x=x/3 condition.
%= x=x%3 ELIF
//= x = x // 3 - “if the previous conditions were not true, then try
**= x = x ** 3 this condition”
&= x=x&3 ELSE
|= x=x|3
- catches anything which isn’t caught by the
^= x=x^3
preceding conditions.
>>= x = x >> 3
Short Hand If
<<= x = x << 3
- if (b>c); print (“lesser”)
- If statements can be written on the same line
COMPARISON OPERATORS
And function (and)
- used to test if both statements are true
- if a>b and c>a:
LOGICAL OPERATORS
OR Function
- used to test if either of the statements are true
IDENTITY OPERATORS
- if a>b or c>a:
Nested If
- can have if statements inside if statements
Pass Statement
- if statements cannot be empty, but if you for some
reason have an if statement with no content, put in
the pass statement to avoid getting an error.
Python Loops
Two primitive loop commands of Python:
 while loops
- we can execute a set of statements as long as a
condition is true.
 for loops
- used for iterating over a sequence (either a list, a
tuple, a dictionary, a set or a string)
-
Statements Applicable to While Loop:
 Break Statement
- can stop the loop even if the while condition is true

 Continue Statement
- can stop the current iteration, and continue with
the next

 The else Statement


- can stop the current iteration, and continue with the
next

OTHER IMPLEMENTED FUNCTIONS FOR LOOP


 range (): returns a sequence of numbers, starting
from 0 by default, and increments by 1 (by default),
and ends at a specified number.

 The range () function defaults to 0 as a starting


value, however it is possible to specify the starting
value by adding a parameter: range (2, 6), which
means values from 2 to 6 (but not including 6)

 The range() function defaults to increment the


sequence by 1, however it is possible to specify the
increment value by adding a third parameter:
range(2, 30, 3)

Nested Loops
- is a loop inside a loop. The “inner loop” will be
executed one time for each iteration of the “outer
loop”

You might also like