一、浮点数
1、修改参数的函数结尾用!
function ff!(x) # 函数ff()会修改x的内容
x[end] = 4
end
x = [1,2,3]
println(ff!(x))
julia> x
3-element Array{Int64,1}:
1
2
4
2、查看变量的类型
julia> a = 2
2
julia> typeof(a)
Int64
julia> b = typeof(a)
Int64
julia> typeof(b)
DataType
julia> supertype(b) # b的父类型
Signed
3、浮点数的表示方法
julia> 1e-3
0.001
julia> typeof(ans)
Float64
julia> 1f-3
0.001f0
julia> typeof(ans)
Float32
4、精度
数值越大,精度越低,浮点数在0附近最稠密
julia> eps(Float32)
1.1920929f-7
julia> eps(Float64)
2.220446049250313e-16
julia> eps(1.0)
2.220446049250313e-16
julia> eps(1000.)
1.1368683772161603e-13
julia> 1.1+1.0
2.1
julia> 1.1+0.1
1.2000000000000002
二、复数
1、基本运算
julia> x = 1 + 2im
1 + 2im
julia> y = 2 + 3im
2 + 3im
julia> x + y
3 + 5im
julia> x - y
-1 - 1im
julia> x * y
-4 + 7im
julia> x / y
0.6153846153846154 + 0.07692307692307691im
julia> x ^ 2
-3 + 4im
julia> 2x # 复数和常数相乘
2 + 4im
julia> 2(1 + 2im)
2 + 4im
julia> 2 * (1+2im)
2 + 4im
2、运算优先级
julia> 2/5im == 2/(5*im)
true
3、复杂运算
julia> sqrt(x)
1.272019649514069 + 0.7861513777574233im
julia> cos(x)
2.0327230070196656 - 3.0518977991518im
julia> exp(x)
-1.1312043837568135 + 2.4717266720048188im
julia> abs(x)
2.23606797749979
julia> sqrt(-1)
ERROR: DomainError with -1.0:
sqrt will only return a complex result if called with a complex argument. Try sqrt(Complex(x)).
Stacktrace:
[1] throw_complex_domainerror(::Symbol, ::Float64) at .\math.jl:32
[2] sqrt at .\math.jl:492 [inlined]
[3] sqrt(::Int64) at .\math.jl:518
[4] top-level scope at none:0
julia> sqrt(-1 + 0im)
0.0 + 1.0im
4 、复数
julia> x = 1
1
julia> y = 1
1
julia> z = complex(x,y)
1 + 1im
julia> real(z)
1
julia> imag(z)
1
julia> angle(z)
0.7853981633974483
三、分数
julia> 2//3
2//3
julia> 4//8
1//2
julia> numerator(2//3) # 分子
2
julia> denominator(2//3) # 分母
3
julia> float(2//3)
0.6666666666666666
julia> isequal(float(1//2),1/2)
true
四、字符串
1、基本操作
julia> x = "a"
"a"
julia> Int(x)
ERROR: MethodError: no method matching Int64(::String)
Closest candidates are:
Int64(::Union{Bool, Int32, Int64, UInt32, UInt64, UInt8, Int128, Int16, Int8, UInt128, UInt16}) at boot.jl:710
Int64(::Ptr) at boot.jl:720
Int64(::Float32) at float.jl:706
...
Stacktrace:
[1] top-level scope at none:0
julia> x = 'a' # 单个字符
'a': ASCII/Unicode U+0061 (category Ll: Letter, lowercase)
julia> Int(x)
97
julia> Char(x)
'a': ASCII/Unicode U+0061 (category Ll: Letter, lowercase)
julia> str = "hello world" # 字符串
"hello world"
julia> str[1] # Julia的下标从1开始
'h': ASCII/Unicode U+0068 (category Ll: Letter, lowercase)
julia> str[0]
ERROR: BoundsError: attempt to access String
at index [0]
Stacktrace:
[1] checkbounds at .\strings\basic.jl:193 [inlined]
[2] codeunit at .\strings\string.jl:89 [inlined]
[3] getindex(::String, ::Int64) at .\strings\string.jl:210
[4] top-level scope at none:0
julia> str[end-3:end]
"orld"
2、遍历字符串
julia> for s in str
println(s)
end
h
e
l
l
o
w
o
r
l
d
3、其他操作
(1) 字符串拼接
julia> x = "hello"
"hello"
julia> y = "world"
"world"
julia> string(x,",",y,"\n")
"hello,world\n"
julia> x * ',' * y
"hello,world"
julia> x * "," * y
"hello,world"
julia> "abc" ^ 3
"abcabcabc"
julia> s = split("one two three")
3-element Array{SubString{String},1}:
"one"
"two"
"three"
julia> join(s)
"onetwothree"
julia> join(s, " ")
"one two three"
(2) 插值操作符,$取出变量的值
julia> n = 4
4
julia> "nnn has $n n"
"nnn has 4 n"
julia> "1 + 2 = $(1+2)"
"1 + 2 = 3"
julia> println("\$100") # 当作文字使用时需要转义
$100
(3) 其他操作
julia> lowercase("HELLO")
"hello"
julia> uppercase("hello")
"HELLO"
julia> replace("I want learn Python","Python" => "Julia")
"I want learn Julia"
julia> startswith("julia is interesting","julia")
true
(4) 数学操作
julia> "abcd" < "xyz"
true
julia> "abcd" != "abcde"
true
五、Tuple
用()表示,内容不可更改
julia> x1 = (1,2,3,4)
(1, 2, 3, 4)
julia> x1[1]
1
julia> length(x1)
4
julia> x1[2:end]
(2, 3, 4)
julia> x3 = (a=1,b=2) # 定义字典性质
(a = 1, b = 2)
julia> x3.a
1
六、字典
1、基本操作
julia> dict = Dict("aa"=>1,"bb"=>2,"cc"=>3)
Dict{String,Int64} with 3 entries:
"bb" => 2
"aa" => 1
"cc" => 3
julia> typeof(dict)
Dict{String,Int64}
julia> dict.count
3
julia> dict.keys # 默认显示16个键,只定义了3个
16-element Array{String,1}:
#undef
#undef
#undef
#undef
"bb"
#undef
#undef
#undef
#undef
#undef
#undef
#undef
"aa"
"cc"
#undef
#undef
julia> dict.vals
16-element Array{Int64,1}:
0
0
0
0
2
344243432
345220920
0
0
0
344240640
0
1
3
0
0
julia> dict["aa"]
1
2、类型转换
julia> arr = ["one"=>1, "two"=> 2]
2-element Array{Pair{String,Int64},1}:
"one" => 1
"two" => 2
julia> Dict(arr)
Dict{String,Int64} with 2 entries:
"two" => 2
"one" => 1
julia> tpl = [("one",1), ("two",2)]
2-element Array{Tuple{String,Int64},1}:
("one", 1)
("two", 2)
julia> Dict(tpl)
Dict{String,Int64} with 2 entries:
"two" => 2
"one" => 1
julia> vas = [1,2,3]
3-element Array{Int64,1}:
1
2
3
julia> kes = ["one", "two", "three"]
3-element Array{String,1}:
"one"
"two"
"three"
julia> Dict(zip(kes,vas))
Dict{String,Int64} with 3 entries:
"two" => 2
"one" => 1
"three" => 3
3、遍历字典
julia> d = Dict("one"=>1, "two"=>2, "three"=>3)
Dict{String,Int64} with 3 entries:
"two" => 2
"one" => 1
"three" => 3
julia> for (k,v) in d
println("value is $v for key $k")
end
value is 2 for key two
value is 1 for key one
value is 3 for key three
julia> for v in values(d)
println(v)
end
2
1
3
4、删除字典元素
julia> delete!(d,"one")
Dict{String,Int64} with 2 entries:
"two" => 2
"three" => 3
七、Set
1、定义
julia> s = Set([1,2,3,2]) # 集合无重复元素
Set([2, 3, 1])
julia> s = Set([1:3:10])
Set(StepRange{Int64,Int64}[1:3:10])
julia> s = Set(1:3:10)
Set([7, 4, 10, 1])
2、关键字in
julia> s = Set(1:3:10)
Set([7, 4, 10, 1])
julia> 2 in s
false
julia> 4 in s
true
3、集合运算
julia> A = Set([1,2,3,4])
Set([4, 2, 3, 1])
julia> B = Set([2,4,6,7])
Set([7, 4, 2, 6])
julia> intersect(A,B) # 交
Set([4, 2])
julia> union(A,B)
Set([7, 4, 2, 3, 6, 1]) # 并