0% found this document useful (0 votes)
3 views

lbobgdt-04-python branch iteration

The document provides an overview of Big Data programming techniques, focusing on flow control, including conditional execution and loops in Python. It explains the use of if, else, and elif statements for conditional execution, as well as for and while loops for repetitive execution. Additionally, it covers logical and comparison operators, the precedence of operations, and examples of using loops and conditional expressions.

Uploaded by

kahetrahd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

lbobgdt-04-python branch iteration

The document provides an overview of Big Data programming techniques, focusing on flow control, including conditional execution and loops in Python. It explains the use of if, else, and elif statements for conditional execution, as well as for and while loops for repetitive execution. Additionally, it covers logical and comparison operators, the precedence of operations, and examples of using loops and conditional expressions.

Uploaded by

kahetrahd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

1/22/2025

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

Operator Meaning Example Result


== equals 1 + 1 == 2 True
!= does not equal 3.2 != 2.5 True
< less than 10 < 5 False
> greater than 10 > 5 True
<= less than or equal to 126 <= 100 False
>= greater than or equal to 5.0 >= 5.0 True

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

Operator Example Result


not not 7 > 0 False
and 9 != 6 and 2 < 3 True
or 2 == 3 or -1 < 5 True

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

not Unary logical operator

and or Binary logical operators

If at same level, operations are evaluated from left to right

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:

if pressure > 66: rain = True


else: rain = False
else: rain = False
else:
if wind > 7.2: rain = True
else:
if pressure > 79: rain = True
else: rain = False

◼ Multiple conditions can be chained with elif ("else if"):


Syntax:
if condition1 :
statements1
elif condition2 :
statements2
else :
statementsx 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

◼ Though it is not compulsory, it is wise to use conditional expression


enclosed in parenthesis to prevent wrong sub-expression groupings.

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!

◼ Exercise: How would we print the "99 Bottles of Beer" song?

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

◼ Exercise: Write a Python program that computes the factorial of


an integer.

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

◼ Exercise: Write code to display and count the factors of a number.

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

Loop Control Statements

break Jumps out of the closest


enclosing loop

continue Jumps to the top of the closest


enclosing loop

pass Does nothing, empty statement


placeholder

9
1/22/2025

Example of break Statement

mysum = 0
for i in range(5, 11, 2):
mysum += i
if mysum == 5:
break
break
mysum += 1
print(mysum)

▪ what happens in this program?

for vs while Loops

for loops while loops


▪ know number of ▪ unbounded number of
iterations iterations
▪ can end early via ▪ can end early via break
break
▪ uses a counter ▪can use a counter but must
initialize before loop and
increment it inside loop
▪ can rewrite a for ▪ may not be able to rewrite
using a while loop a while loop using a for
loop

10
1/22/2025

Strings and Loops


▪ Example: two code snippets that do the same thing
▪ bottom one is more “pythonic”

s = "abcdefgh"

for index in range(len(s)) :


if s[index] == 'i' or s[index] == 'u' :
print("There is an i or u")

for char in s:
if char == 'i' or char == 'u' :
print("There is an i or u")

More Examples of Loops


◼ Similar to perl for loops, iterating through a
list of values

for x in [1,7,13,2]: for x in range(5) :


forloop1.py print(x) forloop2.py print(x)

% python forloop2.py
%python forloop1.py
0
1
1
7
2
13
3
2
4

range(N) generates a list of numbers [0,1, …, n-1]

11

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy