count: false class: center, middle # Beginning with Python ## Jupyter notebooks, Kernels ## code and Markdown Cells ## expressions with Integers, Strings and Booleans *Gérard Rozsavolgyi*
April 2019 --- # Introduction ## What we have seen so far In our introduction, we have seen some examples of what can be made with Python, the vast variety of it's applications: - General purpose Programming - Accessing Databases - Big Data - Text analysis - Machine Learning. - Hybrid applications - Web applications with great frameworks. Jupyter notebooks are widely used in Machine Learning and Google has a products called *CoLab* or *DataLab* in it's Cloud ecosystem that are customised Jupyter Notebook, with versionning (git) and with a better integration to other Google cloud tools (Storage, Machine Learning APIs) --- # Jupyter notebook ## Local install or online services We've also seen how to install your first Python Programming environment which we will shortly be using and shown some services where you can create an account an work for free online with Python notebooks such as: - [microsoft azure notebooks](https://notebooks.azure.com/) - [CoCalC](https://cocalc.com/) - [Google Colab](https://colab.research.google.com) The advantage of the online version is that it's possible to work in collaboration on the same document. Now, let's start programming and using Python ! --- ## Launching the notebook We can start *ipython-notebook* or *jupyter-notebook* (jupyter is the new name) from the launcher or from a terminal. As a programmer, you will have to become a little familiar with the terminal and the command line. To launch your notebook: * Under windows, you can find the Jupyter notebook icon in your start menu * Under Mac OS or Linux, just open a terminal window and type .small[ ```terminal jupyter notebook ``` ] You can also use the same method under windows, that is open a terminal (cmd) and type the same command and press 'ENTER'. --- # Jupyter notebook ## It's in your browser In any case, your default browser should appear, visiting URL [http://localhost:8888/tree](http://localhost:8888/tree) You will see the whole content of the working directory in your browser , that's why it's preferable to create our own working directory before launching ipython notebook so we will just see the files we are working with. It's not very important at this point and we will come back to this later. Just keep in mind that the notebook we'll create will be located into your home directory if you launch ipython from there. If you use Windows and launched it via the *Start* menu, you can customise the starting folder as explained in [documentation](http://jupyter-notebook-beginner-guide.readthedocs.io/en/latest/execute.html) . --- # Our first notebook In your browser, open the *New* select on the right: ![step 1](images/createNB.png) --- # Our first notebook Choose Python 3. You'll get: ![step 2](images/firstNB.png) --- # Structure of a notebook - A Notebook is essentialy composed of *Cells* - In each cell, we can type some Python instructions or delcarations - We have some menus and a toolbar like an ordinary application on the top of the page - Let's change the title's. Just click on it and type the new title. --- # Notebook Cells Let's type an expression in the first cell: ```python 1 + 1 ``` Run the cell by clicking the corresponding button. This will run the code you typed in the cell and open a new cell under to type new things. --- # Expressions We can type some simple Python expressions in the cells of our notebook: ```python 2 + 3 # addition ``` ```python 5 * 2 # mutliplication ``` ```python 5 / 2 # division ``` ```python 5 // 2 # whole division ``` If we put some quotes, it's not a number anymore ... ```python '5' # Not a number ``` It's a *String* --- # Logical expressions We can use logical assumptions with a binary result : *True* or *False*: ```python 5 >= 2 # Test => True ``` ```python 5 == 4+1 # Other Test => True ``` ```python '5' == 5 # A String is not a Number => False ``` ```python True and False # logical operation => False ``` ```python True or False # => True ``` --- # Notebooks Cells ## Running a cell code There are different types of cells, the most importants are: - *Markdown cells* - *Code cells* Markdown cells are made to type in some texts or insert links, images, videos, etc. Code cells are made for running Python *programms* or *snippets* (small pieces of code)
.label[run a cell]
For any kind of the cell, run it by clicking the corresponding button or with the keyboard shortcut *SHIFT ENTER* Using this shortcut will run the code you typed in the cell and create an empty cell below.
--- # String expressions ```python 'Hello' # String ``` ```python 'Hello'+'World' # String concatenation (without space) ``` With double quotes: ```python "This is also a Python string" ``` ```python 'Hello'+'World' # String concatenation (with middle space) ``` --- Notebook of this part: [Variables and Types](./notebooks/variables_and_types.ipynb) Quizzes about this part: - [Jupyter Notebooks](./quizzes/jupyter.html) - [Python Expressions](./quizzes/expressions.html) --- count: false class: center, middle # Beginning with Python ## Variables ## Types ## Environment ## Simple use of a Python module *Gérard Rozsavolgyi*
April 2019 --- # Variables declarations Let's type our first Python declaration: ```python a = 2 a ``` Now just write: ```python print(a) ``` As expected, it will print the value of *a* ... --- # Variables ## code cells Let's change the value stored in *a* and print it: ```python a = 5 print(a) ``` As expected, it will print the new value of a. Using the *print* function is the standard way of showing the content of a variable. We have just *reassigned* the value of a from the previous value of 2 to the new value 5 and printed it.
A variable is a
designed to store some data and name it ! We can change it's content at any time: it's called to
--- # Working with variables Now, we can define other variables by the same way: ```python b = 3 print(b) ``` and print *b* in the same cell Now, let's define c as the product of a and b and show the resulting value: ```python c = b*c print(c) ``` --- # Environment ## All variables defined In a Jupyter notebook ```python who ``` You will get the list of all the variables we have just defined: a,b and c. This constitutes the *environment* of the program (or notebook here)
The environment of a program is the list of all variables defined in the program with their corresponding values
--- # Basic Python types ## integers Try: ```python type(a) ``` You will get the answer: int the same for, b and c: ```python type(b) ``` ```python type(c) ``` This means that a, b and c are *integers* Integers are not the only types you can use in Python ... --- # Basic Python types ## floats Type: ```python type(2/3) ``` You will get the answer: float Floats are used to store decimal numbers but calculus on float are not always very accurate: ```python 0.1+0.1+0.1-0.3 ``` --- # Strings Let's define a python string ```python first_name = 'Sheldon' ``` It's better to choose significant variables names that everybody can understand ! Lets'print this variable and show it's type: ```python print(first_name) type(first_name) ``` --- # basic string operations We can get the string's length: ```python print(len(first_name)) ``` There is no specific type for chars in Python as there is in other languages as C or Java: ```python car = 'a' print(type(car)) print(len(car)) ``` --- # putting strings together Let's define a last name: ```python last_name = 'Cooper' ``` and now we want to put this two strings together: ```python name = first_name + last_name print(name) ``` show the string's length: ```python len(name) ``` --- # Simple use of a Python module As java, a Python program can import additional ressources from other libs For example, let's use the *sys* lib and use it to show current platform. ```python import sys print(sys.version) ``` and Python version with some details: ```python import sys print(sys.platform) ``` We can also use os lib to show the current directory: ```python import os os.getcwd() ``` and its contents: ```python import os os.listdir('.') ``` *getcwd()* means get current working directory. It's a *function* *listdir()* is also a function with one parameter : *'.'* (a shorthand for current dir) --- Notebook of this part: [Python Expressions](./notebooks/Simple_expressions.ipynb) Quizzes of the second part: - [Variables](./quizzes/variables.html) - [Environment and modules](./quizzes/env-mods.html) - FIXME Add references