Day 30: Comprehensive Look at Python Modules and Packages

Exploring Python Modules and Packages: Part Two

Day 30: Comprehensive Look at Python Modules and Packages

Introduction:

Hello fellow coders! Today, we will discuss something more on Module and Packages in my Python journey, and I'm excited to share my experiences with you. In this series, I'll document my progress, successes, and setbacks. Join me as I explore the world of Python !

member aliasing:

Eg ;

from jithonmath import no as x,ADD as sum

print(x)

sum(10,20)

Once we defined as alias name ,we should use alias name only and we should not use original name

Reloading a Module: -

By default module will be loaded only once eventhough we are importing multiple multiple times.

module.py

print("This is from module1")

demo.py

#Demo Program for module reloading

eg :

import time

from imp import reload

import module1

time.sleep (30)

reload (module1)

time.sleep (30)

reload (module1)

print ("bye ")

Finding members of module by using dir() function: -

Python provides inbuilt function dir() to list out all members of current module or

a specified module.

dir() ===>To list out all members of current module

test.py:

import jithonmath2 print(dir(jithonmath))

Working with random module: -

This module defines several functions to generate random numbers

1. random() function: -

This function always generate some float value between 0 and 1 ( not inclusive)

0<x<1

Eg :

from random import * for i in range(10): print(random())

2. randint() function: -

To generate random integer between two given numbers(inclusive)

Eg :

from random import *

for i in range(10):

print(randint(1,200))

3. uniform(): -

It returns random float values between 2 given numbers(not inclusive)

Eg:

from random import *

for i in range(10):

print(uniform(1,200))

random() ===>in between 0 and 1 (not inclusive)

randint(x,y) ==>in between x and y ( inclusive)

uniform(x,y) ==> in between x and y ( not inclusive)

4. randrange([start],stop,[step])

returns a random number from range

start<= x < stop

start argument is optional and default value is 0

step argument is optional and default value is 1

randrange(10)-->generates a number from 0 to 9

randrange(1,11)-->generates a number from 1 to 10

randrange(1,11,2)-->generates a number from 1,3,5,7,9

Eg:

from random import *

for i in range(10):

print(randrange(10))

Eg:

from random import *

for i in range(10):

print(randrange(1, 11))

Eg:

from random import *

for i in range(10):

print(randrange(1, 11, 2))

5. choice() function:

It wont return random number.

It will return a random object from the given list or tuple.

Eg :

from random import *

list=["C","C++","Java","Py","R", "database"]

for i in range(6):

print(choice(list))

Working time module : -

eg :

#Get Current Date

from datetime import date

print (dir ())

today = date.today()

print("Today's date is", today)

eg :

#Get Today’s Year, Month, and Date

from datetime import date

today = date.today()

print("Current year:", today.year)

print("Current month:", today.month)

print("Current day:", today.day)

Challenges :

  1. Understanding various types of Modules and packages. .

  2. Handling errors.

Resources :

  1. Official Python Documentation: Modules and packages.

  2. W3Schools' Python Tutorial: Modules and packages.

  3. Scaler's Python Courses : Modules and packages.

Goals for Tomorrow :

  1. Explore something more on modules.

Conclusion :

Day 30’s a success!

What are your favorite Python resources? Share in the comments below.

Connect with me :

GitHub: [ github.com/p-archana1 ]

LinkedIn : [ linkedin.com/in/archana-prusty-4aa0b827a ]

twitter : [ x.com/_archana77 ]

Join the conversation :

Share your own learning experiences or ask questions in the comments.

HAPPY LEARNING!!

THANK YOU!!