Python Intro-1
Python Intro-1
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.
Both R and
Python have big
tech users.
Introduction to Python .
Fields of application
-MULTIPLATFORM
-INTERPRETED
-READABILITY
Informatic Features of Python
Imperative language
-> writing a sequence of instructions
Higher-order language
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
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
Attributes:
Value: “bonjour”
Methods:
-upper()
a
-…
Object, Variable and Type
Memory
Attributes:
Value: “bonjour”
Methods:
-upper()
a
-…
Attributes:
Value: 1
Methods: ..
Object, Variable and Type
Memory
Attributes:
Value: “bonjour”
Methods:
-upper()
a
-…
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
-> Comparisons
Object, Variable and Type
a Attributes:
Value:
Methods:
Object, Variable and Type
List
-> Operations
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
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
myprogram.py module.py
math
sqrt
namespace of math
pi
sqrt
Libraries
fabs
namespace of math
Libraries
Personalized modules
Importing/Reading Files
Object file
-Open->processing-> close
-Methods: read or write
-File position: name,acces
Syntax: