Type Here to Get Search Results !

Write Python program to demonstrate use of: a)Built-in module (e.g. keyword, math, number, operator) b) User defined module

 Write Python program to demonstrate use of: a)Built-in module (e.g. keyword, math, number, operator) b) User defined module


a) Built-in Modules

Built-in modules are written in C and integrated with the Python interpreter. Each

built-in module contains resources for certain system-specific functionalities such as

OS management, disk IO, keyword, math, number, operator etc. The standard library

also contains many Python scripts (with the .py extension) containing useful utilities.

To display a list of all available modules, use the following command in the Python

console:

>>> help('modules')

Write Python program to demonstrate use of: a)Built-in module (e.g. keyword, math, number, operator) b) User defined module


b) Python - Math Module

Some of the most popular mathematical functions are defined in the math module.

These include trigonometric functions, representation functions, logarithmic functions,

angle conversion functions, etc. In addition, two mathematical pie and Euler's number

constants are also defined in this module.

Example

>>> import math

>>>math.pi

3.141592653589793


c) Python - OS Module

It is possible to automatically perform many operating system tasks. The OS module in

Python provides functions for creating and removing a directory (folder), fetching its

contents, changing and identifying the current directory, etc.

Example

We can create a new directory using the mkdir() function from the OS module.

>>> import os

>>>os.mkdir("d:\\tempdir")

d) Python - Random Module

Functions in the random module depend on a pseudo-random number generator

function random(), which generates a random float number between 0.0 and 1.0.

Example

>>>import random

>>>random.random()

0.645173684807533


e) User-defined Module

The user-defined module is written by the user at the time of program writing.

i) Creation a User-defined Module

To create a module just write a Python code in a file with file extension as.py:

Example

A module in a file with extension as module_name.py in Python is created.

def accept_int():

 val = int(input("Please enter any integer integer: "))

 print('The integer value is', val)

ii) Accessing a User-defined Module

Now we will access the module that we created earlier, by using the import statement.

Example

import the module in Python.

import module_name

Output

Please enter any integer integer: 22

The integer value is 22


Example

1. Write a Python program to create a user defined module that will ask your collegename and will display the name of the college

Simple python file

import askInfo
askInfo.info()

user defined askinfo module file

def info():
    collageName = input("Enter your collage name : ")
    print("\nYour collage name is " ,collageName)


2. Write a Python program that will calculate area and circumference of circle usinginbuilt Math Module

import math

radius = float(input("Enter radius of circle : "))

print("Area of circle" , math.pi * radius * radius)

print("Circumference of circle" , 2 * math.pi * radius)


3. Write a Python program that will display Calendar of given month using CalendarModule

import calendar
year = 2022
month = int(input("Enter month : "))
print("\n\n" , calendar.month(year , month))
Tags

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

Below Post Ad