in

Python Built-in Functions, Libraries, and modules – Day 10

In this article you will learn Python Built-in functions, libraries and modules.

Python has many inbuilt modules and packages. Today we sill discuss them all.

Want to learn python from scratch – Day 1 in python begineer to advance

Day

New here?

Start learning Python from scratch for Free.

1.Random Library function in python

Random library in python is very useful. This module helps in generating random numbers.

The code given below generates a random number between x and y-1 (both inclusive) using the randrange function of the random module.

import random

x=10

y=50

print(random.randrange(x,y)) 

Output will be different in each run.

Methods in random module.

MethodDescription
seed()Initialize the random number generator
getstate()Returns the current internal state of the random number generator
setstate()Restores the internal state of the random number generator
getrandbits()Returns a number representing the random bits
randrange()Returns a random number between the given range
randint()Returns a random number between the given range
choice()Returns a random element from the given sequence
choices()Returns a list with a random selection from the given sequence
shuffle()Takes a sequence and returns the sequence in a random order
sample()Returns a given sample of a sequence
random()Returns a random float number between 0 and 1
uniform()Returns a random float number between two given parameters
triangular()Returns a random float number between two given parameters, you can also set a mode parameter to specify the midpoint between the two other parameters
betavariate()Returns a random float number between 0 and 1 based on the Beta distribution (used in statistics)
expovariate()Returns a random float number between 0 and 1, or between 0 and -1 if the parameter is negative, based on the Exponential distribution (used in statistics)
gammavariate()Returns a random float number between 0 and 1 based on the Gamma distribution (used in statistics)
gauss()Returns a random float number between 0 and 1 based on the Gaussian distribution (used in probability theories)
lognormvariate()Returns a random float number between 0 and 1 based on a log-normal distribution (used in probability theories)
normalvariate()Returns a random float number between 0 and 1 based on the normal distribution (used in probability theories)
vonmisesvariate()Returns a random float number between 0 and 1 based on the von Mises distribution (used in directional statistics)
paretovariate()Returns a random float number between 0 and 1 based on the Pareto distribution (used in probability theories)
weibullvariate()Returns a random float number between 0 and 1 based on the Weibull distribution (used in statistics)Please do not confuse we will talk about these all later.

Python math module

math is another useful module in Python. Once you have imported the math module, you can use some of the below functions:

FunctionExplanation
math.ceil(x)Smallest integer greater than or equal to x
math.floor(x)Largest integer smaller than or equal to x
math.factorial(x)Factorial of x
math.fabs(x)Gives absolute value of x

Try the code:

import math
num1=234.01
num2=6
num3=-27.01
print("The smallest integer greater than or equal to num1,",num1,":",math.ceil(num1))
print("The largest integer smaller than or equal to num1,",num1,":",math.floor(num1))
print("The factorial of num2,",num2,":", math.factorial(num2))
print("The absolute value of num3",num3,":",math.fabs(num3))

String Functions in python

String data type in Python has many inbuilt functions which make it easier to work with strings.

Consider the string, name="Raghav".
FunctionOutputExplanation
name.count(“a”)2Returns the count of a given set of characters. Returns 0 if not found
name.replace(“a”,”A”)RAghAvReturns a new string by replacing a set of characters with another set of characters. It does not modify the original string
name.find(“a”)1Returns the first index position of a given set of characters
name.startswith(“Ra”)TrueChecks if a string starts with a specific set of characters, returns true or false accordingly.
name.endswith(“ha”)FalseChecks if a string ends with a specific set of characters, returns true or false accordingly.
name.isdigit()FalseChecks if all the characters in the string are numbers, returns true or false accordingly.
name.upper()RAGHAVConverts the lowercase letters in string to uppercase
name.lower()raghavConverts the uppercase letters in string to lowercase
name.split(“a”)[‘R’, ‘gh’, ‘v’]Splits string according to delimiter and returns the list of substring. Space is considered as the default delimiter.

List Functions in Python

List data type in Python also have many inbuilt functions.

FunctionOutputExplanation
num_list.append(60)[10,20,30,40,50,60]Adds an element to end of list
num_list.index(10)0Returns the index position of the element.In case of multiple occurrence of the element, returns the index of the first occurrence.Throws ValueError, if the element is not found
num_list.insert(3,60)[10,20,30,60,40,50]Inserts an element at a given position
num_list.pop(3)40Removes and returns the element at given index position from the list
num_list.remove(30)[10,20,40,50]Removes the first occurring element whose value is 30
num_list.sort()[10,20,30,40,50]Sorts the list in ascending order
num_list.reverse()[50,40,30,20,10]Reverses the list

Dictionary function in python

Dictionary in Python also have many inbuilt functions.

Consider a dictionary:

crew_details={
              "Pilot":"Kumar",
              "Co-pilot":"Raghav",
              "Head-Strewardess":"Malini",
              "Stewardess":"Mala"
              }
FunctionOutputExplanation
crew_details.get(“Pilot”)KumarReturns the value for given key. If the given key is not found, returns None
crew_details.update({“Flight Attendant”:”Jane”, “Co-pilot”:”Henry”})No output, dictionary will be updatedUpdates the dictionary with the given key-value pairs. If a key-value pair is already existing, it will be overwritten, otherwise it will be added to the dictionary

Python Date and Time module

Python has inbuilt modules called time and datetime. They are very helpful in finding details of time.

FunctionExplanation
time.gmtime()Returns the current GM time
time.localtime()Returns the current time based on the current locality
time.strftime(t,format)Converts t to a string as specified by the format argument and returns it.Where t is the time returned by time.gmtime() or time.localtime(). It is optional, default value is current time as returned by time.localtime()
datetime.datetime.strptime (date_string, format)Converts a date string in the specified format to a datetime value and returns it
datetime.date.today()Returns the current local date

we hope you liked this article. In this article, we discussed the various functions and libraries in python. If you have any queries or confusion feel free to contact us anytime.

New here?

comment below with your query, we usually replies within 12 hours.

Start learning Python from scratch for Free.

End of python Built-in function article.

some examples

python built-in functions, python modules and python libraries

What do you think?

Written by admin

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

GIPHY App Key not set. Please check settings

Loading…

0

What is Digital Marketing? Its History, Benefits, Types, future 2020? full guide

List of Article Submission Sites to get Do-Follow backlinks