Professional Documents
Culture Documents
Basic Phyton: Conditions, Iterations & Loops: Arif - Boma - Bhayu - Jessy
Basic Phyton: Conditions, Iterations & Loops: Arif - Boma - Bhayu - Jessy
Logical Operators
Bitwise Operators
Membership Operators
Identity Operators
OPERATORS
Phyton language supports the following types of operators
Arithmetic Operators
Operator Name Example
+ Addition x+y
- Subtraction x-y
* Multiplication x*y
/ Multiplication x/y
% Modulus x%y
** Exponentiation x ** y
// Floor division x // y
Modified from :
w3schools
Comparison Relational Operators
Operator Name Example
== Equal x == y
!= Not Equal x != y
Modified from :
w3schools
Assignment Operators
Operator Name Same As
= x=5 x=5
+= x += 3 x=x+3
-= x -= 3 x=x-3
*= x *= 3 x=x*3
/= x /= 3 x=x/3
%= x %= 3 x=x%3
//= x //= 3 x = x // 3
**= x //= 3 x = x ** 3
&= x &= 3 x=x&3
|= x |= 3 x=x|3
^= x |= 3 x=x^3
>>= x >>= 3 x = x >> 3
Modified from :
<<= x <<= 3 x = x << 3 w3schools
Python Logical Operators
Operator Description Example
and Returns True if both x < 5 and x < 10
statements are true
or Returns True if one of the x < 5 or x < 4
statements is true
not Reverse the result, returns not(x < 5 and x < 10)
False if the result is true
and Returns True if both x < 5 and x < 10
statements are true
or Returns True if one of the x < 5 or x < 4
statements is true
not Reverse the result, returns not(x < 5 and x < 10)
False if the result is true
Modified from :
w3schools
Python Bitwise Operators
Operator Name Description
& AND Sets each bit to 1 if both bits are
1
| OR Sets each bit to 1 if one of two
bits is 1
^ XOR Sets each bit to 1 if only one of
two bits is 1
~ NOT Inverts all the bits
Modified from :
w3schools
Python Identity Operators
Operator Description Example
Is Returns true if both x is y
variables are the same
object
is not X is y
Returns true if both
variables are not the same
object
Modified from :
w3schools
CONTROL FLOW
The order in which individual statements, instructions or function calls of an
imperative program are executed or evaluated.
If, Elif, Else
If, elif
If is a syntax used to control program execution by creating conditional
statement. The control is based on Boolean expression written after if syntax.
Creating conditional statements can be using logical operators.
Elif is used to complement the conditions of if when if statement is not true,
then the program will run elif statement.
Else is a syntax used to complement any conditions which were not covered
previously by if of elif.
Nested If
If-Else statement can be executed without indentation when it is written in one line.
EXCEPTIONS
Exceptions are errors happened due to external factors
(wrong data types, wrong operations, etc.)
Try-except are the syntax to control exceptions handling.
BASIC PYTHON II:
LOOP
Iteration
Flowchart Iteration
Definition
True
Purpose Code Inside body
of While Loop
To simplify programming and to streamline
program instructions. End of
Loop
Elements of Iteration
True
statements for adding /
Update Counter subtracting a counter after one Statement
iteration is finished
Type of Iteration
Iteration that are carried out without any information on how many times it is
Indefinite repeated. The iteration will run as long as one condition is met.
Definite Iteration that are performed as long as the conditions are still met and within the
limit of the number of repetitions that will be performed.
Syntax While
Definition
Same as the if-else syntax, while is followed by processing a logical expression indicating the conditions for
executing the iteration under it. The loop syntax also uses indentation in iteration writing and the use of a colon
as the opening of the indent
Iteration Flowchart of the following program
count = 0
Is count < 10
No
Stop
Infinite Loop
This loop occurs if the logical expression always returns True or the counter variable used is
not updated every one iteration is finished. because of that, in implementing a loop, we need
to pay attention to whether the loop will have infinite loops or not
Example Program :
While statement usage
Besides that, in the while statement we can also add other operations, such as
addition, multiplication, or so on. The flowchart below is a logical sequence to
get the sum of the first 5 numbers.
Example Program :
Nested Loop Flowchart for Nested Loop
Nested loops are loops that occur in a loop. so that
the number of operations to be performed as many as Initialization
outside loops is multiplied by inside loops.
End for
End for
Range Function
Example :
Definition
Test False
Expression
of Loop
Test False
Expression
of Loop
Definition