Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Python Functions 1

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Python Functions 1

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Functions in Python

Python provides the feature to divide a large program into different


smaller modules.

Each such module is known as a function which is aimed to


perform a single independent subtask.

A function is a group of statements that exists within a program for


the purpose of performing a specific task.

Functions implement the concept of reusability because, a function


can be written once and can be used as many times needed.

Functions provide a systematic way of problem solving by dividing


the given problem into several sub-problems, finding their
individual solutions, and integrating the solutions of individual
problems to solve the original problem.

This approach of problem solving is known as Modular Approach


or Stepwise Refinement Approach or Divide and Conquer
Approach.

In Python, functions can be categorised into three types –

(i) Built-in functions


(ii) Modules
(iii) User-defined functions

Built-in functions :-

These are the predefined functions which are already available in Python
standard library and can be used in any program.
Some commonly used built-in functions are –

(i) Type conversion functions - These are the functions used to convert one
data type to another.

int() it takes any value and converts it into an integer. In case of a


float it extracts only the integer part but causes error in case of a string.

>>> a=1.5
>>> b="11"
>>> c=int(a)
>>> c
1
>>> d=int(b)
>>> d
11
>>> c="55a"
>>> e=int(c)
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
e=int(c)
ValueError: invalid literal for int() with base 10: '55a'

float() it converts integers and strings into floats.

It works as follows –

 If the argument is a number, float() returns the same as a


float.

>>> a = 55
>>> b = float(a)
>>> b
55.0

 If the argument is an expression, that is evaluated and result


is returned as a float.

>>> a = float(5+3)
>>> a
8.0

 If the argument is a string containing leading +/- sign and a


floating point number, float() returns the float value of that
string.

>>> a = "345"
>>> b = float(a)
>>> b
345.0
 If the argument is a string in any other format, it causes
error.

>>> a = "12.34.5"
>>> b = float(a)
Traceback (most recent call last):
File "<pyshell#17>", line 1, in <module>
b = float(a)
ValueError: could not convert string to float: '12.34.5'

(ii) Input function :- It enables the user to input a string without


evaluating its value. The input() function continues to read input text from
the user until it encounters a new line.

(iii) eval() function :- It is used to evaluate the value of a given string. It


takes a string as argument, evaluates the string as a number and returns the
numeric result ( int or float as the case may be). If the given argument is not a
string or if it cannot be evaluated as a number, then it results in an error.

>>> a = eval('5+3')
>>> a
8

(iii) min() and max() function :- These are used to find out the minimum and
maximum value out of several values. These functions receive several values
as arguments.

>>> min(5, 8, 3, 11)


3
>>> min('hello', 'are', 'Zfd')
'Zfd'

(iv) abs() function :- It returns the absolute value of a number. It


receives an integer or float as argument and returns the positive
value.

>>> abs(-3)
3
>>> abs(5)
5
>>> abs(-6.1)
6.1
(v) type() function :- it takes a variable as argument and returns
the type of value to which the variable refers to.

>>> a = 15
>>> type(a)
<class 'int'>
>>> a = 'abc'
>>> type(a)
<class 'str'>
>>> a = 15.5
>>> type(a)
<class 'float'>

(vi) len() function :- It returns the length (number of items) in an


object. It takes a sequence (string, list, tuple) or a mapping
(dictionary) as argument.

>>> a=[10,20,30]
>>> len(a)
3
>>> b='abcdef'
>>> len(b)
6

(vii) round() function :- It is used to get the result up to a


specified number of digits. It rounds a given floating point number
to the specified number of digits and returns the result.

>>> round(1.234,2)
1.23
>>> round(1.678,2)
1.68

(viii) range() function :- It is used to define a series of numbers.

You might also like