Skip to content
Snippets Groups Projects
Commit ba1d3be3 authored by Mihai Lefter's avatar Mihai Lefter
Browse files

Added hands on exercises for the first introduction lecture.

parent 3a2d0891
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id: tags:
<span style="font-size: 200%">Introduction to Python (1)</span>
===
%% Cell type:markdown id: tags:
About Python
===
History
---
* Created early 90's by Guido van Rossem at CWI.
- Name: Monty Python.
* General purpose, high-level programming language.
* Design is driven by code readability.
%% Cell type:markdown id: tags:
About Python
===
Features
---
* Interpreted, no separate compilation step needed.
* Imperative and object-oriented programming (and some functional programming).
* Imperative and object-oriented programming.
- And some functional programming.
* Dynamic type system.
* Automatic memory management.
We'll come back to most of this.
%% Cell type:markdown id: tags:
About Python
===
Why Python?
---
* Readable and low barrier to entry.
* Rich scientific libraries.
* Many other libraries available.
* Widely used with a large community.
%% Cell type:markdown id: tags:
About Python
===
Python 2 versus Python 3
---
* Python 3 is backwards incompatible.
* Some libraries don't support it yet.
* Python 2.7 is the last Python 2.
* Some Python 3 features are backported in Python 2.7.
* Default Python on most distributions is Python 2.7.
We use Python 2.7 for the time being.
%% Cell type:markdown id: tags:
Running Python code
===
Two main ways of writing and executing Python code.
Interactively
---
* Statement by statement directly in the interpreter.
Non-interactively
---
* By editing in a file and running the code afterwards.
%% Cell type:markdown id: tags:
We'll start with the first option.
%% Cell type:markdown id: tags:
Running Python code
===
The standard Python interpreter
---
Start it by typing `python` on the command line:
$ python
Python 2.7.3 (default, Jan 2 2013, 13:56:14)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
* It shows an interpreter prompt.
* You can give it Python code to interpret.
%% Cell type:markdown id: tags:
Running Python code
===
The IPython interpreter
---
* Similar to the standard Python interpreter, but with
* syntax highlighting,
* tab completion,
* cross-session history, etcetera...
Start it by typing `ipython` on the command line:
$ ipython
Python 2.7.3 (default, Jan 2 2013, 13:56:14)
Type "copyright", "credits" or "license" for more information.
IPython 0.13.1 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]:
%% Cell type:markdown id: tags:
From now on, play along in your own IPython interpreter.
%% Cell type:markdown id: tags:
Python as a calculator
===
Integers
---
%% Cell type:code id: tags:
``` python
17
```
%% Output
17
%% Cell type:code id: tags:
``` python
(17 + 4) * 2
```
%% Output
42
%% Cell type:markdown id: tags:
Python as a calculator
===
Floating point numbers
---
%% Cell type:code id: tags:
``` python
3.2 * 18 - 2.1
```
%% Output
55.5
%% Cell type:code id: tags:
``` python
36. / 5
```
%% Output
7.2
%% Cell type:markdown id: tags:
Scientific notation:
%% Cell type:code id: tags:
``` python
1.3e20 + 2
```
%% Output
1.3e+20
%% Cell type:code id: tags:
``` python
1.3 * 10**20
```
%% Output
1.3e+20
%% Cell type:markdown id: tags:
Python as a calculator
===
The division operator
---
%% Cell type:code id: tags:
``` python
35 / 5
```
%% Output
7
%% Cell type:code id: tags:
``` python
36 / 5
```
%% Output
7
%% Cell type:markdown id: tags:
Division is a bit weird: if you give it integer arguments, the result will also be an integer.
%% Cell type:markdown id: tags:
Python as a calculator
===
The division operator
---
%% Cell type:code id: tags:
``` python
35 / 5
```
%% Output
7
%% Cell type:code id: tags:
``` python
36 / 5
```
%% Output
7
%% Cell type:markdown id: tags:
Solution 1
---
Give floating point arguments instead of integer arguments.
%% Cell type:code id: tags:
``` python
36. / 5.
```
%% Output
7.2
%% Cell type:markdown id: tags:
Python as a calculator
===
The division operator
---
%% Cell type:code id: tags:
``` python
35 / 5
```
%% Output
7
%% Cell type:code id: tags:
``` python
36 / 5
```
%% Output
7
%% Cell type:markdown id: tags:
Solution 2
---
From Python 3 onwards, division behaves differently. You can actually get that behaviour in Python 2.7:
%% Cell type:code id: tags:
``` python
from __future__ import division
36 / 5
```
%% Output
7.2
%% Cell type:markdown id: tags:
Python as a calculator
===
Variables
---
* We can use names to reference values (variables).
* No need to declare them first or define the type.
%% Cell type:code id: tags:
``` python
a = 1.3e20
b = 2
```
%% Cell type:code id: tags:
``` python
a
```
%% Output
1.3e+20
%% Cell type:code id: tags:
``` python
b = a + 1.2e19
b * 2
c = a + 1.5e19 * b
c * 2
```
%% Output
2.84e+20
3.2e+20
%% Cell type:markdown id: tags:
Python's type system (1/2)
Python's type system (1/3)
===
Every value has a type, view it using `type`:
%% Cell type:code id: tags:
``` python
type(27)
```
%% Output
int
%% Cell type:code id: tags:
``` python
type(3.0 * 2.7)
```
%% Output
float
%% Cell type:code id: tags:
``` python
type(a)
```
%% Output
float
%% Cell type:markdown id: tags:
Another example of a builtin datatype is `str`, we'll see more later:
%% Cell type:code id: tags:
``` python
type('I am a homo sapiens')
```
%% Output
str
%% Cell type:markdown id: tags:
Python's type system (2/2)
Python's type system (2/3)
===
Some operations are defined on more than one type, possibly with different meanings.
%% Cell type:code id: tags:
``` python
'beer' * 5 + 'whiskey'
```
%% Output
'beerbeerbeerbeerbeerwhiskey'
%% Cell type:markdown id: tags:
Dynamic typing means that variables can be assigned values of different types during runtime.
%% Cell type:code id: tags:
``` python
a
```
%% Output
1.3e+20
%% Cell type:code id: tags:
``` python
type(a)
```
%% Output
float
%% Cell type:code id: tags:
``` python
a = 'spezi'
type(a)
```
%% Output
str
%% Cell type:markdown id: tags:
Python's type system (2/3)
===
Python is strongly typed, meaning that operations on values with incompatible types are forbidden.
%% Cell type:code id: tags:
``` python
'beer' + 34
```
%% Output
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-24-ec918fbfdf41> in <module>()
----> 1 'beer' + 34
TypeError: cannot concatenate 'str' and 'int' objects
%% Cell type:markdown id: tags:
<div class="alert alert-success">
<h1>Hands on!</h1>
<ol>
<li>We’ve seen that b = 2 is legal.
<ul>
<li> What about 2 = b? </li>
<li>How about a = b = 1?</li>
</ul>
</li>
<li>In math notation you can multiply x and y like this: xy.
What happens if you try that in Python?</li>
<li>How many seconds are there in 42 minutes and 42 seconds?</li>
<li>How many miles are there in 16 kilometers? Hint: there are 1.61 kilometers in a mile.</li>
<li>Let's assume that you run a 42 kilometer race in 4 hours 42 minutes and 42 seconds.
<ul>
<li>What is your average pace (time per mile in minutes and seconds)?</li>
<li>What is your average speed in miles per hour?</li>
</ul>
</li>
<li>Use string operations to reference string 'tra la la la' in a variable named *song*.</li>
<li>If an article costs 249 Euros including the 19% Value Added Tax (VAT), what is the actual VAT amount in Euros for the corresponding article?</li>
</ol>
</div>
%% Cell type:code id: tags:
``` python
from IPython.display import HTML
def css_styling():
styles = open('styles/custom.css', 'r').read()
return HTML('<style>' + styles + '</style>')
css_styling()
```
%% Output
<IPython.core.display.HTML at 0x3463a50>
%% Cell type:markdown id: tags:
Acknowledgements
========
Martijn Vermaat
[Jeroen Laros](mailto:j.f.j.laros@lumc.nl)
Based on
---------
[Python Scientific Lecture Notes](http://scipy-lectures.github.io/)
License
--------
[Creative Commons Attribution 3.0 License (CC-by)](http://creativecommons.org/licenses/by/3.0)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment