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. 1 : <p>What is the output of this code?</p><pre>def calc(x):<br>&nbsp; &nbsp; &nbsp; &nbsp;r=2*x**2<br>&nbsp; &nbsp; &nbsp; &nbsp;return r<br>print(calc(5))</pre>

  1. Error
  2. 50
  3. 100
  4. 20
Qus. 2 : <p>What is the output of the following code ?</p><pre>def add(a, b):<br>&nbsp; &nbsp; &nbsp; &nbsp;return a+5, b+5<br>result = add(3,2)<br>print(result)</pre><div><br></div>

  1. 15
  2. 8
  3. (8,7)
  4. Error
Qus. 3 : Function defined to achieve some task as per the programmers requirement is called a :

  1. User Defined Function
  2. Library Functions
  3. Builtin Functions
  4. All of the above
Qus. 4 : How is a function declared in Python ?

  1. def function function_name():
  2. declare function function_name():
  3. def function_name():
  4. declare function_name():
Qus. 5 : <p>What will be the output of the following Python code?</p><pre>from math import factorial<br>print(math.factorial(5))</pre><div><br></div>

  1. 120
  2. Nothing is printed
  3. NameError: name 'math' is not defined
  4. Error, the statement should be print factorial(5))
Qus. 6 : Where is function defined?

  1. In Module
  2. In Class
  3. In Another function
  4. All of these
Qus. 7 : You can also create your own functions, these functions are called?

  1. built-in functions
  2. user-defined functions
  3. py function
  4. None of the above

Programs

Python program to convert decimal into other number systems

View Solution


python program that generates six random numbers in a sequence created with start stop step

View Solution


Python program to illustrate the global variable

View Solution


python program to create a function to calculate factorial value

View Solution


Python program to find the sum of factorial upto n terms using function

View Solution


python program to create a function for reverse the number

View Solution


Python program to check number is Palindrome or not using function

View Solution


Python program to create a function for calculating sum of digit

View Solution


Python program to create recursive function to find factorial

View Solution


python function to accept 2 number and return addition subtraction multiplication and division

View Solution


python function to accept number of days and return week and days to its equivalent

View Solution


python function to extract the day month year from given date

View Solution


Python function to find the sum of all numbers between 100 and 500 which are divisible by 2

View Solution


Python function to get two matrices and multiply them

View Solution


python recursive function to find the sum of digits

View Solution


python recursive function to print the reverse of number

View Solution


Write a function with name dividebyfive which generate and prints a random integer number from the range 0 to 100 and then return True if the randomly generated number is divisible by 5, and False otherwise.

View Solution


CCC Online Test Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Online Exam Quiz O Level NIELIT Study material and Quiz Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Best Website and Software Company in Allahabad Website development Company in Allahabad