functions using libraries – python cs class 12
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 is divided into modules. A module is a set of small coding instructions written in a programming language. These modules create a library.
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.
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.
A python module is a .py that contains statements, classes, objects, functions, and variables. That allows resuing them anytime by importing the module.
Structure of Python module:
A python module is a python file with comments (docstrings), variables and constants, classes, objects, statements, functions, etc.
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 are a collection of similar objects. It creates a template to creates objects.
Objects are instances of classes. Objects and classes have the same relations as datatype and variable.
Statements are simple instructions or expressions written in the module.
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.
Using Mathematical Functions
To use mathematical function import math object by using import:
The following functions are the most commonly used functions provided by the math module:
Return the smallest integer greater than or equal to the number specified.
Ex.: >>> math.ceil(12345.6786)
Return the absolute value of the given number. Convert and display a negative number into a positive number.
Ex.: >>> math.fabs(-15)
Return the factorial of the given number. If the number is integral or negative then it returns ValueError.
Ex.: >>> math.faactorial(5)
Return the largest integer less than or equal to the number specified.
Ex.: >>> math.floor(12345.6786)
Return the remainder working with float values only. For integer use % operator.
Ex.: >>> math.fmod(15.8899,4)
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])
Return common divisor of the specified numbers x and y.
Ex.: >>> math.gcd(42,28)
Return remainder of the integer with number x divided by y.
Ex.: >>> math.remainder(12,5)
Return power of x raised by y.
Ex.: >>> math.pow(5,3)
The function pow() can accept three arguments also. When it accepts three arguments it will be as follows:
When it accepts three variables it will be evaluated as (x**y)%z. So here 5^2 is 25%3 is 1.
Return the square root of a given number.
Ex.: >>> math.sqrt(100)
To use string functions a variable needs to assign with a specific string value.
For Ex.: str =”fun with python”
Convert first letter of the text into capital.Ex.: >>> str.capitalize()
Output:’Fun with python’
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***’
Return no. occurrences of a specified string.
Ex.: >>> str.count(‘with’,4,10)
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)
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)
Return True if all characters in the string are alphanumeric otherwise False. It requires at least 1 character.
Ex.: >>> str=’funwithpython2020′
Output: True Similarly isalpha(), isdigit(), islower(), isspace(), istitle, isupper() functions return True respectively, otherwise false.
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’
Convert all text into capital letters. Ex.: >>> str.upper()
Output:’FUN WITH PYTHON 2020′
Convert first letter of each word into capitals. Ex.: >>> str.title()
Output:’Fun With Python 2020′
Convert capital letters into small letters and small letters to capital letters.Ex.: >>> str.swapcase()
Output:’FUN WITH PYTHON 2020′
Split the the words by given characters. If number is specified as maxsplit then it returns n+1 string as output.Ex.: >>> str.split(‘ ‘)
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******’
Convert the specified text into lowercase. Ex.: >>> str.lower()
Output:’fun with python 2020
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′
Replace the new text with old text.Ex.: >>> str.replace(‘fun’,’play’)
Output:’play with python 2020′