2.0
Introduction to Prothon
2.1
Comments
Any line starting with the hash symbol, aka pound-sign
( # ), is a comment line and is ignored by the interpreter. If the hash
symbol appears in the middle of the line, then the rest of the line
is ignored.
>>> # print "Hello"
>>> print "hello" #, "world"
hello
>>>
You can also use C-style inline comments /* ...
*/ in the middle of a line of code or to block out large sections of
code easily in a file:
>>> print "hello" /* "there" */ , "world"
hello world
>>>
2.2
Indentation
Almost all languages have code blocks. These are multiple lines of
code that are logically grouped together to be executed as one unit
of code. In the C language for example, a code block is surrounded by
braces "{" and "}"
/* This is C code, NOT Prothon code*/
if (x)
{
do_something();
do_more();
}
In Prothon (and Python) the braces aren't needed. The indentation that
programmers usually use to help show the block structure anyway, is
itself the indicator of the blocks.
The
indentation rule is that when a new block is needed, a line will end
in a colon. Then the next line, which is the first line of the block,
is indented. Each line of that block then must be indented the same
amount to show it belongs to that block.
Blocks can be nested by having nested indentation, but lines further
down that go back to the left again must line up with a line above to
show which block it belongs to.
# Prothon code
if x:
do_something()
do_more()
if y:
do_even_more() # new nested block
do_more() # back to same block
Important:
Mixing Spaces and Tabs: You may use either spaces or tabs to indent
your blocks, but in any one group of nested blocks, you may not intermix
spaces and tabs. You will get an error if you do. The reason for this
rule is that mixed spaces and tabs would cause the lines to not line up
on editors with tab widths set differently.
2.3
Line Continuations
When a Prothon line is too long to fit in your text editor comfortably,
you can spread it across two or more lines by continuing a line of code
on the next line. Some of these ways don't work in the console so I'll
just show you examples here.
There are three ways to indicate a line continuation.
The first is to end a line with the backslash character ( \ ), making
sure that it is the very last character. This even works inside a string:
x = "Hello \
World"
Another way to continue a line is to end a line
while a left parentheses, bracket, or brace: (, [, or {, still does
not have it's matching right partner. The interpreter knows the line
must be continued until all such characters partner's are matched up:
x = 3 * (5 +
4 + 6)
Finally, the last way to continue a line is to
indent a line when the previous line does not end in a colon. Note that
you must still follow the indent rules, so if you are using tabs for
indents you must tab in at least once before using spaces for alignment:
x = factor(a,b)+
factor(c,d)
|