-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjulia-tutorial.jl
150 lines (126 loc) · 6.44 KB
/
julia-tutorial.jl
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# Ladies and gentlemen, welcome to the Julia tutorial.
# This tutorial is based on the Julia tutorial by Derek Banas.
# The tutorial is available at https://youtu.be/sE67bP2PnOo?si=Q7JCCYQdpHfWHVP6
# Here I will resume the tutorial, and teache you how to use Julia.
# The tutorial is divided in the following sections:
# 1. Variables
# 2. Booleans
# Later I will explain what this means.
using Printf
using Statistics
# 1. Variables
# Variables in Julia are defined using the following syntax:
# variable_name = value
# The value can be of any type, and the type of the variable is inferred from the value.
# The type of the variable can be checked using the typeof() function.
# The value of the variable can be changed at any time.
# Look these examples below:
#%%
# Example 1 (variables):
variable = 0; ## Initial value of the variable is a nummber, that is 0.
variable = "Jesus loves you!" ## The value of the variable is changed to a string.
println(variable) ## Prints the value of the variable
#%%
#%%
# Example 2 (variables in functions):
function changeX() ## Function that changes the value of x
x = 10 # Before changing x: 10
println("Before changing x: ", x) ## Prints the value of x
x = 20 # After changing x: 20
println("After changing x: ", x) ## Prints the value of x
end
changeX() # Call the function
#%%
# 2. Booleans
# A boolean is a data type that represents one of two possible values: `true` or `false`.
# Booleans are used to express logical conditions in programming. They are named after the
# mathematician George Boole, who developed Boolean algebra, a branch of mathematics that
# deals with logic and truth values. In Julia, you can define a boolean variable using the
# syntax: `variable_name = true` or `variable_name = false`.
# The value of a boolean variable can be changed at any time during the execution of a program.
# Booleans are commonly used in conditional statements, such as `if` statements, to control
# the flow of execution based on whether a condition is true or false. They are also used
# in logical operations, such as `and`, `or`, and `not`, to combine or negate boolean values.
# Here's an example of using a boolean variable in Julia:
#%%
# Example 3 (booleans):
is_raining = true
if is_raining
println("Remember to take an umbrella!")
else
println("No need for an umbrella today.")
end
#%%
#In this example, the program checks the value of the `is_raining` boolean variable.
# If it is `true`, it prints the message "Remember to take an umbrella!".
# Otherwise, it prints "No need for an umbrella today.".
# Booleans in Julia are defined using the following syntax:
# variable_name = true or variable_name = false
# The value of the variable can be changed at any time, and can be used into functions.
# Look this example below:
#%%
# Example 4 (booleans in functions):
function isEven(x) ## Function that checks if a number is even
if x % 2 == 0
return true
else
return false
end
end
isEven(4) # Call the function with an argument
#%%
# 3. Number and characters types
# In Julia, numbers and characters can be represented using different types, such as integers, floating-point numbers,
# and complex numbers. The type of a number determines how it is stored in memory and how it can be used
# in calculations. Some common number types in Julia include:
# - Integers: Whole numbers without a decimal point, such as 1, 2, 3, -1, -2, -3.
# - Floating-point numbers: Numbers with a decimal point, such as 1.0, 2.5, 3.14, -1.0, -2.5, -3.14.
# - Complex numbers: Numbers with a real and imaginary part, such as 1 + 2im, 3.14 + 2.5im.
# The type of a number can be checked using the `typeof()` function in Julia. Here's an example of
# using different number types in Julia:
# In Julia, numbers are represented by various types. Here’s a brief overview:
# Integer Types: These are used to represent whole numbers. Julia provides signed
# and unsigned integers of different sizes. Here are some examples:
# Int8, Int16, Int32, Int64, Int128: These are signed integers, meaning they can be both positive and negative.
# The number after Int represents the number of bits that type uses.
# UInt8, UInt16, UInt32, UInt64, UInt128: These are unsigned integers, meaning they can only be positive.
# Floating-Point Types: These are used to represent real numbers (numbers with fractions).
# Julia provides two main types of floating-point numbers:
# Float32, Float64: These represent 32-bit and 64-bit floating-point numbers respectively.
# Complex and Rational Types: These are more specialized numeric types used for specific mathematical purposes:
# Complex: This is used to represent complex numbers, which have both a real and an imaginary component.
# Rational: This is used to represent rational numbers, which are represented as a fraction of two integers.
# Each type has its own range and precision, and the appropriate type to use depends on the specific requirements of your program.
# It’s also worth noting that Julia supports arbitrary precision arithmetic using the BigInt and BigFloat types.
# These types aren’t fixed-size and can be used when you need to perform computations that require a high degree of precision.
# Here’s an example of using different number types in Julia:
#%%
# Example 5 (character):
char_example = Char(120) ## The value of the variable is a character
println(char_example) ## Prints the value of the variable
#%%
#%%
# Example 6 (integers numbers):
int_example = UInt8(trunc(π)) ## The value of the variable π is an integer
println(int_example) ## Prints the value of the variable
#%%
#%%
# Example 7 (float numbers):
float_example = Float64(π) ## The value of the variable π is a float number
println(float_example) ## Prints the value of the variable
#%%
#%%
# Example 8 (number to parse):
number_to_parse = parse(Float64, "2518,577") ## The value of the variable is a float number
println(number_to_parse) ## Prints the value of the variable
#%%
# 4. Strings
# In Julia, strings are sequences of characters enclosed in double quotes (") or triple double quotes (""").
# Strings can contain letters, numbers, symbols, and whitespace characters. They are used to represent text
# data in a program. Strings can be manipulated using various string functions and operations in Julia.
# Here's an example of using strings in Julia:
#%%
# Example 8 (string):
string_example = "Hello, world,\n here's the 2518 vibes!" ## The value of the variable is a string
println(string_example) ## Prints the value of the variable
#%%