티스토리 뷰

1_Getting_started(한글)

어떻게 출력하나?

println() 을 사용한다.

In [1]:
println("안녕, 줄리아!")
안녕, 줄리아!

어떻게 변수를 할당하나?

In [2]:
a = 1
println(a)
1
In [3]:
typeof(a)
Out[3]:
Int64
In [4]:
b = 1.1
println(b)
typeof(b)
1.1
Out[4]:
Float64
In [5]:
😺 = "smiley cat!"
println(😺)
typeof(😺)
smiley cat!
Out[5]:
String

smiley cat을 입력하기 위해서는, tab 완성기능을 사용해서 이모지 이름을 선택하고, 탭을 한 번 더 누르면 된다.

In [6]:
# \:smi + <tab> --> 화살표 아래버튼을 눌러서 선택 + <enter> ---> <tab> + <enter> 눌러서 완성.

변수에 값을 할당하고 나서, 아무런 문제 없이 다른 값을 또 할당할 수 있다. (덮어쓰기)

In [7]:
😺 = 1
println(😺)
typeof(😺)
1
Out[7]:
Int64

이 방법은 아래와 같은 코드를 작성할 수 있도록 해준다.

In [8]:
😄 = 0
😠 = -1
😄 - 😺 == 😠
Out[8]:
true

코멘트를 어떻게 다나?

In [9]:
# 한 줄의 코멘트를 달 때는, # 키 다음에 내용을 입력하면 된다.
In [10]:
#=

여러 줄의 코멘트를 달고 싶으면,
'#= =#' 안에 내용을 넣는다.

=#

기초적인 산수들

In [11]:
my_sum = 3 + 7
Out[11]:
10
In [12]:
my_difference = 10 - 3
Out[12]:
7
In [13]:
my_product = 20 * 5
Out[13]:
100
In [14]:
my_quotient = 100 / 10
Out[14]:
10.0
In [15]:
my_power = 10 ^ 2
Out[15]:
100
In [16]:
my_modulus = 101 % 2
Out[16]:
1

연습문제

1.1

convert 함수의 설명을 보자.

In [17]:
?convert
search: convert code_native @code_native

Out[17]:
convert(T, x)

Convert x to a value of type T.

If T is an Integer type, an InexactError will be raised if x is not representable by T, for example if x is not integer-valued, or is outside the range supported by T.

Examples

jldoctest
julia> convert(Int, 3.0)
3

julia> convert(Int, 3.5)
ERROR: InexactError: Int64(Int64, 3.5)
Stacktrace:
[...]

If T is a AbstractFloat or Rational type, then it will return the closest value to x representable by T.

jldoctest
julia> x = 1/3
0.3333333333333333

julia> convert(Float32, x)
0.33333334f0

julia> convert(Rational{Int32}, x)
1//3

julia> convert(Rational{Int64}, x)
6004799503160661//18014398509481984

If T is a collection type and x a collection, the result of convert(T, x) may alias all or part of x.

jldoctest
julia> x = Int[1, 2, 3];

julia> y = convert(Vector{Int}, x);

julia> y === x
true

1.2

days 라는 변수에 365를 할당하자. 그리고 days를 float로 바꾸자.

In [18]:
days = 356
Out[18]:
356
In [19]:
days_copy = convert(Float64,days)
Out[19]:
356.0
In [20]:
println(days)
typeof(days)
356
Out[20]:
Int64
In [21]:
println(days_copy)
typeof(days_copy)
356.0
Out[21]:
Float64

1.3

아래 코드를 실행할 때 어떤 일이 일어나는지 보자.

convert(Int64, "1")

그리고

parse(Int64, "1")
In [22]:
convert(Int64, "1")
MethodError: Cannot `convert` an object of type String to an object of type Int64
Closest candidates are:
  convert(::Type{T<:Number}, !Matched::T<:Number) where T<:Number at number.jl:6
  convert(::Type{T<:Number}, !Matched::Number) where T<:Number at number.jl:7
  convert(::Type{T<:Integer}, !Matched::Ptr) where T<:Integer at pointer.jl:23
  ...

Stacktrace:
 [1] top-level scope at In[22]:1
In [23]:
parse(Int64, "1")
Out[23]:
1
In [24]:
typeof(parse(Int64, "1"))
Out[24]:
Int64
In [25]:
?parse
search: parse tryparse partialsortperm partialsortperm! pairs skipchars

Out[25]:
parse(type, str; base)

Parse a string as a number. For Integer types, a base can be specified (the default is 10). For floating-point types, the string is parsed as a decimal floating-point number. Complex types are parsed from decimal strings of the form "R±Iim" as a Complex(R,I) of the requested type; "i" or "j" can also be used instead of "im", and "R" or "Iim" are also permitted. If the string does not contain a valid number, an error is raised.

Examples

jldoctest
julia> parse(Int, "1234")
1234

julia> parse(Int, "1234", base = 5)
194

julia> parse(Int, "afc", base = 16)
2812

julia> parse(Float64, "1.2e-3")
0.0012

julia> parse(Complex{Float64}, "3.2e-1 + 4.5im")
0.32 + 4.5im

'Flux in Julia > Learning Julia (Intro_to_Julia)' 카테고리의 다른 글

03. Data structures (한글)  (0) 2018.09.13
03. Data structures  (0) 2018.09.13
02. Strings (한글)  (0) 2018.09.12
02. Strings  (0) 2018.09.12
01. Getting started  (0) 2018.09.11
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
글 보관함