Python Session 6-7 Functions
Python Session 6-7 Functions
Module 1
• Topic 1: Why should you learn to write programs
• Topic 2: Variables, expressions and statements
• Topic 3: Conditional execution
• Topic 4: Functions
VIDYA VIKAS INSTITUTE
Department of Electronics & Communication Engineering OF ENGINEERING &
TECHNOLOGY
Topic 4: Functions
4.1 Function calls
4.2 Built-in functions
4.3 Type conversion functions
4.4 Math functions
4.5 Random numbers
4.6 Adding new functions
4.7 Definitions and uses
4.8 Flow of execution
4.9 Parameters and arguments
4.10 Fruitful functions and void functions
4.11 Why functions?
4.12 Debugging
4.13 Exercises
VIDYA VIKAS INSTITUTE
Department of Electronics & Communication Engineering OF ENGINEERING &
TECHNOLOGY
Another very common built-in function is the len function which tells us how many
items are in its argument.
If the argument to len is a string, it returns the number of characters in the string.
>>> len('Hello world')
11
These functions are not limited to looking at strings.
They can operate on any set of values.
You should treat the names of built-in functions as reserved words (i.e., avoid
using “max” as a variable name).
VIDYA VIKAS INSTITUTE
Department of Electronics & Communication Engineering OF ENGINEERING &
TECHNOLOGY
The empty parentheses after the name indicate that this function doesn’t take any
arguments.
Later we will build functions that take arguments as their inputs.
The first line of the function definition is called the header; the rest is called the
body. The header has to end with a colon and the body has to be indented.
By convention, the indentation is always four spaces.
The body can contain any number of statements.
>>> print(type(print_lyrics))
<class 'function'>
The value of print_lyrics is a function object, which has type “function”.
VIDYA VIKAS INSTITUTE
Department of Electronics & Communication Engineering OF ENGINEERING &
TECHNOLOGY
Exercise 3: Move the function call back to the bottom and move the definition
of print_lyrics after the definition of repeat_lyrics. What happens when you run
this program?
VIDYA VIKAS INSTITUTE
Department of Electronics & Communication Engineering OF ENGINEERING &
TECHNOLOGY
4.12 Debugging
• If you are using a text editor to write your scripts, you might run into problems with spaces and
tabs. The best way to avoid these problems is to use spaces exclusively (no tabs).
• Most text editors that know about Python do this by default, but some don’t. Tabs and spaces
are usually invisible, which makes them hard to debug, so try to find an editor that manages
indentation for you.
• Also, don’t forget to save your program before you run it. Some development environments do
this automatically, but some don’t. In that case, the program you are looking at in the text
editor is not the same as the program you are running.
• Debugging can take a long time if you keep running the same incorrect program over and over!
• Make sure that the code you are looking at is the code you are running. If you’re not sure, put
something like print("hello") at the beginning of the program and run it again. If you don’t see
hello, you’re not running the right program!
VIDYA VIKAS INSTITUTE
Department of Electronics & Communication Engineering OF ENGINEERING &
TECHNOLOGY
Exercises
Exercise 4: What is the purpose of the “def” keyword in Python?
a) It is slang that means “the following code is really cool”
b) It indicates the start of a function
c) It indicates that the following indented section of code is to be
stored for later
d) b and c are both true
e) None of the above
VIDYA VIKAS INSTITUTE
Department of Electronics & Communication Engineering OF ENGINEERING &
TECHNOLOGY
Exercises
Exercise 5: What will the following Python program print out?
def fred():
print("Zap")
def jane():
print("ABC")
jane()
fred()
jane()
a) Zap ABC jane fred jane
b) Zap ABC Zap
c) ABC Zap jane
d) ABC Zap ABC
e) Zap Zap Zap
VIDYA VIKAS INSTITUTE
Department of Electronics & Communication Engineering OF ENGINEERING &
TECHNOLOGY
Exercises
Exercise 6: Rewrite your pay computation with time-and-a-half for overtime and create a function
called computepay which takes two parameters (hours and rate).
Enter Hours: 45
Enter Rate: 10
Pay: 475.0
hours = input('Enter Hours:')
hours = int(hours)
rate=input('Enter rate:')
rate= float(rate)
def computepay(hours,rate):
pay = hours*rate
return(pay)
pay=computepay(hours,rate)
print(pay)
VIDYA VIKAS INSTITUTE
Department of Electronics & Communication Engineering OF ENGINEERING &
TECHNOLOGY
Exercises
Exercise 7: Rewrite the grade program from the Enter score: 0.95
previous chapter using a function called
compute grade that takes a score as its A
parameter and returns a grade as a string. Run
the program repeatedly to test the various Enter score: perfect
different values for input.
Bad score
Score Grade
Enter score: 10.0
>= 0.9 A
Bad score
>= 0.8 B
Enter score: 0.75
>= 0.7 C
C
>= 0.6 D
Enter score: 0.5
< 0.6 F
F
VIDYA VIKAS INSTITUTE
Department of Electronics & Communication Engineering OF ENGINEERING &
TECHNOLOGY
Exercises
score=input('enter the score:') elif score>=0.6:
def score_grade(score): grade="E"
try: elif score<0.6:
score=float(score) grade="F"
grade=[] else:
if score>0.1 and score<1.0: grade="BAD Score"
if score>= 0.9: except:
grade="A" grade="Bad Score"
elif score>=0.8: return grade
grade="B" grade=score_grade(score)
elif score>=0.7: print(grade)
grade="C"