Download as pdf or txt
Download as pdf or txt
You are on page 1of 31

Basic Phyton :

Conditions, Iterations & Loops

Arif | Boma | Bhayu | Jessy


BASIC PROGRAMMING:
CONDITIONS
Basic Programming : Conditions
Operators Control Flow Exceptions

Arithmetic Operators If, Elif, Else

Comparison Relational Operators Nested If

Assignment Operators If-Else Statement

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

> Greater than x>y

< Less than x<y

>= Greater than or equal to x >= y

<= Less than or equal to 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

<< Zero fill left shift Shift left by pushing zeros in


from the right and let the leftmost
bits fall off
>> Signed right shift Shift right by pushing copies of
the leftmost bit in from the left,
and let the rightmost bits fall off
Modified from :
w3schools
Python Membership Operators
Operator Description Example
in Returns True if a x in y
sequence with the
specified value is present
in the object
not in Returns True if a x not in y
sequence with the
specified value is not
present in the object

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.

Indentation is needed in Phyton as it does not use brackets like other


language.
Else

Else is a syntax used to complement any conditions which were not covered
previously by if of elif.
Nested If

Nested if statement is when you put an if statement inside an if statement.


If-Else Statement

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

Iteration is a special instruction in a programming


Initialization
language and an algorithm that is used to repeat
several commands according to a predetermined
number. False
Condition

True
Purpose Code Inside body
of While Loop
To simplify programming and to streamline
program instructions. End of
Loop
Elements of Iteration

Initialization creation of counter variable


Start Initialization

statement / logical expression:


False Increment/D
Conditional condition for one iteration to be End Condition
executed ecrement

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

In the Python programming language, the


while syntax is used for indefinite type.

The while syntax is as follows:

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

Example Program : Start

count = 0

Print Hello World!

count = count + 1 Yes

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.

Example Program : False Increment/


Condition 1
Decrement
True
False Increment/
Condition 2
Decrement
True
Statements

End for

End for
Range Function
Example :

Definition

The range function generates a sequence of


integers by defining a start and the end point of
the range.
It is generally used with the for loop to iterate over
a sequence of numbers
Break and Continue Statement
Flowchart for Break Statement
Enter Loop
Definition of Break :

Test False
Expression
of Loop

The break statement terminates the loop True


containing it. If the break statement is inside a
Yes
nested loop (loop inside another loop), the break Break
statement will terminate the innermost loop.
No
Remaining Body
of loop
Exit Loop
Break and Continue Statement
Flowchart for Continue Statement
Enter Loop
Definition of Continue :

Test False
Expression
of Loop

The continue statement is used to skip the rest of True


the code inside a loop for the current iteration Yes
only. Loop does not terminate but continues on Continue
with the next iteration.
No
Remaining Body
of loop
Exit Loop
Break and Continue Statement
Example for Break and Continue Statement :
Else Statement
Example :

Definition

The else statement will execute the code below it


when the while process finishes normally. This
statement will not be executed when the looping
process is finished because of the break
statement.

You might also like