Professional Documents
Culture Documents
MA214 lab 1 part 1
MA214 lab 1 part 1
1)
(MA214-5/7)
Dr Joe Bailey
jbailef@essex.ac.uk
Dr Andrew Harrison
harry@essex.ac.uk
Software installation
Python
• windows https://www.python.org/downloads/windows/
• macOS https://www.python.org/downloads/
Software installation
Python IDEs: Spyder, PyCharm,
Jupyter Notebook etc.
Software installation
R: https://www.r-project.org
Download: https://www.stats.bris.ac.uk/R/ or https://cran.ma.imperial.ac.uk
Software installation
R IDE:
Rstudio
https://www.rstudio.com/products/rstudio/download/
R and Python are installed but you don’t want to install so many IDEs
separately.
--- You can install Anaconda instead of searching and installing those
IDEs
Software installation Important note: If you use
Anaconda Anaconda, please make sure your
installed python match the required
Download and installation: https://www.anaconda.com/products/individual
version.
Software Installation
Get started with Python
In this module we will mainly use Jupyter Notebook for demonstration.
• Open Anaconda
• Click the “launch”
button under
“Jupyter
Notebook”.
Get started with Python
Build a file for coding.
Files go to the
folder you want to
use “New”
Python 3
See https://www.w3schools.com/python/python_datatypes.asp
Python basics: basic data types
Numbers:
Operators: +, -, *, / e.g., 3+4, 3-4, 3*4, 3/4
Division: //, % e.g., 17//3, 17%3
Power operator: ** e.g., 5**2, 3**7
Assign a value to a variable: e.g.,
Price=2.5
Number=22
Total_cost=Price*Number
Python basics: basic data types
Remark that one “cell” in Jupter notebook gives only one output, enable multiple outputs
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = 'all'
Python basics: basic data types
Strings
You can use both single quote ‘’ or double quote “”
• Nested list: e.g., a=['a', 'b', 'c’], x=[1,3,7], y=[a,x], y, y[0], y[1], y[0][1], y[1][2]
More?
list.append(x), list.remove(x), list.clear(), etc.
See
https://docs.python.org/3/tutorial/datastructures.html
Python basics: basic data types
Tuple
A tuple consists of a number of values separated by commas:
e.g., a=1,2,'MA214’, nested tuple: e.g., b=a,(3,4,5)
e.g., define a dictionary, print the dictionary, and print using the key
Python basics: basic data types
Set
A set of objects
e.g., basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
print(basket)
See https://www.w3schools.com/r/r_variables_global.asp
and https://www.tutorialspoint.com/r/r_data_types.htm
R basic: basic data types
Numbers:
Operators: +, -, *, / e.g., 3+4, 3-4, 3*4, 3/4
Power operator: ^ e.g., 5^2, 3^7
Assign a value to a variable: e.g.,
Price=2.5, more often we use Price<-2.5
R basic: basic data types
Character:
It is quite similar to strings in python
Quoted using single quote ‘’, or double quote “”
R basic: basic data types
Vectors:
Can have different modes such as numeric, character, complex or logical
a<-c(1,3,4,7,8)
b<-c(“apple”, “pear”, “banana”)
a[1]; a[5]
Note that:
• “a[-1]” is remove the first element from a vector
• Different from Python, the index begins from 1
instead of 0.
R basic: basic data types
Get the length of a vector: length(vector_name)
Using print() to print results: print()
while(condition){
statement
}
R basic: For, while loops and if statement
For Iterate over items
If (condition 1){
statement 1
} else if (condition 2) {
statement 2
} else{
statement 3
}
R basic: Function
Define a function:
Note that:
Myfun<-function(val1, val2, …, valn){ For multiple return, “a” should be a
write the function here list which lists the returned variables.
return (a)
}
R basic: Install and use a package
Install a package: Windows and Mac are the same
install.packages(“package_name”)
Use an installed package:
library(package_name)