_Python_
_Python_
O Level / A Level
Chapter - 6 : Functions
Top-down approach
A top down approach also helps one to clarify the overall structure and operation of the
program before one code the low level functions.
The top down method starts with a general description and works towards specifics.
A good way to design a program is to define exactly what the program is going to do at
the top level.
Each entry in the list should contain only one functional unit.
A functional unit can be thought of as a black box that performs a single task. Modular
programming
Modular programming is a style that adds structure and readability to the program code.
It may not make much difference on small projects, but as one starts to work on
something bigger it can make the code much easier to read and maintain.
Structuring the code is a simple task of splitting the program into manageable part so that
each part is self contained.
By creating these self contained modules, one can focus on programming each part.
Functions
A function is a named, independent section of Python code that performs a specific task
and optionally returns a value to the calling program.
A function is named. Each function has a unique name.
By using the name in another part of the program, one can execute the statements
contained in the function. This is known as calling the function.
A function can be called from within any other function.
A function is independent.
A function can perform its task without interference from or interfering with other parts
of the program.
Defining a Function
Function blocks begin with the keyword def followed by the function name and parentheses
( ).
Any input parameters or arguments should be placed within these parentheses. We can also
define parameters inside these parentheses.
The first statement of a function can be an optional statement - the documentation string of
the function or docstring.
The code block within every function starts with a colon (:) and is indented.
The statement return [expression] exits a function, optionally passing back an expression to
the caller. A return statement with no arguments is the same as return None.
Syntax
def function_name( parameters ) :
"function_docstring"
function_local variable
function statements
return [expression]
Example
# Function definition is here
def printme( str ):
"This prints a passed string into this function" # docstring
print (str)
return
Output
This is first call to the user defined function!
Again second call to the same function
Built-in Python Functions
1. input() Function
The input() function allows user input.
input(prompt)
object(s) Any object, and as many as you like. Will be converted to string before
printed
sep='separator' Optional. Specify how to separate the objects, if there is more than one.
Default is ' '
end='end' Optional. Specify what to print at the end. Default is '\n' (line feed)
file Optional. An object with a write method. Default is sys.stdout
flush Optional. A Boolean, specifying if the output is flushed (True) or buffered
(False). Default is False
3. int() Function
The int() function converts the specified value into an integer number.
x = int("12")
print(x+10)
3. float() Function
The float() function converts the specified value into a floating point number..
Syntax : float(value)
x = float(3)
print(x)
x = float("3.500")
print(x)
4. list() Function :
The list() function creates a list object.
A list object is a collection which is ordered and changeable.
5. dict() Function :
The dict() function creates a dictionary.
A dictionary is a collection which is unordered, changeable and indexed.
6. set() Function
The set() function creates a set object.
The items in a set list are unordered, so it will appear in random order.
7. str() Function
The str() function converts the specified value into a string.
8. tuple() Function
The tuple() function creates a tuple object.
We cannot change or remove items in a tuple.
Note – Function list(), dict(), set(), str() & tuple() already covered in Chapter -5 .
9. type() Function
x = type(a)
y = type(b)
z = type(c)
mylist = "Hello"
x = len(mylist)
print(x)
11. format() Function
The format() function formats a specified value into a specified format
format(value, format)
Parameter Description
value A value of any format
format The format you want to format the value into.
Legal values:
'<' - Left aligns the result (within the available space)
'>' - Right aligns the result (within the available space)
'^' - Center aligns the result (within the available space)
'=' - Places the sign to the left most position
'+' - Use a plus sign to indicate if the result is positive or negative
'-' - Use a minus sign for negative values only
' ' - Use a leading space for positive numbers
',' - Use a comma as a thousand separator
'_' - Use a underscore as a thousand separator
'b' - Binary format
'c' - Converts the value into the corresponding unicode character
'd' - Decimal format
'e' - Scientific format, with a lower case e
'E' - Scientific format, with an upper case E
'f' - Fix point number format
'F' - Fix point number format, upper case
'g' - General format
'G' - General format (using a upper case E for scientific notations)
'o' - Octal format
'x' - Hex format, lower case
'X' - Hex format, upper case
'n' - Number format
'%' - Percentage format