Basics of Programming
Objectives
Understand what programming is
Learn about variables and data types
Understand conditional statements (
if
statements)Explore arrays and collections
Understand loops and iteration
Learn about functions
Familiarize with basic operators
What is Programming?
Programming is the process of writing instructions for a computer to execute. These instructions are written in programming languages, which computers interpret and perform specific tasks. Programming languages allow us to create software, applications, and websites.
Programming involves tasks such as:
Creating algorithms (step-by-step instructions)
Managing data
Automating tasks
Variables
Variables are containers used to store data values. They have names, and values can change during program execution.
Example:
name = "Alice"
age = 30
print(name)
print(age)
name <- "Alice"
age <- 30
print(name)
print(age)
name = "Alice"
age = 30
println(name)
println(age)
Variable names:
Should be descriptive
Can include letters, numbers, and underscores (
_
)Cannot begin with a number
Data Types
Data types define the type of data stored in variables. Common data types include:
Integer: Whole numbers
age = 30
age <- 30
age = 30
Float: Numbers with decimal points
price = 9.99
price <- 9.99
price = 9.99
String: Text or characters
greeting = "Hello World"
greeting <- "Hello World"
greeting = "Hello World"
Boolean: True or False values
is_student = True
is_student <- TRUE
is_student = true
Operators
Operators are used to perform operations on variables and values.
Common types of operators:
Arithmetic Operators:
+
,-
,*
,/
,%
sum = 5 + 3
sum <- 5 + 3
sum = 5 + 3
Assignment Operators:
=
,+=
,-=
x = 10 x += 5
x <- 10 x <- x + 5
x = 10 x += 5
Comparison Operators:
==
,!=
,<
,>
print(10 > 5)
print(10 > 5)
println(10 > 5)
Logical Operators:
and
,or
,not
print(10 > 5 and 5 < 10)
print(10 > 5 & 5 < 10)
println(10 > 5 && 5 < 10)
Conditional Statements
Conditional statements perform different actions based on conditions. The if
statement is the most common:
age = 18
if age >= 18:
print("You are an adult.")
else:
print("You are not an adult.")
age <- 18
if (age >= 18) {
print("You are an adult.")
} else {
print("You are not an adult.")
}
age = 18
if age >= 18
println("You are an adult.")
else
println("You are not an adult.")
end
Arrays
Arrays (or lists in Python) store multiple values in a single variable:
colors = ["red", "green", "blue"]
print(colors[0]) # Output: red
colors <- c("red", "green", "blue")
print(colors[1]) # R is 1-indexed
colors = ["red", "green", "blue"]
println(colors[1]) # Julia is 1-indexed
Arrays are useful for:
Storing multiple related values
Iterating through values
Sorting and modifying collections
Loops
Loops are used to repeat actions multiple times.
For loop:
colors = ["red", "green", "blue"]
for color in colors:
print(color)
colors <- c("red", "green", "blue")
for (color in colors) {
print(color)
}
colors = ["red", "green", "blue"]
for color in colors
println(color)
end
While loop:
count = 0
while count < 3:
print(count)
count += 1
count <- 0
while (count < 3) {
print(count)
count <- count + 1
}
count = 0
while count < 3
println(count)
count += 1
end
Loops help in iterating over collections and automating repetitive tasks.
Functions
Functions are reusable blocks of code that perform specific tasks:
def greet(name):
print("Hello, " + name + "!")
greet("Alice")
greet <- function(name) {
print(paste("Hello,", name, "!"))
}
greet("Alice")
function greet(name)
println("Hello, " * name * "!")
end
greet("Alice")
Functions:
Improve readability
Promote code reuse
Simplify debugging
Exercise: Running code on the terminal
Open a terminal (in a computer where Python/R/Julia are installed) and try some of the commands above.
# in your terminal, start python by typing the command "python"
# it will look like this:
(base) enrico@computername:~$ python
Python 3.11.9 | packaged by conda-forge | (main, Apr 19 2024, 18:36:13) [GCC 12.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
Then run some code for example with
(base) enrico@computername:~$ python
Python 3.11.9 | packaged by conda-forge | (main, Apr 19 2024, 18:36:13) [GCC 12.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print("Hello world!")
Hello world!
>>>
# in your terminal, start R by typing the command "R"
# it will look like this:
(base) enrico@computername:~$ R
R version 4.3.3 (2024-02-29) -- "Angel Food Cake"
Copyright (C) 2024 The R Foundation for Statistical Computing
Platform: x86_64-conda-linux-gnu (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
Then run some code for example print("Hello world!")
:
> print("Hello world!")
[1] "Hello world!"
# in your terminal, start Julia by typing the command "julia"
# it will look like this:
(base) enrico@computername:~$ julia
_
_ _ _(_)_ | Documentation: https://docs.julialang.org
(_) | (_) (_) |
_ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
| | | | | | |/ _` | |
| | |_| | | | (_| | | Version 1.10.3 (2024-04-30)
_/ |\__'_|_|_|\__'_| | Official https://julialang.org/ release
|__/ |
julia>
Then run some code for example println("Hello world!")
:
julia> println("Hello world!")
Hello world!
Why is this approach unpractical? Consider the usability perspective but also the reproducibility point of view…
Learn More
Explore these resources for further learning:
Python:
R:
Julia:
Keypoints
Programming means instructing computers to perform tasks.
Variables store data, and each variable has a data type.
Conditional statements allow programs to make decisions.
Arrays and lists store collections of data.
Loops repeat actions efficiently.
Functions encapsulate reusable code.
Operators manipulate data.