Darcy
Darcy

Reputation: 659

In Python, can you write multiple lines of code in one line like in MATLAB?

I am a Python newbie transitioning from MATLAB (it's so expensive!)

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

In MATLAB I can do the same thing:

a = 1;
b = 2;
c = 3;
d = 4;
e = 5;

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

Is there an equivalent in Python to "condense" all those lines of code into one line of script?

Thanks

Upvotes: 1

Views: 2563

Answers (3)

ralf janser
ralf janser

Reputation: 21

copy paste this code to python (3.x) and run it

a = 1; b = 2 ; c = 3

print (a,b,c)

d= "I'am " ; e= "too "; f= "lazy "; g="to " ; h = "try"; i = "things" ; j = " on" ; k = "my "; l= "own !"

print (d,e,f,g,h,i,j,k,l)

besides, there are many shorter ways to do this too

Upvotes: 1

Yilun Zhang
Yilun Zhang

Reputation: 9018

Yes, and you can also do something like this in Python:

a,b,c,d,e = 1,2,3,4,5;

Upvotes: 3

rsaxvc
rsaxvc

Reputation: 1780

Yes, simply separate the statements with a semicolon

a = 1; b = 2; c = 3; d = 4; e = 5

Upvotes: 4

Related Questions