Modular programming and functions

Modular programming and functions


A function is a named sequence of statement(s) that performs a computation. It contains line of code(s) that are executed sequentially from top to bottom by Python interpreter. They are the most important building blocks for any software in Python.

Functions can be categorized as belonging to

i. Modules

ii. Built in

iii. User Defined


Module

A module is a file containing Python definitions (i.e. functions) and statements. Standard library of Python is extended as module(s) to a programmer. Definitions from the module can be used within the code of a program. To use these modules in the program, a programmer needs to import the module. Once you import a module, you can reference (use), any of its functions or variables in your code. There are many ways to import a module in your program, the one‟s which you should know are:

i. import

ii. from

Import

It is simplest and most common way to use modules in our code. Its syntax is:

import modulename1 [,modulename2, ---------]

Example

>>> import math

(i) search for the file „math.py‟.

(ii) Create space where modules definition & variable will be created,

(iii) then execute the statements in the module.


Now the definitions of the module will become part of the code in which the module was imported.

To use/ access/invoke a function, you will specify the module name and name of the function- separated by dot (.). This format is also known as dot notation.

Example

>>> value= math.sqrt (25) # dot notation

The example uses sqrt( ) function of module math to calculate square root of the value provided in parenthesis, and returns the result which is inserted in the value. The expression (variable) written in parenthesis is known as argument (actual argument). It is common to say that the function takes arguments and return the result.

This statement invokes the sqrt ( ) function. We have already seen many function invoke statement(s), such as

>>> type ( )

>>> int ( ), etc.

From Statement

It is used to get a specific function in the code instead of the complete module file. If we know beforehand which function(s), we will be needing, then we may use from. For modules having large no. of functions, it is recommended to use from instead of import.

Its syntax is

>>> from modulename import functionname [, functionname…..]

Example

>>> from math import sqrt

value = sqrt (25)

Here, we are importing sqrt function only, instead of the complete math module. Now sqrt( ) function will be directly referenced to. These two statements are equivalent to previous example.

from modulename import *

will import everything from the file.

Qus: What is a function? How is it useful?

A function is a named block in a program which has some certain working. Whenever you call this it performs all its working. It reduces the complexity and reduces the labour of writing same code again and again.

Qus: What is an argument? Give an example of one.

Ans : An argument is the requirement of a function to perform its working. For ex- pow(x,y), here x and y are the arguments which are required to pass to pow() for its working.

Qus. What is a string slice? How is it useful?

Ans: String Slice is a part of a string containing some contiguous characters from the string. It is accessed from the string by providing a range in ―[ ]‖ brackets i.e. S [n:m]. Python returns all the characters at indices n, n+1, n+2 . . . m-1 e.g. 'Barabanki'.[4:7] will return 'ban'.

Qus. Which functions would you chose to use to remove leading and trailing white spaces from a given string?

Ans: Python String strip() function will remove leading and trailing white spaces. If you want to remove only leading or trailing spaces, use lstrip() or rstrip() function instead.

Qus. Suggest appropriate functions for the following tasks –

(a) To check whether the string contains digits.

(b) To find the occurrence a string within another string.

(c) To convert the first letter of a string to upper case.

(d) To convert all the letters of a string to upper case.

(f) To check whether all the letters of the string are in capital letters.

(g) to remove all the white spaces from the beginning of a string.

Ans.  (a) isalnum() (b) find() (c) capitalize()   (d) upper() (f) isupper() (g) lstrip()


Programs

Write a python script that traverses through an input string and prints its characters in different lines – two characters per line.


View Solution


WAP to print the number of occurrences of a substring into a line.


View Solution


WAP to check the given string is palindrome or not.


View Solution