In this article, you will get comprehensive notes on Python library Functions. Read the article and learn!
Topics Covered
Introduction Python library Functions
A library is a place where lots of books, magazines, CDs, newspapers are found to read. These all books are divided into several chapters, magazines are divided into several articles. One book is divided into many chapters to manage and to attract readers for reading.
A similar concept is applied to programming. A large program is divided into modules. A module is a set of small coding instructions written in a programming language. These modules create a library.
Definition Python Library Functions
A library is a collection of modules or functions in a python that allows doing specific tasks to fulfill user’s needs. This library serves little bit similar functions as header files used in C or C++.
Commonly Used libraries
Some commonly used python libraries are as following:
- Standard Library: It provides some common standard IO operation related functions such as print(), input(), int() along with some math modules, random modules, statistic modules, etc.
- NumPy Library: This library is used to handle arrays to cater basic mathematical operations. Ex.: square(), absoulte(), log(), sqrt() etc.
- SciPY Library: This offers algorithmic and mathematical functions for scientific calculations. Ex.: io(), linalg(), interopolate() etc
- Matplotlib Library: This library offers functions related to graphs and charts in python. Ex.: plot(), label(), show() etc
- Tkinter Library: This library provides functions to design GUI based interface for different applications. Ex.: tkMessageBox(), tkColorChooser(), tkSimpleDialog() etc.
In next section we will discuss about python module which are the most important part of Python library Functions Class 12.
Python Module
A python module is a .py that contains statements, classes, objects, functions, and variables. That allows reusing them anytime by importing the module. The structure of the python module plays an important role in python library functions class 12.
Structure of Python module
A python module is a python file with comments (docstrings), variables and constants, classes, objects, statements, functions, etc.
docstrings
This is a multi-line text used for documentation in a module. This part consists of simple comments about authors, programs, or some statements.
variables and constants
These are simple identifiers names or labels used to store future values in modules.
classes
Classes are a collection of similar objects. It creates a template to creates objects.
objects
Objects are instances of classes. Objects and classes have the same relations as datatype and variable.
Statements
Statements are simple instructions or expressions written in the module.
The next topic very important for python library functions class 12.
Functions
Set of instructions to fulfill a specific task. Importing python module:
import python module
To import python modules in a program import statement can be used in two ways:
import <command> : import math, import NumPy etc
from <module> import <object>: from math import pi, sqrt
Note: User can import multiple objects from <module> import <object> with comma-separated objects or * to import all objects.
There are various Python library Functions to use in python coding but we will cover mathematical functions and string functions as they are given in your syllabus for the Python library Functions Class 12.
Using Mathematical Functions – python library functions
To use mathematical function import math object by using import:
import math
The following functions are the most commonly used functions provided by the math module:
ceil()
Return the smallest integer greater than or equal to the number specified.
Ex.: >>> math.ceil(12345.6786)
Output:12346
fabs()
Return the absolute value of the given number. Convert and display a negative number into a positive number.
Ex.: >>> math.fabs(-15)
Output:15.0
factorial()
Return the factorial of the given number. If the number is integral or negative then it returns ValueError.
Ex.: >>> math.faactorial(5)
Output:120
floor()
Return the largest integer less than or equal to the number specified.
Ex.: >>> math.floor(12345.6786)
Output:1234
fmod()
Return the remainder working with float values only. For integer use % operator.
Ex.: >>> math.fmod(15.8899,4)
Output:3.889900000000001
fsum()
Return addition of values in iterable. Sum() can be also used for the same along with integer numbers.
Ex.: >>> math.fsum([1.5,2.5,3.5])
Output:7.5
gcd()
Return common divisor of the specified numbers x and y.
Ex.: >>> math.gcd(42,28)
Output:14
theremainder()
Return remainder of the integer with number x divided by y.
Ex.: >>> math.remainder(12,5)
Output:2.0
pow()
Return power of x raised by y.
Ex.: >>> math.pow(5,3)
Output:125.0
The function pow() can accept three arguments also. When it accepts three arguments it will be as follows:
>>> p=pow(5,2,3)
Output:1
When it accepts three variables it will be evaluated as (x**y)%z. So here 5^2 is 25%3 is 1.
sqrt()
Return the square root of a given number.
Ex.: >>> math.sqrt(100)
Output:10.0
These are the most useful functions from from python library functions.
String Functions – python library functions
To use string functions a variable needs to assign with a specific string value.
For Ex.: str =”fun with python”
len()
Len function will return no. of characters present in the specified text. The len() function is a python standard library function.
Ex.: >>> len(str)
Output:15
capitalize()
Convert first letter of the text into capital.
Ex.: >>> str.capitalize()
Output:’Fun with python’
center()
It aligns the string to center by filling padding the character left and right of the string.
Ex.: >>> str2=str.center(20,’*’)
Output:’**fun with python***’
count()
Return no. occurrences of a specified string.
Ex.: >>> str.count(‘with’,4,10)
Output:1
endswith()
Return True if the specified text ends with the given string, otherwise False. stratswith() method is the opposite of endswith.
Ex.: >>> str.endswith(‘on’,0,20)
Output: True
index()
It returns the lowest index of specified substring. It returns ValueError if the substring is not found in the string.
Ex.: >>> str.index(‘with’)
Output:4
str.index(‘t’,8)
Output:11
str.index(‘t’,8,10)
Traceback (most recent call last):
File “”, line 1, in
str.index(‘t’,8,10)
ValueError: substring not found
find()
Return the lowest index in the string where substring sub is found, such that sub is contained in the range. Returns -1 if a substring is not found in the text.
Ex.: >>> str.find(‘with’,4,10)
Output:4
isalpha()
It returns True if the string contains only alphabets, otherwise false.
Ex.: str.isalpha()
False
“Python”.isalpha()
True
isalnum()
Return True if all characters in the string are alphanumeric otherwise False. It requires at least 1 character.
Ex.: >>> str=’funwithpython2020′
>>> str.isalnum()
Output: True Similarly isdigit(), islower(), isspace(), istitle, isupper() functions return True respectively, otherwise false.
join()
Concat string specified as in sequence.
Ex.: >>>str=’fun with python 2020′
>>> str.join([‘hello! ‘,’ is amazing’)
Output: ‘hello! fun with python 2020 is amazing’
upper()
Convert all text into capital letters. Ex.: >>> str.upper()
Output:’FUN WITH PYTHON 2020′
title()
Convert first letter of each word into capitals. Ex.: >>> str.title()
Output:’Fun With Python 2020′
swapcase()
Convert capital letters into small letters and small letters to capital letters.Ex.: >>> str.swapcase()
Output:’FUN WITH PYTHON 2020′
split()
Split the the words by given characters. If number is specified as maxsplit then it returns n+1 string as output.Ex.: >>> str.split(‘ ‘)
Output:[‘fun’,’with’,’python’,’2020′]
partition()
It splits() the string at the first occurrence of the specified part and returns a tuple with the combination of three parts before the specified part, the specified part itself and after the specified part.
Ex. str.partition(‘with’)
Output:(‘fun ‘, ‘with’, ‘ python’)
ljust()
Return the string in left-justified in a string of up to the length of the string. The string is filled up with a specified character. rjust() is just opposite of ljust().Ex.: >>> str.ljust(25,’*’)
Output:’fun with python 2020******’
lower()
Convert the specified text into lowercase. Ex.: >>> str.lower()
Output:’fun with python 2020
lstrip()
Removes the specified characters from the string from leading text. rstrip() is just opposite of lstrip().Ex.: >>> str.lstrip(‘ufp’)
Output:’n with python 2020′
strip() removes both leading and trailing spaces from the text.
replace()
Replace the new text with old text.Ex.: >>> str.replace(‘fun’,’play’)
Output:’play with python 2020′
Follow this link to to get notes about string manipulations basics:
Follow this link for Questions based on the topic:
Learn them and do more and more practice with them.
So here we have almost covered mostly useful functions for Python library Functions Class 12.
If you have any suggestions/feedbacks etc, feel free to contact us for the same.
Click here to read more about functions.
nice
Good information 👍
remainder() function doesn't work in Python 3.8
Thanks
Thanks
Yes sir