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

Introduction update to types.

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