Python Data Science Jobs & Interviews pinned «The latest and the most up-to-date cyber news will be presented on PPHM HACKER NEWS. PPHM subscribers are the first people that receive firsthand cybernews and Tech news. You won't miss any cyber news with us. https://t.iss.one/pphm_HackerNews»
❤4😐1
This media is not supported in your browser
VIEW IN TELEGRAM
modular programming exists in Python too. It helps us organize related code in a clean, maintainable, and scalable way. Let’s learn how to do this step by step in Python.
---
Step 1: Create a Module File
Create a new file called
---
Step 2: Use the Module in the Main File
Create another file called
---
Step 3: Run the Code
To run the program, just execute the following command in your terminal or in an IDE like VS Code:
---
Practice:
Now it’s your turn!
1. Create a new file called
2. Define functions like
3. Import and test them in
⭐️ https://t.iss.one/DataScienceQ
---
Step 1: Create a Module File
Create a new file called
string_utils.py
and write related functions inside it:# string_utils.py
def to_uppercase(s):
return s.upper()
def to_lowercase(s):
return s.lower()
---
Step 2: Use the Module in the Main File
Create another file called
main.py
and import the functions from string_utils
:# main.py
from string_utils import to_uppercase, to_lowercase
print(to_uppercase("hello")) # Output: HELLO
print(to_lowercase("WORLD")) # Output: world
---
Step 3: Run the Code
To run the program, just execute the following command in your terminal or in an IDE like VS Code:
python main.py
---
Practice:
Now it’s your turn!
1. Create a new file called
math_utils.py
.2. Define functions like
add
, subtract
, and multiply
inside it.3. Import and test them in
main.py
.Please open Telegram to view this post
VIEW IN TELEGRAM
👍6
This media is not supported in your browser
VIEW IN TELEGRAM
👍2
You will get a runtime error, and the program will crash.
---
In Python, if you attempt to import a module that doesn't exist, the interpreter will raise a runtime error called
ModuleNotFoundError
. This error immediately stops the execution of the program—unless it is properly handled using a try-except
block.---
import my_fake_module
ModuleNotFoundError: No module named 'my_fake_module'
---
To prevent the program from crashing, you can catch the error using t
ry-except:
try:
import my_fake_module
except ModuleNotFoundError:
print("Module not found, but the program continues running.")
In Python, importing a non-existent module leads to a runtime error. If not handled, it will crash the program.
Stay tuned for tomorrow’s question
Please open Telegram to view this post
VIEW IN TELEGRAM
❤5👍1🥰1
This media is not supported in your browser
VIEW IN TELEGRAM
👏3
In Python, when you import a module, it's only loaded once from disk and then cached in memory (RAM). So if you modify that module later, Python won’t reload it automatically — even if you import it again!
---
Let’s say you have a file called
mathutils.py
with:def add(a, b):
return a + b
You import it in
main.py
like this:import mathutils
Later, you update
mathutils.py
and add a new function:def subtract(a, b):
return a - b
If you now run
import mathutils
again in main.py
, Python will not see the new subtract()
function. It uses the cached version already loaded in memory.---
import importlib
import mathutils
importlib.reload(mathutils) # Forces Python to reload the updated module
# Now the new function is accessible
print(mathutils.subtract(10, 3)) # ➡️ Output: 7
---
To improve performance, Python loads modules from disk only once, and then stores them in memory (RAM).
If the module file changes, Python doesn’t detect it unless you explicitly tell it to reload.
- Re-importing a module doesn’t reload its changes.
- Use
importlib.reload()
to reload the updated version from disk.Please open Telegram to view this post
VIEW IN TELEGRAM
👍5👏1
This media is not supported in your browser
VIEW IN TELEGRAM
👍1
Forwarded from Python | Machine Learning | Coding | R
This channels is for Programmers, Coders, Software Engineers.
0️⃣ Python
1️⃣ Data Science
2️⃣ Machine Learning
3️⃣ Data Visualization
4️⃣ Artificial Intelligence
5️⃣ Data Analysis
6️⃣ Statistics
7️⃣ Deep Learning
8️⃣ programming Languages
✅ https://t.iss.one/addlist/8_rRW2scgfRhOTc0
✅ https://t.iss.one/Codeprogrammer
Please open Telegram to view this post
VIEW IN TELEGRAM
👍1
This media is not supported in your browser
VIEW IN TELEGRAM