Modules in Python
A module allows you to logically organize your Python code and can define functions, classes and variables. A module can also include a runnable code.
Math Module
- Python math module is used to perform mathematical operations in python.
- The math module is a standard module in Python and is always available.
- We can use all functionality of math module by importing it
Let's see it by an example
# Square root calculation import math print(math.sqrt(256)) print(math.sqrt(111)) |
---|
Output
16.0
10.535653752852738
Note: Math module has number of function/methods, here we used sqrt (for square root)
Python Random Module
Random module is used to generate random numbers in python.
import random random.seed(2) print(random.random()) print(random.random()) |
---|
Output
0.9560342718892494
0.9478274870593494
Note: Random module also have number of function/methods , here i mentioned some of them
Function/Methods | Description |
---|
seed() | Initialize the random number generator |
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 |
Input-Output Printing on screen, Reading data from keyboard
#Reading data from keyboard or input a= input("Enter your name : ") #Printing input data print("Printing input on screen=", a) |
---|
Output
Enter your name : Goeduhub Technologies
Printing input on screen= Goeduhub Technologies
Python Packages
A package is basically a directory with Python files and a file with the name __init__.py.
Example: Pandas, Numpy, Sklearn , Pygame all are python packages and libraries to perform specific task.
Let's see a simple example
Problem: We have a list and we want to convert it in a Series
Solution: We can do this problem in core python but we have to write a long code, let's see how pandas do this
#To use pandas first we have to import pandas import pandas as pd #Here pd just a object of pandas #Defining a List A=['a','b','c','d','e'] #Converting into a series df=pd.Series(A) print(df) |
---|
Output
Note: As you can see from the output our list is converted into a series with index.
File Handling in Python click here for Article
WBUT B.Tech (CSE-III Sem) IT Workshop (Sci Lab/MATLAB/Python/R) Lab for more question and answers Click Here
Python Tutorial
Machine Learning Tutorial
AI Tutorial