Tipuri de date de bază

Introducere în Julia

James Fulton

Climate informatics researcher

Tipuri de date

Întregii sunt numere fără zecimale

# This is stored as an integer
x_int = 1

# Print the type of x_int println(typeof(x_int))
Int64

Numerele cu virgulă mobilă au zecimale

# This is stored as a float
x_float = 1.0

# Print the type of x_float println(typeof(x_float))
Float64
Introducere în Julia

Alte tipuri de date de bază

Booleeni

x_bool = true # or false

# Print the type of x_bool println(typeof(x_bool))
Bool

Șiruri de caractere

x_string = "Hello there"

# Print the type of x_string println(typeof(x_string))
String
  • Trebuie să folosiți "..."
  • Nu se poate folosi '...'
Introducere în Julia

Alte tipuri de date de bază

Caractere

x_char = 'A'

# Print the type of x_char println(typeof(x_char))
Char
  • Nu se poate folosi 'AB'
  • Doar 'A' sau 'B', etc.
Introducere în Julia

Consecințele tipurilor de date

# Bank balance as integer
balance = 100


# Interest rate as float interest_rate = 1.05
new_balance = balance * interest_rate
println(new_balance) println(typeof(new_balance))
105.0
Float64
# Bank balance as string
balance = "$100"

# Interest rate as string
interest_rate = "5%"

new_balance = balance * interest_rate


println(new_balance) println(typeof(new_balance))
$1005%
String
Introducere în Julia

Consecințele tipurilor de date

# Bank balance as integer
balance = 100

# Interest rate as string
interest_rate = "5%"

new_balance = balance * interest_rate
ERROR: MethodError: 
no method matching *(::Int64, ::String)
Introducere în Julia

Conversii între tipuri

# Convert the integer x to float y
x = 1

y = Float64(x)
println(typeof(y))
Float64
# Convert the float x to integer y
x = 1.0
y = Int64(x)


println(typeof(y))
Int64
Introducere în Julia

Conversii între tipuri

# Convert the float x to integer y

x = 1.01
y = Int64(x)
ERROR: InexactError: Int64(1.01)
# Convert the float x to integer y
x = 1.0
y = Int64(x)


println(typeof(y))
Int64
Introducere în Julia

Conversii între tipuri

# Convert the integer x to string y
x = 1
y = string(x) # y = "1"

println(typeof(y))
String
# Convert the string x to integer y
x = "1"
y = parse(Int64, x) 

println(typeof(y))
Int64
Introducere în Julia

Conversii între tipuri

# Convert the integer x to string y
x = 1
y = string(x) # y = "1"

println(typeof(y))
String
# Convert the string x to float y
x = "1.0"

y = parse(Float64, x)
println(typeof(y))
Float64
Introducere în Julia

Rezumat al conversiilor

  • Folosiți typeof(x) pentru a afla tipul lui x
  • Conversii între tipuri de date:
De la La Funcție
Integer Float Float64(x)
Float Integer Int64(x)
Integer sau Float String string(x)
String Float parse(Float64, x)
String Integer parse(Int64, x)
Introducere în Julia

Să exersăm!

Introducere în Julia

Preparing Video For Download...