Python Modules - NareshIT
Python Modules

Python Modules - NareshIT

Introduction:

As we know that Python is a more powerful language that offers great tools for data crunching and preparation, as well as for complex scientific data analysis and modeling. Python is a popular programming language. It was created by Guido van Rossum, and released in 1991. It is basically used for:

  • web application development (server-side),
  • software development,
  • mathematics,
  • system scripting.

It has a wide range of inbuilt modules which makes it more unique and different than other languages. It also supports the concept of an OOPS environment and hence it is a more popular language. Here I am going to discuss its various features in detail.

Built-in Modules in Python:

1.?????These are the modules that are mainly written in C language and interpreted by the Python interpreter.

2.?????Here each module that is present are having specific functionalities.

3.?????In contrast to functionalities, they are used to have OS management, Disk I/O, etc which are used to exhibit their task.

4.?????Here we are having the standard library which is used to contain many python scripts with useful utilities.?

5.?????There are several built-in modules in python at our disposal that we can use whenever we want.

6.?????If we need to see all the available modules in Python, we are using the command help.

Let us see how we can use the help command. To execute the command, we need to write the command on the Console in the following manner.

help('modules')

When the command gets run then it will be listed out all the inbuilt modules of Python.

?

OSmodule in Python:

?1.?????Like other modules the OS (Operating System) is also a module in Python.

2.?????As the OS module in Python provides functions for interacting with the operating system so it is being considered under Python’s standard utility modules.

3.?????It is basically used to provide a portable way of using operating system-dependent functionality.

4.?????The *os* and *os.path* modules include many functions to interact with the file system.

5.?????Python is used to support the concept of Handling the Current Working Directory.

6.?????The Current Working Directory is used to mean that it is a folder, where Python is operating.

7.?????if we are going to Consider?Current Working Directory (CWD)?as a folder then Whenever the files are called only by their name, Python assumes that it starts in the CWD which means that name-only reference will be successful only if the file is in the Python’s CWD.

Let us consider the following program which will let you know about the use of CWD.

Program:

# Python program to explain os.getcwd() method?

??????????

# importing os module?

import os?

??????

# Get the current working?directory (CWD)?

cwd = os.getcwd()?

??????

# Print the current working directory (CWD)?

print("Current working directory:", cwd)?

?

Output: When the above program will run then the output will be like as below.

?

Current working directory: /home/current_directory/Desktop/gfg        

?

Changing the Current working directory:

1.?????Similarly, if we need to change the CWD we are going to use?os.chdir()?method.

2.?????This method when get used then it basically changes the CWD to a specified path.

3.?????It should also be get noted that this method only takes a single argument as a new directory path.

Let us consider the following program which will let you know about the use of changing of CWD.

Program:

# Python program to change the?current working directory?

????import os?

# Function to Get the current?working directory?

def current_path():?

????print("Current working directory before")?

????print(os.getcwd())?

????print()?

?# Driver's code?for printing CWD before?

current_path()?

# Changing the CWD?

os.chdir('../')?

# Printing CWD after?

current_path()

Output: When the program will runs then the output will be get shown below.

Current working directory before

C:\Users\Current_Directory\Desktop\gfg

Current working directory after

C:\Users\ Current_Directory \Desktop

Math:

Like the OS module in Python , we are having Math as a built-in module that can be used for performing mathematical tasks.

As in Python, the?math?module has an inbuilt set of methods and constants so we needn’t require to put any effort into this. Here I am going to discuss some important methods below which will let you know about them in brief.


No alt text provided for this image
No alt text provided for this image

Random:

1.?????We can be able to generate the Random?integer values by using the randint()?function.

2.?????When we are going to use the function?randint() then it is used to takes two arguments such as start and end.

3.?????Here the start and the end are used to denote the range for the integer to be generated values.?

4.?????When we are concerned about the Random?integers, then they are used to be get generated within the boundary values. Here the boundary values are the start and end of range values.

5.?????More specifically they are the interval [start, end].

Here, I am going to discuss How to generate the Random Integer in Python . Here we are also going to discuss how they are used to support the inbuilt functions which are being provided by the Python library.

Random Number Generator in Python:

1.?????A Random Number Generator in Python is a function that is used to generate a random number whenever it is get called.

2.?????It is a built-in function in Python.

3.?????It is a function that is present in the Python Random module.

