Download as pdf or txt
Download as pdf or txt
You are on page 1of 56

IMREDD - UCA 2rd December 2023

Python and R .
Infrastructure
The course does not use any university-specific resources.
It is designed to run entirely on students’ machines using solely open-source software.
Two coding languages will be used during this course.

R is a programming language for statistical


Python is a high-level, general-purpose
computing and graphics supported by the R
programming language. Its design philosophy
Core Team and the R Foundation for
Statistical Computing. Created by emphasizes code readability with the use of
statisticians Ross Ihaka and Robert significant indentation. It is often described as a
Gentleman, R is used among data miners, "batteries included" language due to its
and statisticians for data analysis and comprehensive standard library. Python
developing statistical software consistently ranks as one of the most popular
programming languages.
What they
look like:
Key
Differences vs.

R is a programming language Python is a general-purpose


1. Goals created to provide an easy way language designed to be elegant
to analyze data and create and simple. Therefore, it is
visualizations. Its use is mainly widely used in Artificial
limited to statistics, data Intelligence and Web
science, and machine learning. Development fields.

Python is used widely by different


R is designed to be used by
2. Users statisticians. As a result, it offers
developers of all experience levels.
Because of its versatility, it is used by
a simple interface with functions
different professions, such as
you would need for statistics.
software engineers, analysts, and
scientists.
Key
vs.
Differences
R is made to be used for Data One of Python’s most popular
3. Data Science. As a result, R has applications is data science –
Analysis and functions for data analysis and
plotting graphs out of the box. In
which involves analyzing and
visualizing data. However, Python
Visualisation addition to the built-in functions,
there are packages that you can
does not support this application
natively and instead relies on
use for further analysis. libraries and packages

Python also provides high-level


R’s simplicity and abstractions
abstractions and simplifications,
come at the cost of performance. It
making it more flexible but slower.
is not very fast. In programming,
4. Performance being slow means running a few
To improve the performance of
Python programs, libraries such as
milliseconds slower than in other
NumPy and PyTorch use C++ or C.
languages. Therefore R’s slowness
This enables Python to run faster.
might be barely noticeable to users.
Key
vs.
Differences
In addition to visualization and Python is the most popular language
5. Usage in analysis, R can be used to build used in Machine Learning. It has a
machine learning models using rich library ecosystem, such as
Machine regression, classification, and Tensorflow, PyTorch, and Jax. It is
clustering algorithms. It has also the most widely supported by
Learning packages such as Caret, lattice, Machine Learning Infrastructure
DataExplorer, and Dalex. platforms.

Python is one of the most


6. Learning R was designed to be simple.
However, its syntax can take beginner-friendly programming
Curve some time to get used to and is
slightly more difficult than
languages designed to be
elegant and simple. It has a
Python, in my experience. gentler learning curve compared
to R.
Key
vs.
Differences
R has a large community – albeit Python probably has the largest
7.Community smaller than Python’s. It is still
significant as it has more than 10,000
community of any programming language
in the world. It has open-source packages
packages for you to use. It also has for almost anything, including machine
many educational resources to learn learning and data handling. There are many
from as the language is still incredibly free and paid educational resources to
popular. learn Python and its many tools created by
the community..

8. Popularity R is definitely not as popular as


Python, but it is still popular
Python is one of the most popular
programming languages in the world
enough. It is used by big tech (rivaled only by JavaScript and Java). It is
companies, and a significant also one of the most preferred languages
number of job openings require because of its simplicity and versatility.
expertise in R.
Key
vs.
Differences

9.Flexibility/ R is primarily meant for statistical On the other hand, Python is


versatile. It is widely used for
Versatility computations. Therefore it is not
used for anything beyond this use different purposes, and data
case. science is just one of the many.

Python is distributed solely as an


10.IDEs The primary IDE used for R is R
Studio which is available for
interpreter. The IDE is up to the
user. The two most popular IDEs
MacOS, Windows, and Linux. are VsCode and PyCharm.In
addition, you can also use Jupyter
Notebooks to run your Python
interactively in cells.
Key
vs.
Differences

While Python, a general-purpose


11. Common R, a statistical language, is
commonly used for Machine
language, has a wide range of
uses, including Machine
Applications Learning, Statistics and Learning, Web development,
Analytics, and Data visualization. Data analysis, and Numerical
computation.

Both R and
Python have big
tech users.
Introduction to Python .
Fields of application

Programming Language Web programming Scientific calculus


(exploitation system)
General features

-MODULARITY -> a high number of libraries/modules

-MULTIPLATFORM

-POWERFUL -> from small scripts to high-complexity projects


Informatic Features of Python

-INTERPRETED

-READABILITY
Informatic Features of Python

Imperative language
-> writing a sequence of instructions

Object-oriented programming language

Higher-order language

Dynamically typed language


Python installation, IDLE and colab

Installing Python : Command prompt lines:


Under Linux
Python is installed by default

Under Windows
Python is not installed by default. Go to the following
address

Look for the download links and choose the one best
suited to your machine
(32-bit or 64-bit)
● 32-bit : Python 3.x.x Windows Installer
● 64-bit : Python 3.x.x Windows X86-64 Installer

Another option: install the "Scientific Python" distribution


Python(x,y), which is
downloadable from the following address
Python installation, IDLE and colab
Python installation, IDLES and colab

