print and println@printftry-catch for Exception HandlingfinallyThe core of programming involves working with information. Variables are the fundamental way we give names to and manage this information within our Julia programs. Think of a variable as a labeled container or a named placeholder in your computer's memory where you can store a piece of data. This data can be a number, some text, or other types of information that your program needs to remember and use.
In Julia, you create a variable and give it a value using the assignment operator, which is the equals sign (=). The basic structure is:
variable_name = value
This statement tells Julia: "Take the value on the right side and store it in a memory location associated with the variable_name on the left side." It's important to understand that = in programming is an assignment, not a statement of mathematical equality. It means "gets set to".
For example, to store a person's age:
age = 25
Here, age is the variable name, and 25 is the value assigned to it. Now, whenever your program needs to use this age, it can refer to it by the name age.
You can also assign the result of an expression to a variable:
width = 10
height = 5
area = width * height
In this case, Julia first calculates width * height (which is 10 * 5 = 50), and then assigns the result, 50, to the variable area.
Choosing good names for your variables makes your code much easier to read and understand. Julia offers a lot of flexibility in naming, but there are some rules and conventions to follow:
Rules:
_).!), and many other Unicode characters (yes, you can use symbols like π or α as variable names!).myValue, myvalue, and MYVALUE would be three distinct variables.Conventions (Good Practices):
customer_name is better than cn or x.snake_case (e.g., user_id, total_amount) or lowercase words are common in the Julia community. camelCase (e.g., userId, totalAmount) is also acceptable. The most important thing is to be consistent within your project.if, else, function, true, false) as variable names. If you try, Julia will give you an error.δ = 0.1), ensure they are easy to type and readable for anyone who might work with your code. You can type many math symbols in the Julia REPL using LaTeX-like abbreviations followed by a tab (e.g., \delta+Tab becomes δ).Here are some examples of valid variable names:
nameuser_agetemperature_celsius_temporary_valueisUserActiveπ_value (using the Greek letter pi)And some invalid or poor ones:
1st_value (cannot start with a digit)my-value (hyphens are not allowed; use my_value instead)for (this is a keyword)Variables can hold various kinds of data. For now, let's look at a few common ones you've seen briefly:
Integers (whole numbers):
count = 10
year = 2024
Floating-point numbers (numbers with a decimal point):
price = 19.99
pi_approx = 3.14159
Strings (sequences of text characters): Strings are enclosed in double quotes (").
greeting = "Hello, Julia learners!"
user_name = "Alice"
Booleans (logical values true or false):
is_ready = true
has_permission = false
The following diagram illustrates the idea of a variable as a named label pointing to a stored value:
A variable named
user_nameis assigned the string value "Alice".
Once a variable has a value assigned to it, you can use it in your code. For instance, you can print its value, use it in calculations, or pass it to functions.
Let's try this in the Julia REPL (Read-Eval-Print Loop):
julia> item_name = "Laptop"
"Laptop"
julia> quantity = 2
2
julia> unit_price = 750.00
750.0
julia> total_cost = quantity * unit_price
1500.0
julia> println(item_name)
Laptop
julia> println("Total cost for items:", total_cost)
Total cost for items: 1500.0
In this interactive session:
item_name.2 to quantity and 750.00 to unit_price.quantity * unit_price and assigned the result to total_cost.println to display the value of item_name and a message including the total_cost.The term "variable" implies that its value can vary or change. You can assign a new value to an existing variable at any point in your program.
julia> score = 100
100
julia> println("Initial score:", score)
Initial score: 100
julia> score = score + 50 # Increase the score
150
julia> println("Updated score:", score)
Updated score: 150
julia> score = 0 # Reset the score
0
julia> println("Final score:", score)
Final score: 0
Each time score is assigned a new value, the old value is replaced. This ability to update variables is fundamental to how programs manage changing states and information.
Julia provides a concise way to assign multiple variables at once, or to assign the same value to multiple variables.
To assign different values to different variables in one line:
julia> x, y, z = 10, 20, "hello"
("hello", 20, 10)
julia> println(x)
10
julia> println(y)
20
julia> println(z)
hello
This is equivalent to:
x = 10
y = 20
z = "hello"
The REPL shows ("hello", 20, 10) as the result of the assignment expression itself; this tuple reflects the values assigned but not necessarily in the order of variables x, y, z in all Julia versions or contexts, so focus on the individual println outputs for clarity on what each variable holds.
To assign the same value to multiple variables:
julia> a = b = c = 100
100
julia> println(a)
100
julia> println(b)
100
julia> println(c)
100
Here, c is first assigned 100. Then, b is assigned the value of c (which is 100), and finally a is assigned the value of b (also 100).
Understanding variables and how to assign data to them is your first step towards writing programs that can process information. As we move forward, you'll see how these named pieces of data are the building blocks for more complex operations and structures in Julia. Next, we'll take a closer look at the different types of data that these variables can hold.
Was this section helpful?
© 2026 ApX Machine LearningEngineered with