A Python module is a file with definitions and statements that can include functions, classes, and runnable code, making code organization easier. Modules can be imported using the import statement, and the dot operator is used to access their contents. Python also allows renaming modules upon import and includes several built-in modules like math and random for various functionalities.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
4 views
Python Module
A Python module is a file with definitions and statements that can include functions, classes, and runnable code, making code organization easier. Modules can be imported using the import statement, and the dot operator is used to access their contents. Python also allows renaming modules upon import and includes several built-in modules like math and random for various functionalities.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2
# module math
What is Python Module
A Python module is a file containing from math import sqrt, factorial Python definitions and statements. A # if we simply do "import math", then module can define functions, classes, and variables. A module can also # math.sqrt(16) and math.factorial() include runnable code. # are required. Grouping related code into a module print(sqrt(16)) makes the code easier to understand and use. It also makes the code print(factorial(6)) logically organized. Import all Names Create a Python Module The * symbol used with the import To create a Python module, write the statement is used to import all the desired code and save that in a file names from a module to a current with .py extension namespace. Ex: Syntax: # A simple module, calc.py from module_name import * # importing sqrt() and factorial from the def add(x, y): # module math return (x+y) from math import * def subtract(x, y): return (x-y) # if we simply do "import math", then Import module in Python # math.sqrt(16) and math.factorial() We can import the functions, and classes defined in a module to another # are required. module using the import statement in print(sqrt(16)) some other Python source file. print(factorial(6)) When the interpreter encounters an import statement, it imports the module Locating Python Modules if the module is present in the search Whenever a module is imported in path. Python the interpreter looks for several Syntax to Import Module in locations. First, it will check for the built- Python in module, if not found then it looks for a import module list of directories defined in Note: This does not import the the sys.path. Python interpreter functions or classes directly instead searches for the module in the following imports the module only. To access the manner – functions inside the module the dot(.) Directories List for Modules operator is used. Here, sys.path is a built-in variable within Ex: the sys module. It contains a list of # importing module skd.py directories that the interpreter will search for the required module. import skd # importing sys module print(skd.add(10, 2)) import sys
Python Import From Module
# importing sqrt() and factorial from the # importing sys.path print(sys.path) # printing random integer between 0 and 5 print(random.randint(0, 5)) Renaming the Python # print random floating point number between 0 and Module 1 We can rename the module while print(random.random()) importing it using the keyword. Syntax: Import Module_name as # random number between 0 and 100 Alias_name print(random.random() * 100) # importing sqrt() and factorial from the ……………………………………………………………. List = [1, 4, True, 800, "python", 27, "hello"] # module math # using choice function in random module for import math as mt choosing # if we simply do "import math", then # a random element from a set such as a list print(random.choice(List)) # math.sqrt(16) and math.factorial() # are required. ………………………………………………………………………… # importing built in module datetime print(mt.sqrt(16)) import datetime from datetime import date print(mt.factorial(6)) import time Python Built-in modules # Returns the number of seconds since the There are several built-in modules in # Unix Epoch, January 1st 1970 Python, which you can import whenever print(time.time()) you like. # importing built-in module math # Converts a number of seconds to a date object import math print(date.fromtimestamp(454554)) # using square root(sqrt) function contained ………………………………………………………….. # in math module print(math.sqrt(25)) ……………………………………………………….. # using pi function contained in math module print(math.pi) ………………………………………………………………. # 2 radians = 114.59 degrees print(math.degrees(2)) ………………………………………………….. # 60 degrees = 1.04 radians print(math.radians(60)) ………………………………………….. # Sine of 2 radians print(math.sin(2)) ………………………………………………….. # Cosine of 0.5 radians print(math.cos(0.5)) …………………………………………………………… # Tangent of 0.23 radians print(math.tan(0.23)) …………………………………………………………….. # 1 * 2 * 3 * 4 = 24 print(math.factorial(4)) ………………………………………………………………….. # importing built in module random import random ………………………………………………………………….
Instant ebooks textbook (Ebook) Guide to Discrete Mathematics: An Accessible Introduction to the History, Theory, Logic and Applications (Texts in Computer Science) by Gerard O'Regan ISBN 9783030815875, 3030815870 download all chapters