0% found this document useful (0 votes)
5 views39 pages

Lecture 07

The document provides an overview of loops in programming, including while loops, for loops, and sentinel loops, which process data until a special value is reached. It discusses the use of functions, including void and fruitful functions, and emphasizes the importance of scope, global vs local variables, and best programming practices. Additionally, it covers function calling methods, boolean functions, and common debugging issues.

Uploaded by

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

Lecture 07

The document provides an overview of loops in programming, including while loops, for loops, and sentinel loops, which process data until a special value is reached. It discusses the use of functions, including void and fruitful functions, and emphasizes the importance of scope, global vs local variables, and best programming practices. Additionally, it covers function calling methods, boolean functions, and common debugging issues.

Uploaded by

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

REVIEW

Computing machines are often


used to automate repetitive tasks.
REVIEW: LOOPS
▪ A loop repeats a sequence of statements

▪ Two types
▪ while loop
▪ for loop
SENTINEL LOOPS
▪A sentinel loop continues to
process data until reaching a
special value that signals the end.
▪This special value is called the
sentinel.
4
from … import …
>>> from math import sqrt

>>> from math import *

>>> sqrt(25)

Difference? Problem?
n=0

What does this loop


n>0? do?
Yes n=0
N while (n >
o 0):
print(‘Hi’) print(‘Hi’)
print('Dry
off!‘)
print('Dry
off!‘)
range

>>> range(1)

>>> range(25)

>>> range(2,10,2)
THE for LOOP

for <variable> in range(<number of


times>):
<statement-1> loop
body
<statement-2>

<statement-n>
USING THE LOOP VARIABLE
▪The loop variable picks up the next value in a
sequence on each pass through the loop
▪The expression range(n) generates a sequence of
ints from 0 through n – 1

>>> for x in range(5):


print(x)
COUNTING FROM x
THROUGH N
▪The expression range(low, high) generates a
sequence of ints from low through high – 1

for x in range(1, 6):

print(x)
ACCUMULATOR LOOP:
SUMMATION
Compute and print the sum of the numbers between 1
and 5, inclusive

total = 0
for n in range(1, 6):
total = total + n total += n
print(total)
NESTED LOOPS

for <variable1> in range(<number of times>):


for <variable2> in range(<number of
times>):
<statement-1>
<statement…>
NESTED LOOPS

while (<Condition>):
while (<Condition>):
<statement-1>
<statement…>
BREAK & CONTINUE

for letter in 'Python': for letter in 'Python':


if letter == 'h': if letter == 'h':
break continue
print(letter) print(letter)
YET ANOTHER IMPORT
STATEMENT

>>> import turtle as t

>>> bob = t.Turtle()


FRUITFUL
FUNCTIONS
FUNCTIONS
The importance of functions:
▪Break your code into separate, independent parts
that will work together to solve the ultimate
problem (DECOMPOSITION).
▪Hide the details of your computation as long as you
know what it produces (ABSTRACTION)
CONT…
The advantages of functions:
▪Break your code into simpler independent
modules
▪These modules can be reused as many times as
you like
▪And they need to be debugged only once
FUNCTION PYTHON
▪Defining functions:
VOID FUNCTIONS

In several programming languages derived


from C and Algol68, is the type for the
result of a function that returns normally,
but does not provide a result value to its
caller.
VOID FUNCTIONS

def greeting(name): def maximum(x,y):


print(‘hi ’,name) if(x > y):
print(x)
greeting(‘Haven’) else:
greeting(‘Robel’) print(y)
maximum(2,4)
<type ‘NoneType’>
▪Void functions might display
something on the screen or have
some other effect, but they don’t
have a return value.

▪If you try to assign the result to a


variable, you get a special value
FRUITFUL FUNCTIONS

A function that returns a


value to the caller
RETURN VS PRINT

def greeting(name): def maximum(x,y):


print(‘hi ’,name) if(x > y):
return x
greeting(‘Haven’) else:
greeting(‘Robel’) return y
maximum(2,4)
DEAD CODE
When a return statement executes,
the function terminates without
executing any subsequent
statements.
Code that appears after a return
statement, or any other place the flow
of execution can never reach, is called
dead code.
SCOPE
A variable can have something
called a ‘scope’. This refers to
its accessibility.
SCOPE
A variable can be declared in one of two ways…

When it is accessible from all


When it is accessible ONLY
parts of a program
within a function/procedure.
(ie. Anywhere!)

A GLOBAL Variable. A LOCAL Variable.


EXAMPLE PROGRAMS

def Greeting():
name = ‘Mary’
name = 'Mary Poppins‘
print(name) print(name)

print(name)
GLOBAL VARIABLES
name = 'Mary Poppins‘

def Greeting():
print(name)

Greeting()
GLOBAL VS LOCAL
num = 6

def multi(): 18
Local: ?
num = 6
num = num * 3 Global: ? 6

print(‘local variable: ’,
num)

multi()
print(‘Global: ’, num)
BEST PROGRAMMING
PRACTICES

Avoid use of global variables in


favour of local variables.
OPTIONAL(DEFAULT)
PARAMETERS
def print_name(fname, lname, reverse):
if(reverse):
print(lname, ‘, ’, fname)
else:
print(fname, lname)
FUNCTION CALLING:
POSITIONAL
The most common method, which
is the only one we have used thus
far, is called positional—the first
parameter is bound to the first
actual argument, the second to
the second
FUNCTION CALLING:
KEYWORD ARGUMENTS
Python also supports keyword
arguments, in which formals are
bound to actuals using the name
of the formal parameter
BOOLEAN FUNCTIONS

▪Functions that return Boolean


values

▪Convenient for hiding complicated


tests
BOOLEAN FUNCTIONS
def is_divisible_by_2(num):
if(num % 2 == 0)
return True
else:
return False
BOOLEAN FUNCTIONS
def is_divisible_by_2(num):
if(num % 2 == 0)
return True
return False
DEBUGGING IN FUNCTIONS

▪ Preconditions

▪ Postconditions
COMMON PROBLEMS

▪ Indentation issues

▪ Parameter naming and


passing

You might also like