Difference between revisions of "Python"
Line 116: | Line 116: | ||
=== range function === | === range function === | ||
+ | |||
+ | The very common built-in function <c>range</c> in both languages takes up to three arguments: ''start'', ''stop'', and ''step''. But there are a few differences to note: | ||
+ | |||
+ | * In Python, the ''stop'' value is not included in the resulting sequence, while in MiniScript it is (i.e., it is an inclusive bound). | ||
+ | * In Python, if ''stop'' is less than ''start'', the result is an empty list unless you also specify a negative ''step''. In MiniScript, if ''step'' is omitted in such a case, it defaults to -1 (and you get a list that counts down). | ||
+ | * In Python, if only one argument is given, it is considered ''stop'' and the resulting sequence counts from 0 up to (but not including) that value. In MiniScript, a single argument is ''start'' and the resulting sequence counts from that number down (or up) to 0. | ||
+ | * In Python, the result of <c>range</c> is a '''range''' object; in MiniScript it is an ordinary list. |
Revision as of 21:24, 27 November 2022
Python is a popular scripting language that was first released in 1991. This article compares Python to MiniScript, and may be useful to people coming to MiniScript with a background in Python.
Contents
Similarities
Data Types
MiniScript's six data types have direct analogs to common types in Python:
- Numbers in MiniScript are double-precision floating point numbers, equivalent to Python's
numbers.Real
(akafloat
) data type. Note that in MiniScript, False and True are represented directly by the numbers 0 and 1; there is no separatebool
type.
- Strings in both languages are an immutable sequence of Unicode code points. String literals in both languages can be enclosed in double-quotes; MiniScript does not have the optional single-quote or triple-quote syntax. While Python uses backslash-escaping to include a quotation mark within a literal, in MiniScript you just write the quotation mark twice, as in SQL.
- Lists in both languages are a mutable sequence of arbitrary values; a list literal uses square brackets surrounding comma-delimited values (see List Syntax, below). Note that MiniScript does not have a
tuple
type, and uses a list in situations where Python might use a tuple.
- Maps in both languages are mutable, unordered set of key-value pairs, with the same literal syntax (see Map Syntax and Object-Oriented Programming, below).
- Function objects in both languages are similar: first-class, callable objects. However functions in MiniScript have no attributes, and the syntax for defining functions is somewhat different.
null
is a special datatype in both languages, representing a null reference.
List Syntax
(ToDo)
Map Syntax
(ToDo)
Variable Scope
Variables in both Python and MiniScript are local by default. There are some subtle differences, however: in Python, an inner function can implicitly access the variables of an outer function, but can't implicitly access global variables, while in MiniScript, any function can implicitly read values from both the outer context and the global context. To update a global variable, where Python uses a special global
statement, MiniScript uses a globals
prefix in a manner similar to using self
to access object data.
(ToDo: example)
Object-Oriented Programming
(ToDo)
Differences
Language Scale
MiniScript is a considerably smaller language by any reasonable measure. For example, the following table compares Python and MiniScript on C/C++ source code lines, source code files, number of data types, and number of standard intrinsic functions, as of 2021 (source).
Python | MiniScript | |
---|---|---|
Source Lines | 661,775 | 13,752 |
Source Files | 718 | 46 |
Data Types | 34 | 6 |
Intrinsics | 69 | 53 |
Block Syntax
The most obvious difference between Python and MiniScript is in block syntax. While Python defines blocks by ending the opening line with a colon, and indenting the body of the block, MiniScript uses keyword pairs like if
/end if
, for
/end for
, and function
/end function
(more similar to Visual Basic). Indentation in MiniScript is strictly cosmetic, and is ignored by the compiler; and tools can automatically standardize or re-indent based on the keywords.
Python | MiniScript |
---|---|
def myfunc(): for i in range(1, 11): if i == 9: print("Almost done...") else: print(i) myfunc() |
myFunc = function
for i in range(1, 10)
if i == 9 then
print "Almost done..."
else
print i
end if
end for
end function
myFunc
|
Function Invocation
While functions in Python are always invoked by putting parentheses after a function reference, in MiniScript, such parentheses can be omitted when (1) the function call is the entire statement, or (2) there are no arguments.
Python | MiniScript |
---|---|
import random x = random.uniform(0,1) print(x) |
x = rnd
print x
|
Magic Methods
import
range function
The very common built-in function range
in both languages takes up to three arguments: start, stop, and step. But there are a few differences to note:
- In Python, the stop value is not included in the resulting sequence, while in MiniScript it is (i.e., it is an inclusive bound).
- In Python, if stop is less than start, the result is an empty list unless you also specify a negative step. In MiniScript, if step is omitted in such a case, it defaults to -1 (and you get a list that counts down).
- In Python, if only one argument is given, it is considered stop and the resulting sequence counts from 0 up to (but not including) that value. In MiniScript, a single argument is start and the resulting sequence counts from that number down (or up) to 0.
- In Python, the result of
range
is a range object; in MiniScript it is an ordinary list.