4.?????So, whenever we need to generate a random number we need to call this module and invoke the Generate method.

5.?????Along with this method this module also consists of other random number generator functions methods such as seed(), randrange(), randint(), choice(), shuffle(), sample(), and uniform().

6.?????These methods are also called supporting methods for Random number generation.

7.?????The seed() method is basically used to produce values which are deterministic in nature.

8.?????A deterministic value is used to have the same sequence of values.

9.?????The randrange() method is basically used when we need to return random values between the specified limit and interval.

10.?The randint() is basically used when we are going to return a random integer as per the given limit.

11.?The choice() method is basically get used when we need to return a random number from a given sequence of numbers.

12.?The suffle() method is basically used when we need a shuffle value within a given sequence of numbers.

13.?The sample() method is basically get used when we need to return randomly selected values.

14.?The uniform() method is basically get used when we need to return the floating-point values from a given range of values.

Let us consider the example as discussed below which will let you know how to use these methods.

randrange():

1.?????As we know that the randrange() function is basically used when we need to return random values between the specified limit and interval.

2.?????It is basically used to allow the user to generate values by stepping over the interval count.

Syntax:

randrange()

Let us consider the following example which shows the use of randrange().

import random

for x in range(5):

???print(random.randrange(2,60,2))

randint():

1.?????When we are going to use the randint() method then it is basically used to generate integers between a given limit.

2.?????Here It will take two parameters as input which are used to specify the limit values.

3.?????Here the lower limit is get specified by the first parameter whereas the second specifies the upper limit.

Syntax:

randint(a,b)?

here a is the lower limit and b is the upper limit.

Example:

import random

random.randint(1,5)

Similarly, if we need to generate the series of sequences then we may take the approach of the loop here. But it should be get noted that we need to put the function as a body of the loop.

For Example:

import random

for x in range(2):

???print(random.randint(2,11))

DateTime:

1.?????Like other inbuilt modules, In Python , we are having a module named?DateTime.

2.?????It is basically get used by importing it to our work with the date as well as time.

3.?????It should be get noted that here the date and time are not a data type of their own.?

4.?????The Datetime module in Python is used to supply the classes to work with date and time.

5.?????These classes provide a number of functions to deal with dates, times, and time intervals.

6.?????It should be get noted that the Date and DateTime are an object in Python, so when we need to manipulate them, then they are going to manipulate the objects and not string or timestamps.

In Python, the datetime classes are basically used to get categorized into 6 main classes. The description of these classes is discussed below.

  • ???date?– It is having the attributes of year, month, and day. It is having an idealized naive date.
  • ???time?– An idealized time, independent of any particular day, assuming that every day has exactly 24*60*60 seconds.
  • ???datetime?– It is basically a combination of date and time classes. Hence it has all the attributes like a year, month, day, hour, minute, second, microsecond, and tzinfo.
  • ???timedelta?– It is basically get used when we need to specify the duration expressing the difference between two date, time, or date time instances to microsecond resolution.
  • ??tzinfo?– It provides time zone information objects.
  • ???timezone?– A class that implements the tzinfo abstract base class as a fixed offset from the UTC (New in version 3.2).

Let us consider the following example which will let you know to understand the concept in detail.

Program:

# Python program to?demonstrate date class

# import the date class

from datetime import date

# initializing constructor and passing arguments in the?

# format year, month, date

my_date = date(1996, 12, 11)

print("Date passed as argument is", my_date)

# Uncommenting my_date = date(1996, 12, 39)

# will raise an ValueError as it is

# outside range

# uncommenting my_date = date('1996', 12, 11)

# will raise a TypeError as a string is?

# passed instead of interger

Output:

Date passed as argument is 1996-12-11        

Scope @ NareshIT:

1.?????At NareshIT’s Python application Development program you will be able to get extensive hands-on training in front-end, middleware, and back-end technology.

2.?????It skilled you with phase-end and capstone projects based on real business scenarios.

3.?????Here you learn the concepts from leading industry experts with content structured to ensure industrial relevance.

4.?????An end-to-end application with exciting features

You can contact us anytime for your Python training , and from any part of the world. Naresh I Technologies caters to one of the best Python training in India.

Follow us on Linkedin: https://bit.ly/NITLinkedIN

要查看或添加评论,请登录

社区洞察

其他会员也浏览了