Python library function examples -

Python library function examples

Introduction

A Python module is a program file(.py) containing Variables, Class definition, Statements and Junctions related to a particular task. Every program that we write in Python is also called module. In addition to it, Python comes loaded with some pre-defined modules called Standard Library Modules. Each of these modules contains some built in functions called Library Functions.Here You will learn Python library function examples.

Python library function list

  • abs()
  • sqrt()
  • exp()
  • ceil()
  • floor()
  • log()
  • pow()
  • sin()
  • cos()
  • tan()
  • random()
  • randint()
  • ord()
  • chr()
  • str()
  • isalpha()
  • islower()
  • isdigit()
  • isupper()
  • capitalize()
  • find()
  • Istrip()
  • rstrip()
  • pattern()

Python import module example

In order to use functions defined inside a module we need to bring that module inside our program. This is called importing module. Python provides “import” statement to import module in a program. It can be done in three ways”

  • Import entire module
  • Import selected objects from a module
  • Importing all objects of module

To import entire module

Importing entire module means all functions defined inside module will be accessible in the program. To import an entire module we use ‘import’ command in the following format:

Syntax

import <module1>, <module2>,

import math, random

when we import entire module in our program we can use functions defined inside the module using module name only. The general format of calling a function will be, name>()

Syntax

<module name>.<function

Example

math.sqrt(25)

To import selected object of module

Rather than importing an entire module, we can import only a selected list of functions also. To

do so we use from-import command in the following format

Syntax

from <module_name> import <object_list>

from math import sqrt, pi, pow

Example Now since we have imported selected objects from the module we don’t need to use module name with object name.

Valid Example

print(pi)

print(sqrt(25))

Invalid Example:

print(math.pi)

print(math.sqrt(25))-

To import all object of module

We can use ‘from-import’ command to import all object from a module. It is very much like importing entire module but with a difference that here we don’t have to use module name with function name.

Syntax

from <module_name>import *

Example

from math import*

Now since we have imported all objects from the module we don’t need to prefix module name with object name.

Valid Example

print(pi)

print(sqrt(25))

Python library function examples PDF Notes Whatsapp on 9235740454

Python Math module function

Python’s standard library provides a module namely “math” for math related functions that work with all numeric values except for complex number. In order to work with functions of “math” module, we need to first import it to our program. There are the following math library functions. The math python math function list is given below

1) abs()

This function is used to calculate absolute value of the number given. In other words, it converts negative numbers into positive. It can take both integer and floating pointing number as argument.

Syntax

math.abs(<any number>)

print(math.abs(-12.8))

2) sqrt()

This function is used to calculate square root of the number given. It takes non-negative integer and floating pointing number as argument.

Syntax

math.sqrt(<any number>)

Example

print(math.sqrt(25))

dictionary functions in python with examples (2024)

3) exp()

This function is used to calculate exponent value (e) of the number given. It takes both integer

and floating pointing number as argument. (e=2.718281)

Syntax

math.exp(<any number>)

Example

print(math.exp(3))

20.08554

4) ceil()

This function is used to round off the given number to the higher integer. It takes both negative and positive floating pointing number as argument.

Syntax

Example

math.ceil(<any number>)

print(math.ceil(3.2))

4

print(math.ceil(3.9))

4

print(math.ceil(-3.2))

-3

5) floor()

This function is used to round off the given number to the lower integer. It takes both negative and positive floating pointing number as argument.

Syntax

math.floor(<any number>)

Example

print(math.floor(3.2))

3

print(math.floor(3.9))

3

print(math.floor(-3.2))

Dictionary operations in python with examples

6) log()

This function is used to calculate natural logarithm of the given number. It takes both integer and floating pointing number as argument.

Syntax

math.log(<any number>)

Example

print(math.log(2.34))

0.85015092936961

7) log10()

This function is used to calculate logarithm to the base of 10, of the given number. It takes both Integer and floating pointing number as argument.

Syntax

math.log10(<any number>)

Example

print(math.log10(2.34))

0.3692158574101428

8) pow()

This function is used to calculate one number(x) raised to the power of another (y) number (X”). It takes both integer and floating pointing number as argument.

Syntax

math.pow(<base>, <power>)

Example

print(math.pow(3,6))

9) sin()

This is trigonometric function and used to calculate sine of the given angle. The angle must be specified in radians. To convert and angle from degree to radian we multiple that angle by “3.14/180”.

Syntax

math.sin(<any angle in radians>)

Example

print(math.sin(45 (3.14/180)))

0.7068251811053661

10) cos()

This is trigonometric function and used to calculate cosine of the given angle. The angle must be specified in radians. To convert and angle from degree to radian we multiple that angle by “3.14/180”

Syntax

Example

math.cos(<any angle in radians>)

print(math.cos(60″ (3.14/180)))

0.5004596890082056

11) tan()

This is trigonometric function and used to calculate tangent of the given angle. The angle must be specified in radians. To convert and angle from degree to radians we multiple that angle by “3.14/180”

Syntax

math.tan(<any angle in radians>)

print(math.tan(45″ (3.14/180)))) 0.9992039901050429

Example

12) degrees()

This function is used to convert angle from radians to degree.

Syntax

math.degrees(<any angle in radians>)

Example

print(math.degrees(1.0472))

13) radians()

This function is used to convert angle from degrees to radians.

Syntax

math.radians(<any angle in degrees

Example

print(math.radians(60))

1.04719

14) trunc()

This function is used to truncate the given floating point number. Truncation means this function will remove the fractional part from the given floating point number and print only the integer part.

math.trunc(<any floating point number>)

print(math.trunc(6,97))

Python math function example

Write a Program to input the value of x and calculate the result of the following equation

ex+cosx0+root(x)

import math

x=float(input(“Enter the value for x: “))

a=math.exp(x)

b=math.cos(x*(3.14/180))

c=math.sqrt(x)

d=a+b+c

print(“Result = “, d)

Write a program to print three sides of a triangle. Calculate and print its area using Heron’s Formula.

import math

x=float(input(“Enter side 1: “))

y=float(input(“Enter side 2: “))

z=float(input(“Enter side 3:”))

s=(x+y+2)/2

area=math.sqrt(s*(s-x)*(s-b)*(s-z))

print(“Result=”, area)

Thanks For Reading the blog Python library function examples. I hope you will understand the topic well.

For Full Python Class Subscribe Our Youtube Channel by click on the link below-

https://www.youtube.com/@olevelguruji

 

Leave a Comment