在 Python 中,您可以像在 MATLAB 中一样在一行中编写多行代码吗? [英] In Python, can you write multiple lines of code in one line like in MATLAB?
问题描述
我是一个从 MATLAB 过渡的 Python 新手(太贵了!)
I am a Python newbie transitioning from MATLAB (it's so expensive!)
我喜欢 MATLAB 的一件事是您可以通过将多行代码集中到一行MATLAB脚本来缩短代码(减少滚动).例如,假设我正在初始化一堆变量.在 Python 中,我会这样写:
One thing I like about MATLAB is you can make your code shorter (less scrolling) by lumping multiple lines of code into one line of a MATLAB script. For example, suppose I'm initializing a bunch of variables. In Python, I would write this:
a = 1
b = 2
c = 3
d = 4
e = 5
在 MATLAB 中我可以做同样的事情:
In MATLAB I can do the same thing:
a = 1;
b = 2;
c = 3;
d = 4;
e = 5;
然而,在长代码中,有时节省空间(和滚动)是很好的.在 MATLAB 中,我可以通过编写以下内容使其更加简洁:
However, in a long code, it is sometimes nice to save space (and scrolling). In MATLAB, I can make this much more concise by writing:
a = 1; b = 2; c = 3; d = 4; e = 5
Python 中是否有等价物将所有这些代码行压缩"为一行脚本?
Is there an equivalent in Python to "condense" all those lines of code into one line of script?
谢谢
推荐答案
是的,只需用分号分隔语句
Yes, simply separate the statements with a semicolon
a = 1; b = 2; c = 3; d = 4; e = 5
这篇关于在 Python 中,您可以像在 MATLAB 中一样在一行中编写多行代码吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!