17 KiB
17 KiB
In [ ]:
t = (33, 4.5, "Hello")
@show t[2] # indizierbar
for i ∈ t println(i) end # iterierbarIn [ ]:
typeof(t)In [ ]:
# Ganzzahldivision und Rest:
# Quotient und Rest werden den Variablen q und r zugewiesen
q, r = divrem(71, 6)
@show q r;In [ ]:
x, y, z = 12, 17, 203In [ ]:
yIn [ ]:
x = (13,) # ein 1-Element-TupelIn [ ]:
x= (13) # kein TupelIn [ ]:
r = 1:1000
typeof(r)In [ ]:
(3:100)[20] # das zwanzigste ElementIn [ ]:
@allocated [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]In [ ]:
@allocated 1:20In [ ]:
collect(20:-3:1)In [ ]:
LinRange(2, 50, 300)In [ ]:
# Einwohner 2020 in Millionen, Quelle: wikipedia
EW = Dict("Berlin" => 3.66, "Hamburg" => 1.85,
"München" => 1.49, "Köln" => 1.08)In [ ]:
typeof(EW)In [ ]:
EW["Berlin"]In [ ]:
EW["Leipzig"]In [ ]:
haskey(EW, "Leipzig")In [ ]:
@show get(EW, "Leipzig", -1) get(EW, "Berlin", -1);In [ ]:
keys(EW)In [ ]:
values(EW)In [ ]:
for i in keys(EW)
n = EW[i]
println("Die Stadt $i hat $n Millionen Einwohner.")
endIn [ ]:
for (stadt, ew) ∈ EW
println("$stadt : $ew Mill.")
endIn [ ]:
EW["Leipzig"] = 0.52
EW["Dresden"] = 0.52
EWIn [ ]:
# Oh, das war bei Leipzig die Zahl von 2010, nicht 2020
EW["Leipzig"] = 0.597
EWIn [ ]:
delete!(EW, "Dresden")In [ ]:
maximum(values(EW))In [ ]:
d1 = Dict()In [ ]:
d2 = Dict{String, Int}()In [ ]:
collect(EW)In [ ]:
collect(keys(EW)), collect(values(EW))In [ ]:
for k in sort(collect(keys(EW)), rev = true)
n = EW[k]
println("$k hat $n Millionen Einw. ")
endIn [ ]:
for (k,v) in sort(collect(EW), by = pair -> last(pair), rev=false)
println("$k hat $v Mill. EW")
endIn [ ]:
l = rand(1:6, 100_000) .+ rand(1:6, 100_000)In [ ]:
# In diesem Fall könnte man das auch mit einem einfachen Vektor
# lösen. Eine bessere Illustration wäre z.B. Worthäufigkeit in
# einem Text. Dann ist i keine ganze Zahl sondern ein Wort=String
d = Dict{Int,Int}() # das Dict zum 'reinzählen'
for i in l # für jedes i wird d[i] erhöht.
d[i] = get(d, i, 0) + 1
end
dIn [ ]:
using Plots
plot(collect(keys(d)), collect(values(d)), seriestype=:scatter)