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
ii. Built in
iii. User Defined
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:
It is simplest and most common way to use modules in our code. Its syntax is:
import modulename1 [,modulename2, ---------]
>>> 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.
>>> 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.
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…..]
>>> 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()
Write a python script that traverses through an input string and prints its characters in different lines – two characters per line.
WAP to print the number of occurrences of a substring into a line.
WAP to check the given string is palindrome or not.
Python program to convert decimal into other number systems
Write a Python program to show the application of Global variable.
Write a Python program to create a function for factorial value
Write program to find the sum of factorial upto n terms
Write a python program to create a function which reverse the given number
Write a Python program to input any number and check number is Palindrome or Not using Function
Write a Python program to input any number from user and find its sum of digit
Write a Python function to calculate the factorial value of given number using recursion.