119 lines
2.4 KiB
Python
119 lines
2.4 KiB
Python
# Python Syntax Test
|
|
|
|
# Segments written by Kyler or (and mostly at that) from python docs
|
|
|
|
if 1900 < year < 2100 and 1 <= month <= 12 \
|
|
and 1 <= day <= 31 and 0 <= hour < 24 \
|
|
and 0 <= minute < 60 and 0 <= second < 60: # Looks like a valid date
|
|
return 1
|
|
|
|
month_names = ['Januari', 'Februari', 'Maart', # These are the
|
|
'April', 'Mei', 'Juni', # Dutch names
|
|
'Juli', 'Augustus', 'September', # for the months
|
|
'Oktober', 'November', 'December'] # of the year
|
|
|
|
# Here is an example of a correctly (though confusingly)
|
|
# indented piece of Python code:
|
|
def perm(l):
|
|
# Compute the list of all permutations of l
|
|
if len(l) <= 1:
|
|
return [l]
|
|
r = []
|
|
for i in range(len(l)):
|
|
s = l[:i] + l[i+1:]
|
|
p = perm(s)
|
|
for x in p:
|
|
r.append(l[i:i+1] + x)
|
|
return r
|
|
|
|
def perm2(l):
|
|
for i in range(len(l)):
|
|
s = l[:i] + l[i+1:]
|
|
p = perm(l[:i] + l[i+1:])
|
|
for x in p:
|
|
r.append(l[i:i+1] + x)
|
|
return r
|
|
|
|
'This string will not include \
|
|
backslashes or newline characters.'
|
|
|
|
re.compile("[A-Za-z_]" # letter or underscore
|
|
"[A-Za-z0-9_]*" # letter, digit or underscore
|
|
)
|
|
|
|
# Some examples of formatted string literals:
|
|
|
|
name = "Fred"
|
|
f"He said his name is {name!r}."
|
|
|
|
f"He said his name is {repr(name)}." # repr() is equivalent to !r
|
|
|
|
width = 10
|
|
precision = 4
|
|
value = decimal.Decimal("12.34567")
|
|
f"result: {value:{width}.{precision}}" # nested fields
|
|
|
|
today = datetime(year=2017, month=1, day=27)
|
|
f"{today:%B %d, %Y}" # using date format specifier
|
|
|
|
f"{today=:%B %d, %Y}" # using date format specifier and debugging
|
|
|
|
number = 1024
|
|
f"{number:#0x}" # using integer format specifier
|
|
|
|
foo = "bar"
|
|
f"{ foo = }" # preserves whitespace
|
|
|
|
line = "The mill's closed"
|
|
f"{line = }"
|
|
|
|
f"{line = :20}"
|
|
|
|
f"{line = !r:20}"
|
|
|
|
newline = ord('\n')
|
|
f"newline: {newline}"
|
|
|
|
def foo():
|
|
f"Not a docstring"
|
|
|
|
foo.__doc__ is None
|
|
|
|
# Triple and Multiline
|
|
|
|
"""Test" test"""
|
|
|
|
"""test
|
|
# test
|
|
test"""
|
|
|
|
# Some examples of integer literals:
|
|
7
|
|
2147483647
|
|
0o177
|
|
0b100110111
|
|
3
|
|
79228162514264337593543950336
|
|
0o377
|
|
0xdeadbeef
|
|
100_000_000_000
|
|
0b_1110_0101
|
|
|
|
# Some examples of floating point literals:
|
|
3.14
|
|
10.
|
|
.001
|
|
1e100
|
|
3.14e-10
|
|
0e0
|
|
3.14_15_93
|
|
|
|
# Some examples of imaginary literals:
|
|
3.14j
|
|
10.j
|
|
10j
|
|
.001j
|
|
1e100j
|
|
3.14e-10j
|
|
3.14_15_93j
|