Skip to main content

ads1

Python Module

 Python Modules

            



A python module can be defined as a python program file which contains a python code including python functions, class, or variables. In other words, we can say that our python code file saved with the extension (.py) is treated as the module. We may have a runnable code inside the python module.

Modules in Python provides us the flexibility to organize the code in a logical way.

To use the functionality of one module into another, we must have to import the specific module.

Example

In this example, we will create a module named as file.py which contains a function func that contains a code to print some message on the console.

  • Python Math Module

Python math module is defined as the most famous mathematical functions, which includes trigonometric functions, representation functions, logarithmic functions, etc. Furthermore, it also defines two mathematical constants, i.e., Pie and Euler number, etc.

  • Pie (n): It is a well-known mathematical constant and defined as the ratio of circumstance to the diameter of a circle. Its value is 3.141592653589793.
  • math.exp()

This method returns a floating-point number after raising e to the given number.

Example

import math    

number = 5e-2  # small value of of x    

print('The given number (x) is :', number)    

print('e^x (using exp() function) is :', math.exp(number)-1)   


  • math.pow(x,y)

This method returns the power of the x corresponding to the value of y. If value of x is negative or y is not integer value than it raises a ValueError.

Example

import math  

number = math.pow(10,2)  

print("The power of number:",number)  

  • math.floor(x)

This method returns the floor value of the x. It returns the less than or equal value to x.

Example:

import math  

number = math.floor(10.25201)  

print("The floor value is:",number)  

  • math.ceil(x)

This method returns the ceil value of the x. It returns the greater than or equal value to x.

import math  

number = math.ceil(10.25201)  

print("The floor value is:",number)  

  • math.fabs(x)

This method returns the absolute value of x.

import math  

number = math.fabs(10.001)  

print("The floor absolute is:",number) 

math.factorial()

This method returns the factorial of the given number x. If x is not integral, it raises a ValueError.

Example

import math  

number = math.factorial(7)  

print("The factorial of number:",number)  

  • Python statistics module

Python statistics module provides the functions to mathematical statistics of numeric data. There are some popular statistical functions defined in this module.

  • mean() function

The mean() function is used to calculate the arithmetic mean of the numbers in the list.

Example

import statistics    

# list of positive integer numbers   

datasets = [5, 2, 7, 4, 2, 6, 8]     

x = statistics.mean(datasets)     

# Printing the mean   

print("Mean is :", x)  

  • median() function

The median() function is used to return the middle value of the numeric data in the list.

Example

import statistics     

datasets = [4, -5, 6, 6, 9, 4, 5, -2]      

# Printing median of the   

# random data-set   

print("Median of data-set is : % s "  

        % (statistics.median(datasets)))


  • mode() function

The mode() function returns the most common data that occurs in the list.

Example

import statistics     

# declaring a simple data-set consisting of real valued positive integers.   

dataset =[2, 4, 7, 7, 2, 2, 3, 6, 6, 8]     

# Printing out the mode of given data-set   

print("Calculated Mode % s" % (statistics.mode(dataset)))  

  • Python Random module

The Python random module functions depend on a pseudo-random number generator function random(), which generates the float number between 0.0 and 1.0.

There are different types of functions used in a random module which is given below:

  • random.random()

This function generates a random float number between 0.0 and 1.0.

  • random.randint()

This function returns a random integer between the specified integers.

  • random.choice()

This function returns a randomly selected element from a non-empty sequence.

Example

# importing "random"  module.  

import random  

# We are using the choice() function to generate a random number from  

# the given list of numbers.  

print ("The random number from list is : ",end="")  

print (random.choice([50, 41, 84, 40, 31]))  


Q.1 What is Tuple? How to Defines Tuples?

Sol :- 

Python Tuple is used to store the sequence of immutable Python objects. The tuple is similar to lists since the value of the items stored in the list can be changed, whereas the tuple is immutable, and the value of the items stored in the tuple cannot be changed.

Creating a tuple

A tuple can be written as the collection of comma-separated (,) values enclosed with the small () brackets. The parentheses are optional but it is good practice to use. A tuple can be defined as follows.




ads2

Popular posts from this blog

11. Write a Java program to input basic salary of an employee and calculate its Gross salary according to following:

    11. Write a Java program to input basic salary of an employee and calculate its Gross salary according to following: Basic Salary <= 10000 : HRA = 20%, DA = 80% Basic Salary <= 20000 : HRA = 25%, DA = 90% Basic Salary > 20000 : HRA = 30%, DA = 95% Static Solution :-  class salary {     public static void main(String args[])     {     double basic=20000.50,gross,da,hra;     if(basic <=10000)     {         da = basic * 0.8;         hra = basic *0.2;     }              else if(basic <=20000)     {         da = basic * 0.9;         hra = basic *0.25;     }     else     {         da = basic * 0.95;         hra = basic * 0.3;     }     gross = basic + da + hra;     System.out.println("The Gross Salary is :-"+gross);     } } Output :-  Dynamic Solution :-  class salary {     public static void main(String args[])     {     double basic=20000.50,gross,da,hra;     Scanner in = new Scanner(System.in);     System.out.println("Enter the Basic Salary

1. Given the school result data, analyses the performance of the students on #different parameters, e.g subject wise or class wise.

1. Given the school result data, analyses the performance of the students on #different parameters, e.g subject wise  or class wise. Solution :-   # x-axis is shows the subject and y -axis # shows the markers in each subject # import pandas and matplotlib  import pandas as pd  import matplotlib.pyplot as plt # Simple Line Chart with setting of Label of X and Y axis, # title for chart line and color of line  subject = ['Physic','Chemistry','Mathematics', 'Biology','Computer'] marks =[80,75,70,78,82] # To draw line in red colour plt.plot(subject,marks,'r',marker ='*')     # To Write Title of the Line Chart plt.title('Marks Scored') # To Put Label At Y Axis plt.xlabel('SUBJECT')           # To Put Label At X Axis plt.ylabel('MARKS')             plt.show() Output :- 

class 10th chapter 3 Handling Images in HTML

    Choose the correct option from the following: (1) Which of the following tag is used to insert a video file into an HTML document ? (a) img (b) image (c) href (d) ime (2) As the image element does not cause a line break, it is also referred to as which of the following ? (a) An online image (b) An inline image (c) An outline image (d) Blank image (3) Which one of the following is a valid image file format ? (a) Img (b) Move (c) Mp3 (d) Png (4) Which of the following is provided when we use alt attribute of an image ? (a) Alternative description (b) Alt key definition  (c) Alternative image (d) Alternative HTML link (5) Which of the following attributes specify the values of height and width of the image in pixels ? (a) Img src (b) Height and width (c) H and V (d) Any of the above (6) Which of the following is not a valid image format ? (a) Imv (b) Png (c) Bmp (d) Gif (7) Which of the following concept is used to display whole image as a link ? (a) Image as hot spot (b) Hot text