Introduction
Welcome to my 44th blog post on the Python journey. On day 44, I learned about a very important concept i.e how the starting point of Python is executed. I learned about the working of the 'import' keyword in Python. Let's dive into more details.
So let's get started......
How 'import' works in python
In Python, using the 'import' keyword we can load the code from a Python module into the current script.
Importing allows us to make use of the functions and variables that are defined in the other module in our current script, as well as make use of any other additional modules that the imported module may depend on.
To import a module in Python, make use of the import statement followed by the name of the module.
Example - In the below code we import math module from python to perform mathematical functions.
import math
Once we import a module, we can make use of the functions and variables defined in the module by using dot notation.
Example -
import math
result = math.sqrt(9)
print(result)
3.0
'from' Keyword
We can also import specific functions or variables from a module using the 'from ' keyword.
Example - 1
from math import sqrt
result = sqrt(9)
print(result)
Example - 2
Here we import multiple functions from the module. In this case, multiple functions are separated by comma.
from math import sqrt, pi
result = sqrt(9)
print(result) # Output: 3.0
print(pi) # Output: 3.141592653589793
'*' Keyword - import everything
It's possible to import all functions and variables from a module using the '*' wildcard.
But importing all the functions can create confusion and make it difficult to understand from where specific functions and modules are imported.
Example -
from math import *
result = sqrt(9)
print(result) # Output: 3.0
print(pi) # Output: 3.141592653589793
'as' keyword
Python allows us to rename imported functions using the 'as' keyword.
Renaming the imported functions allows us to use a shorter or more descriptive name for a module, or if you want to avoid naming conflicts with other modules or variables in your code.
Example -
import math as m
result = m.sqrt(9)
print(result) # Output: 3.0
print(m.pi) # Output: 3.141592653589793
'dir' function
Python has an in-built function called 'dir' that can be used to view all the functions and variables defined in a module.
This can be useful when exploring the different functions of a module and understanding its work.
Example -
In the below example, we make use of the 'dir' keyword to view all the functions defined in the math module. This outputs a list of all the names defined in the math module, including functions like sqrt and pi, as well as other variables and constants.
import math
print(dir(math))
Resources Used
You can watch the video of Day#44 by clicking on the below link ๐๐๐๐๐
Conclusion
Thanks, guys for going through this blog post. On day 44, we learned about the working of the 'import' keyword and how it can be used to access various in-built and user-defined functions and variables of a module. This makes it easier for us to perform operations using predefined modules and access their code.
Thank you if you read this post and have found this post useful. I hope you have joined me and are enjoying my magical journey of Python coding. This is it for Day #44
See you in the next one.....
About Me
Hey Guys, I am Chintan Jain from CodeWithJain. I am a trader and content creator. I am also passionate about tech and hence wanted to explore the field of tech. I always wanted to learn to code so I watched many tutorials but procrastinated practicing coding. To get into the habit of coding consistently I am starting to BLOG with HASHNODE on daily basis.
I will document my coding journey from scratch and share my daily learnings in a blog post on HASHNODE. I hope you all will enjoy my content and my coding journey.
So what are you waiting for, smash the FOLLOW and LIKE buttons and follow along my coding journey, a step to create more beautiful digital products and empower people.