May 14, 2021 Julia
A variable in Julia is a name associated with (or bound) to a value. I t does this when you want to store a value (for example, you get some values after doing some math, and you need to use them later). For example:
# 给变量 x 赋值为 10
julia> x = 10
10
# 用 x 的值做一些数学运算
julia> x + 1
11
# 重新给 x 赋值
julia> x = 1 + 1
2
# 您可以为变量赋给种类型的值,例如文本字符串等
julia> x = "Hello World!"
"Hello World!"
Julia provides an extremely flexible variable naming system. Variable names are case sensitive.
julia> x = 1.0
1.0
julia> y = -3
-3
julia> Z = "My string"
"My string"
julia> customary_phrase = "Hello world!"
"Hello world!"
julia> UniversalDeclarationOfHumanRightsStart = "人人生而自由,在尊严和权力上一律平等。"
"人人生而自由,在尊严和权力上一律平等。"
You can also use the Unicode character (UTF-8 encoding) to name it:
julia> δ = 0.00001
1.0e-5
julia> 안녕하세요 = "Hello"
"Hello"
In Julia REPL and several other Julia editing environments, you can type many Unicode math symbols by entering the backslash symbol name and then the label.
For example,
δ
be typed by typing by
\_2
s
\delta
or even by entering the value of the file by entering s
\alpha
tab
- s
\hat
-
tab
α̂₂
α̂₂.
Julia even allows you to redefine the built-in constants and functions:
julia> pi
π = 3.1415926535897...
julia> pi = 3
Warning: imported binding for pi overwritten in module Main
3
julia> pi
3
julia> sqrt(100)
10.0
julia> sqrt = 4
Warning: imported binding for sqrt overwritten in module Main
4
It is clear that such an approach is not encouraged.
Variable names must start with letters (a-z or A-Z), underscores, or a Unicode encoded pointer that points to a larger subset of pointers than 00A0; C haracters after the first place also include! and numbers (0-9 and other characters Nd/No), as well as other Unicode encoded pointers: donics and other modified markers (letters Mn/Mc/Me/Sk), some punctuation connectors (letter PCs), prime numbers, and others.
Operators like
+
identifiers, but require special resolution. I
n some cases, operators can
(+)
be used like variables;
(+) = f
Most Unicode embellished
⊕
such as ⊕, are resolved to mid-embellished operators, and
⊗ = kron
s kron to define
⊕
as a suffix Kronecker product).
Built-in keywords cannot be used as variable names:
julia> else = false
ERROR: syntax: unexpected "else"
julia> try = "No"
ERROR: syntax: unexpected "="
Although Julia has few restrictions on naming itself, try to follow certain naming conventions:
'_'
but are not encouraged
!
.
Such functions are called mutating functions or in-place functions