Can I run sections of code independently in Python and C++ like in MATLAB?

  • Context: MATLAB 
  • Thread starter Thread starter member 428835
  • Start date Start date
  • Tags Tags
    C++ Matlab Python
Click For Summary
SUMMARY

This discussion clarifies the capabilities of running code sections independently in Python and C++ compared to MATLAB. Python, being an interpreted language, allows for dynamic code changes and execution, especially when using Jupyter notebooks, which enable running code cells independently. In contrast, C++ is a compiled language, necessitating recompilation for any code modifications. The conversation highlights the advantages of using interpreted languages for iterative development and debugging.

PREREQUISITES
  • Understanding of interpreted vs. compiled languages
  • Familiarity with Python programming and Jupyter notebooks
  • Basic knowledge of C++ compilation process
  • Awareness of MATLAB programming environment
NEXT STEPS
  • Explore Python's interactive features in Jupyter notebooks
  • Learn about Python debugging tools and techniques
  • Investigate C++ wrapper libraries for integrating with Python
  • Study the differences between scripting and interactive execution in MATLAB
USEFUL FOR

Programmers transitioning from MATLAB to Python, data scientists utilizing Jupyter notebooks, and developers needing to understand the differences in execution between interpreted and compiled languages.

member 428835
Hi PF!

I typically code in MATLAB. Here, if I run the following program
Code:
%% Section 1
x = 5;
y = 2;
%% Section 2
z = x + y;

everything works. But now let's say I wanted to change line 5 to z = x - y. In MATLAB this is simple: I can simply change line 5 and rerun section 2. Is there a way to do this in Python and C++, or do I have to rerun the entire program?

Thanks for your help!
 
Physics news on Phys.org
MATLAB and Python are interpreted languages. You can change the code whenever you like. A program reads and executes that code.

C++ is compiled : a program called a compiler takes the source code and produces unreadable machine code that then may be executed by the CPU. Usually in order to change anything you have to recompile it, though there are special programs called debuggers that you can use to stop the program and read data.

Compiled code executes more quickly, but if you are spending most of your computer time in someone else's routines then it may not matter much.

So I would suspect you can easily do what you want to do in Python, but I seldom use it so I don't know how. Can't do it in C++.
 
  • Like
Likes   Reactions: member 428835
joshmccraney said:
In MATLAB this is simple: I can simply change line 5 and rerun section 2
Because Matlab is interpreted, as is Python. That means they can be changed at run time.

C++ is compiled so requires recompilation for changes.

EDIT: I see Hornbein beat me to it
 
Last edited:
  • Like
Likes   Reactions: member 428835
While you may be able to rerun section 2 in a Matlab program, in other languages interpreted or otherwise you'll need to run the entire program from start to finish.

This is one of the reasons programmers break up code into separate steps (programs) with intermediate files to allow rerunning one section on long runs or to restart when an error occurs like a lost network connection or out of disk space condition.

In truth, I didn't know you could rerun a portion of a Matlab program. I've always rerun from the beginning.
 
  • Like
Likes   Reactions: member 428835
There is also a difference between running a script from the command line (which runs the entire thing each time) as opposed to using an interactive session (where you can step through line by line and change things or repeat things as you go).
 
  • Like
Likes   Reactions: pbuk and member 428835
In data science, Python is often used via Jupyter notebooks. These consist of cells of code which can be executed independently. So yes, what you are asking is not only possible but also common.

Jupyter works with multiple languages. Actually, its name is kind of a wordplay on them: Julia, Python and R.

As the others have written, C++ is a completely different beast. If Python programmers need the speed provided by C or C++, they mostly use wrapper libraries which are called like normal Python code but use C/C++ under the hood.
 
  • Like
Likes   Reactions: The Bill and member 428835

Similar threads

  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 11 ·
Replies
11
Views
5K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K