https://jupyter.org/
1. Files: Here you will be able to upload datasets and other files from both your computer and Google Drive
2. Code Snippets: Here you will be able to find prewritten snippets of code for different functionalities like
adding new libraries or referencing one cell from another.
3. Run Cell: This is the run button. Clicking this will run any code that is inserted in the cell beside it. You can
use the shortcut shift+enter to run the current cell and exit to a new one.
4. Table of Contents: Here you will be able to create and traverse different sections inside of your notebook.
Sections allow you to organize your code and improve readability.
5. Menu Bar: Like in any other application, this menu bar can be used to manipulate the entire file or add new
files. Look over the different tabs and familiarize yourself with the different options. In particular, make sure
you know how to upload or open a notebook and download the notebook (all of these options are under
“File”).
6. File Name: This is the name of your file. You can click on it to change the name. Do not edit the extension
(.ipynb) while editing the file name as this might make your file unopenable.
7. Insert Code Cell: This button will add a code cell below the cell you currently have selected.
8. Insert Text Cell: This button will add a text cell below the cell you currently have selected.
9. Cell: This is the cell. This is where you can write your code or add text depending on the type of cell it is.
10. Output: This is the output of your code, including any errors, will be shown.
11. Clear Output: This button will remove the output.
1. Ram and Disk: All of the code you write will run on Google’s computer, and you will only see the output.
This means that even if you have a slow computer, running big chunks of code will not be an issue. Google
only allots a certain amount of Ram and Disk space for each user, so be mindful of that as you work on
larger projects.
2. Link to Cell: This button will create a URL that will link to the cell you have selected.
3. Comment: This button will allow you to create a comment on the selected cell. Note that this will be a
comment on (about) the cell and not a comment in the cell.
4. Settings: This button will allow you to change the Theme of the notebook, font type, and size, indentation
width, etc.
5. Delete Cell: This button will delete the selected cell.
6. More Options: Contains options to cut and copy a cell as well as the option to add form and hide code.
Learning by doing:
now open a new COLAB page and
let’s start typing python code!
Object, Variable and Type

Object
Object, Variable and Type
Memory

Object Object
Object type: character string

Attributes:
Value: “bonjour”

Methods:
-upper()
-…
Object, Variable and Type
Memory

Variable Variable Object


Object type: character string

Attributes:
Value: “bonjour”

Methods:
-upper()
a
-…
Object, Variable and Type
Memory

Variable Variable Object


Object type: character string

Attributes:
Value: “bonjour”

Methods:
-upper()
a
-…

Object type: integer

Attributes:
Value: 1
Methods: ..
Object, Variable and Type
Memory

Variable Variable Object


Object type: character string

Attributes:
Value: “bonjour”

Methods:
-upper()
a
-…

b Object type: integer

Attributes:
Value: 1
Methods: ..
Object, Variable and Type

Variable names
-> Any sequence of alphanumeric characters
- a,b,...z
- A,B,...Z
- 1,2,...9,0
- _
-> Must begin with a letter or the underscore
-> Some names are reserved
-> Case-sensitive
-> Suggestion: choose explicit variable names
Object, Variable and Type
Memory

Variable Types Variable Object


Object type: …
-> Simple types
a Attributes:
- Boolean: bool
Value:
- Integer: int Methods:
- Floating: float
- Complex: complex
- Character string: str
>>> type()

-> Composite types


containers for simple types
Object, Variable and Type

Variable Type: bool

-> Operations with booleans

-> Comparisons
Object, Variable and Type

Variable Type: int

-> Operations with integers


Object, Variable and Type

Variable Type: float

-> Operations with floating


Object math
Attributes:
Value:
Methods:
floor()
ceil()
Object, Variable and Type

Variable Type: complex

-> Complex numbers are


constructed from integers or floats
Object, Variable and Type

Variable Type: str


-> Operations with strings
Object, Variable and Type
Memory
Variable Type: str Variable Object

-> Methods Object type: …

a Attributes:
Value:
Methods:
Object, Variable and Type

Composite types Variable Object

Object type: int


-> Simple types
- Boolean: bool
- Integer: int Object type: int
- Floating: float a Composite
type
- Complex: complex
- Character string: str Object type: int

-> Composite types


containers for simple types
Object, Variable and Type

Collection of elements Variable Object

Object type: int


-> Ordered Collections
- List
- Tuples (not modifiables) Object type: int
- Chains of characters a Composite
type

-> Non-ordered collections Object type: int

- Set (different elements)


- Tables/dictionaries
Object, Variable and Type

List
-> Operations
Object, Variable and Type

String character => List

->A string character is a list!


Object, Variable and Type

Tuple
->Not modifiables collections
Object, Variable and Type

Set
->Not modifiables collections
Object, Variable and Type

Table/Dictionary
->Non-ordered collection of couples (key,
value)
Object, Variable and Type

Dataframes

and geodataframes
Python: a programming language

So far:
-> Object types
-> Operators
-> variable referencing
-> Intrinsic functions
Conditional Test
Syntax
Conditional Test
Syntax

-break
-continue
Conditional Test
Syntax
Functions
myprogram.py
Variable Object
instruction 1
instruction 2 Object type:
… function
myfunction
definition of myfunction()

instruction 4: myfunction()
instruction 5
….
Functions
Syntax

Error
Functions
Syntax
If many return values
Functions
Syntax
If many input values
Functions
Nested Functions main namespace

main code block f()

code block of f namespace of f

def f(a): a, g
code block of g namespace of g
g(b):
b
print(b)

g(a)
print
(locals())

f(5)
Functions
Nested Functions
Libraries

Python has a limited number Syntax


of basic commands

myprogram.py module.py

Add functions as import module variable1 =...


variable2 = …
modules (import) functionF(5)
functionF():
…..
functionG():
….
Libraries

import math main namespace

math
sqrt

namespace of math

pi
sqrt
Libraries

from math import fabs main namespace

fabs

namespace of math
Libraries

Personalized modules
Importing/Reading Files
Object file
-Open->processing-> close
-Methods: read or write
-File position: name,acces
Syntax:

You might also like