lbobgdt-04-python branch iteration
lbobgdt-04-python branch iteration
BIGDATA
Big Data Techniques
and Technologies
Bobby Reyes
Program
Program Flow
Flow Control:
Control:
Branching
Branching and
and Iteration
Iteration
Conditional Execution – if, else, elif
Repeated Execution (loops) – for, while
1
1/22/2025
Python Programs
▪ Recall:
▪ a program is a sequence of definitions and commands
◦ definitions evaluated
◦ commands executed by Python interpreter in a shell
▪ interpreter executes each instruction in order
◦ use tests to change flow of control through sequence
◦ stop when done
▪
▪control the flow of programs using:
◦ Conditional statements: if, else, elif
◦ Loops: for, while
Comparison Operators
◼ Many logical expressions use relational operators
◼ comparisons evaluate to a Boolean
2
1/22/2025
Logical Operators
◼ Logical expressions can be combined with logical operators:
not a → True if a is False
False if a is True
A B A and B A or B
a and b → True if both are True True True True True
True False False True
a or b → True if either or both are True
False True False True
False False False False
Precedence of Operators
▪ precedence: Order in which operations are evaluated
▪ Parentheses used to tell Python to do these operations first
+ - Unary operators
** Exponentiation has a higher precedence
* / // % Multiplication / Division / Modulo
+ - Addition / Subtraction
SIMPLE OPERATIONS
< > <= >= == != Relational operators
3
1/22/2025
Conditional Execution:
if Statement
◼ if statement: Executes a group of statements only if a certain
condition is True. Otherwise, the statements are skipped.
◼ Syntax:
if condition :
statements a code block
◼ Example:
gpa = 3.4
if gpa > 2.0 :
print("Your application is accepted.")
If-else Statement
◼ if-else statement: Executes one block of statements if
condition is True, and a second block of statements if
condition is False.
◼ Syntax:
if condition :
statements1
else:
statements2
◼ Example:
gpa = 1.4
if gpa > 2.0 :
print("Welcome to Mars University!")
else:
print("Your application is denied.")
4
1/22/2025
Nested if Statements
◼ Possible to nest if-else statements; i.e. combine multiple if-else
statements within each other.
◼ Example:
if temperature > 21:
if wind > 3.2:
Indentation
▪ matters in Python
▪ how you denote blocks of code
▪ Code blocks are logical groupings of commands.
They are always preceded by a colon :
x = float(input("Enter a number for x: "))
y = float(input("Enter a number for y: "))
if x == y:
print("x and y are equal")
equal" )
if y != 0
0::
print("therefore, x / y is", x/y)
x/y ) code blocks
elif x < y:
print("x is smaller")
smaller" )
else:
print("y is smaller")
smaller" )
print("thanks!")
5
1/22/2025
Example of if Statements
import math
x = 30
if x <= 15 :
y = x + 15
elif x <= 30 :
y = x + 30
else : [] %run ifstatement.py
y = x y = -0.3048106211022167
print(‘y = ', []
math.sin(y))
In console
In file ifstatement.py
Conditional Expression
◼ The if statement does not return a value. A non-statement alternative
that has a return value is the conditional expression.
◼ A conditional expression also uses the if and else keywords. This time
if is not at the start of a statement but following a value.
◼ Syntax:
expressionYES if condition else expressionNO
◼ Example:
x = -34.1905
y = (x if x > 0 else -x)**0.5
print(y)
Output:
5.84726431761
6
1/22/2025
Repetitive Execution:
The for Loop
◼ for loop: Repeats a set of statements over a group of values (collection).
◼ Syntax:
for variableName in groupOfValues :
statements
◼ We indent with tabs or spaces the statements (code block) to be repeated.
◼ variableName gives a name to each value, so you can refer to it in the statements.
◼ groupOfValues can be a range of integers, specified with the range function;
can also specify a container (string, list, dictionary, set).
◼ Example:
for x in range(1, 6) :
print(x, "squared is", x * x)
Output:
1 squared is 1
2 squared is 4
3 squared is 9
4 squared is 16
5 squared is 25
range
◼ The range function specifies a range of integers:
range(start, stop) - the integers between start (inclusive)
and stop (exclusive)
◼ It can also accept a third value specifying the change between values.
◼ range(start, stop, step) - the integers between start (inclusive)
and stop (exclusive) by step
◼ Example:
for x in range(5, 0, -1):
print x
print("Blast off!")
Output:
5
4
3
2
1
Blast off!
7
1/22/2025
Cumulative Loops
◼ Some loops incrementally compute a value that is initialized
outside the loop. This is sometimes called a cumulative sum.
sum = 0
for i in range(1, 11):
sum = sum + (i * i)
print("sum of first 10 squares is", sum)
Output:
sum of first 10 squares is 385
while Loop
◼ while loop: Executes a group of statements as long as condition is True.
◼ good for indefinite loops (repeat an unknown number of times)
◼ Syntax:
while condition:
statements
◼ Example:
number = 1
while number < 200:
print(number, end=‘ ‘)
number = number * 2
◼ Output:
1 2 4 8 16 32 64 128
8
1/22/2025
while Loops
[] %run whileloop.py
x = 1 1
while x < 10 : 2
print(x) 3
x = x + 1 4
5
In whileloop.py 6
7
8
9
[]
In console
9
1/22/2025
mysum = 0
for i in range(5, 11, 2):
mysum += i
if mysum == 5:
break
break
mysum += 1
print(mysum)
10
1/22/2025
s = "abcdefgh"
for char in s:
if char == 'i' or char == 'u' :
print("There is an i or u")
% python forloop2.py
%python forloop1.py
0
1
1
7
2
13
3
2
4
11