Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the ";" exist when it goes against formatting standards?

As you know, the ; symbol is used to separate lines like this:

print "Hai"; print "Bai"

From what I've heard, this goes against formatting standards for python. So, if this goes against formatting standards, why was it implemented? How is it used properly?

like image 272
Vladimir Putin Avatar asked Jan 19 '26 17:01

Vladimir Putin


2 Answers

";" are used in cases, you have no other choice. E.g. when calling Python from command line and willing to execute those two statements.

$ python -c "print 'Hai'; print 'Bai'"
Hai
Bai

Or more complex cases:

$ python -c 'a=1; b=2; print "a + b = {a} + {b} = {res}".format(a=a, b=b, res=a+b)'
a + b = 1 + 2 = 3

note: Do not take my "no other choice" literally, you will find nice alternatives to some cases in comments.

like image 184
Jan Vlcinsky Avatar answered Jan 22 '26 05:01

Jan Vlcinsky


To extend on Jan Vlcinsky's answer, if you're trying to quickly determine the efficiency of something, timeit comes in handy. For example, (from the docs):

python -m timeit -s 'text = "sample string"; char = "g"'  'char in text'
like image 44
Dair Avatar answered Jan 22 '26 05:01

Dair