Python and Viv · Basics
Printing and formatting
What to notice
printputs a space between its arguments in both, andsep=", "(or any other text) replaces it.- A bare float prints differently. Python's
print(x)writes the shortest digits that read back as the same double; Viv'sprint(x)writes six significant digits, as C's%gdoes. Each language has the other's form:fmt(x)in Viv,f"{x:g}"in Python. - f-strings are the same on both sides for these specs:
{x},{x=},{x:.3f},{x:10.4f},{word!r}. The counts in this program are numbers, notInts, so an integer is written with:d({count:d}); without it,f"{count}"would be3.0. for i in range(count)isfor i to count. A loop body is in braces instead of indented.- Python's
assertstatements become assertions inside a Vivtestblock, which prints onePASS formatsline.
Python printing.py · 39 lines
Viv printing.viv · 43 lines
py2viv translated from printing.py
What py2viv adds before the program
(nothing)
(nothing)
6 lines: a header and the guard functions its translation calls
1###
2Translated from printing.py by py2viv 0.2.0 (numeric Python -> Viv).
3Python source sha256: 39a65d86d2965b5b7cefa62e0b70ae72350dbe42d9738f3fee584f8f66a5282a
4Each print(...) is one print(...) line; fmt(x) writes a float as Python's repr does.
5A `raise`, a failed assert and every guard `stop` with Python's exception line.
6###
1 print text and values
4name = "Ada"
5count = 3
6print("hello,", name)
7print("count:", count)
8print("a", "b", "c", sep=", ")
2name = "Ada"
3count = 3
4print("hello,", name)
5print("count:", count)
6print("a", "b", "c", sep=", ")
11name = "Ada"
12count = 3
13print("hello,", name)
14print("count:", fmt(count + 0.0, "d"))
15print("a, b, c")
2 the full digits of a float
11x = 2.0 / 3.0
12print(x)
13print(f"x = {x}")
14print(f"{x=}")
9x = 2.0 / 3.0
10print(fmt(x))
11print(f"x = {x}")
12print(f"{x=}")
17x = 2.0 / 3.0
18print(fmt(x))
19print("x =", fmt(x))
20print("x=", fmt(x), sep="")
3 six significant digits
17print(f"{x:g}")
18print(f"{1e-7:g} {123456789.0:g}")
15print(x)
16print(fmt(1e-7, "g"), fmt(123456789.0, "g"))
22print(fmt(x, "g"))
23print(fmt(1e-07, "g"), fmt(123456789.0, "g"))
4 a format per value
21print(f"x = {x:.3f}")
22print(f"x = {x:.3e}")
23print(f"[{x:10.4f}]")
24print(f"count = {count:d}, padded = [{count:4d}]")
19print(f"x = {x:.3f}")
20print(f"x = {x:.3e}")
21print(f"[{x:10.4f}]")
22print(f"count = {count:d}, padded = [{count:4d}]")
25print("x =", fmt(x, ".3f"))
26print("x =", fmt(x, ".3e"))
27print("[", fmt(x, "10.4f"), "]", sep="")
28print("count = ", fmt(count + 0.0, "d"), ", padded = [", fmt(count + 0.0, "4d"), "]", sep="")
5 strings in quotes
27word = "it's"
28print(repr(word))
29print(f"{word!r} has {len(word)} characters")
25word = "it's"
26print(repr(word))
27print(f"{word!r} has {len(word):d} characters")
30word = "it's"
31print(repr(word))
32print(repr(word), "has", fmt((len(word)) + 0.0, "d"), "characters")
6 build a line piece by piece
32line = ""
33for i in range(count):
34 line += f"{i:d} "
35print(line.rstrip())
30line = ""
31for i to count {
32 line += f"{i:d} "
33}
34print(line.rstrip())
34line = ""
35for i to count {
36 line = line + (fmt(i + 0.0, "d") + " ")
37}
38print(rstrip(line))
7 checks
38assert f"{x:.2f}" == "0.67"
39assert str(0.5) == "0.5"
37test "formats" {
38 assert_true(f"{x:.2f}" == "0.67")
39 assert_true(str(0.5) == "0.5")
40}
40if not (fmt(x, ".2f") == "0.67") {
41 stop("AssertionError")
42}
43if not (fmt(0.5) == "0.5") {
44 stop("AssertionError")
45}
What they print
identical — the two programs print the same text.
Dimmed lines are left out of the comparison. Each Viv test block prints one PASS line; the Python checks the same facts with assert, which prints nothing.
$ python3 printing.py
hello, Adacount: 3a, b, c0.6666666666666666x = 0.6666666666666666x=0.66666666666666660.6666671e-07 1.23457e+08x = 0.667x = 6.667e-01[ 0.6667]count = 3, padded = [ 3]"it's""it's" has 4 characters0 1 2
$ viv run printing.viv
hello, Adacount: 3a, b, c0.6666666666666666x = 0.6666666666666666x=0.66666666666666660.6666671e-07 1.23457e+08x = 0.667x = 6.667e-01[ 0.6667]count = 3, padded = [ 3]"it's""it's" has 4 characters0 1 2PASS formats
$ viv run printing.py (py2viv’s translation)
The translation prints exactly what the Python prints.
hello, Adacount: 3a, b, c0.6666666666666666x = 0.6666666666666666x=0.66666666666666660.6666671e-07 1.23457e+08x = 0.667x = 6.667e-01[ 0.6667]count = 3, padded = [ 3]"it's""it's" has 4 characters0 1 2