Python Language: Control Flow: The FOSSEE Group
Python Language: Control Flow: The FOSSEE Group
Mumbai, India
1 Control flow
Basic Conditional flow
2 Control flow
Basic Looping
3 Exercises
Outline
1 Control flow
Basic Conditional flow
2 Control flow
Basic Looping
3 Exercises
if/elif/else : branching
while : looping
for : iterating
break, continue : modify loop
pass : syntactic filler
Outline
1 Control flow
Basic Conditional flow
2 Control flow
Basic Looping
3 Exercises
if...elif...else example
Type the following code in an editor & save as ladder.py
x = int(input("Enter an integer: "))
if x < 0:
print(’Be positive!’)
elif x == 0:
print(’Zero’)
elif x == 1:
print(’Single’)
else:
print(’More’)
if...elif...else example
Type the following code in an editor & save as ladder.py
x = int(input("Enter an integer: "))
if x < 0:
print(’Be positive!’)
elif x == 0:
print(’Zero’)
elif x == 1:
print(’Single’)
else:
print(’More’)
Ternary operator
Ternary operator
Outline
1 Control flow
Basic Conditional flow
2 Control flow
Basic Looping
3 Exercises
Outline
1 Control flow
Basic Conditional flow
2 Control flow
Basic Looping
3 Exercises
while: Fibonacci
Example: Fibonacci series
Sum of previous two elements defines the next:
1
Start with: a, b = 0, 1
2
Next element: next = a + b
3
Shift a, b to next values
a = b
b = next
4
Repeat steps 2, 3 when b < 30
while: Fibonacci
Example: Fibonacci series
Sum of previous two elements defines the next:
1
Start with: a, b = 0, 1
2
Next element: next = a + b
3
Shift a, b to next values
a = b
b = next
4
Repeat steps 2, 3 when b < 30
while: Fibonacci
Example: Fibonacci series
Sum of previous two elements defines the next:
1
Start with: a, b = 0, 1
2
Next element: next = a + b
3
Shift a, b to next values
a = b
b = next
4
Repeat steps 2, 3 when b < 30
while: Fibonacci
Example: Fibonacci series
Sum of previous two elements defines the next:
1
Start with: a, b = 0, 1
2
Next element: next = a + b
3
Shift a, b to next values
a = b
b = next
4
Repeat steps 2, 3 when b < 30
while: Fibonacci
Example: Fibonacci series
Sum of previous two elements defines the next:
1
Start with: a, b = 0, 1
2
Next element: next = a + b
3
Shift a, b to next values
a = b
b = next
4
Repeat steps 2, 3 when b < 30
while
In []: a, b = 0, 1
In []: while b < 30:
...: print(b, end=’ ’)
...: next = a + b
...: a = b
...: b = next
...:
...:
while
for . . . range()
range()
range([start,] stop[, step])
range() returns a sequence of integers
The start and the step arguments are optional
stop is not included in the sequence
Documentation convention
Anything within [] is optional
Nothing to do with Python
for . . . range()
Solution
a, b = 0, 1
for i in range(10):
print(b, end=’ ’)
a, b = b, a + b
break example
continue
pass example
Try this:
for i in range(5):
if i % 2 == 0:
pass
else:
print(i, ’is Odd’)
pass: does nothing
Keep Python syntactically happy
Another example:
while True:
pass
FOSSEE Team (FOSSEE – IITB) Control flow 24 / 32
Exercises
Outline
1 Control flow
Basic Conditional flow
2 Control flow
Basic Looping
3 Exercises
Hints
Break problem into easier pieces
How would you solve the problem?
Can you explain to someone else how to solve it?
Some hints
Solution: part 1
x = 153
a = x//100
b = (x%100)//10
c = x%10
Solution: part 2
x = 100
while x < 1000:
print(x)
x += 1 # x = x + 1
Solution
x = 100
while x < 1000:
a = x//100
b = (x%100)//10
c = x%10
if (a**3 + b**3 + c**3) == x:
print(x)
x += 1
1
Start with an arbitrary (positive) integer.
2
If the number is even, divide by 2; if the number is
odd, multiply by 3 and add 1.
3
Repeat the procedure with the new number.
4
It appears that for all starting values there is a
cycle of 4, 2, 1 at which the procedure loops.
Write a program that accepts the starting value and
prints out the Collatz sequence.