"* Created early 90's by Guido van Rossem at CWI.\n",
" - Name: Monty Python.\n",
"* General purpose, high-level programming language.\n",
"* Design is driven by code readability."
]
...
...
@@ -46,7 +47,8 @@
"---\n",
"\n",
"* Interpreted, no separate compilation step needed.\n",
"* Imperative and object-oriented programming (and some functional programming).\n",
"* Imperative and object-oriented programming.\n",
" - And some functional programming.\n",
"* Dynamic type system.\n",
"* Automatic memory management.\n",
"\n",
...
...
@@ -612,16 +614,17 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"a = 1.3e20"
"a = 1.3e20\n",
"b = 2"
]
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 21,
"metadata": {},
"outputs": [
{
...
...
@@ -630,7 +633,7 @@
"1.3e+20"
]
},
"execution_count": 16,
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
...
...
@@ -641,23 +644,23 @@
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 28,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2.84e+20"
"3.2e+20"
]
},
"execution_count": 17,
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b = a + 1.2e19\n",
"b * 2"
"c = a + 1.5e19 * b\n",
"c * 2"
]
},
{
...
...
@@ -668,7 +671,7 @@
}
},
"source": [
"Python's type system (1/2)\n",
"Python's type system (1/3)\n",
"===\n",
"\n",
"Every value has a type, view it using `type`:"
...
...
@@ -769,7 +772,7 @@
}
},
"source": [
"Python's type system (2/2)\n",
"Python's type system (2/3)\n",
"===\n",
"\n",
"Some operations are defined on more than one type, possibly with different meanings."
...
...
@@ -804,7 +807,47 @@
},
{
"cell_type": "code",
"execution_count": 23,
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1.3e+20"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"float"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(a)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
...
...
@@ -813,7 +856,7 @@
"str"
]
},
"execution_count": 23,
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
...
...
@@ -825,8 +868,15 @@
},
{
"cell_type": "markdown",
"metadata": {},
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"Python's type system (2/3)\n",
"===\n",
"\n",
"Python is strongly typed, meaning that operations on values with incompatible types are forbidden."
]
},
...
...
@@ -850,6 +900,41 @@
"'beer' + 34"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"<div class=\"alert alert-success\">\n",
"<h1>Hands on!</h1>\n",
"\n",
"<ol>\n",
" <li>We’ve seen that b = 2 is legal.\n",
" <ul>\n",
" <li> What about 2 = b? </li>\n",
" <li>How about a = b = 1?</li>\n",
" </ul>\n",
" </li>\n",
" <li>In math notation you can multiply x and y like this: xy.\n",
" What happens if you try that in Python?</li>\n",
" <li>How many seconds are there in 42 minutes and 42 seconds?</li>\n",
" <li>How many miles are there in 16 kilometers? Hint: there are 1.61 kilometers in a mile.</li>\n",
" <li>Let's assume that you run a 42 kilometer race in 4 hours 42 minutes and 42 seconds.\n",
" <ul>\n",
" <li>What is your average pace (time per mile in minutes and seconds)?</li>\n",
" <li>What is your average speed in miles per hour?</li>\n",
" </ul>\n",
" </li>\n",
" <li>Use string operations to reference string 'tra la la la' in a variable named *song*.</li>\n",
" <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>\n",
"</ol>\n",
"\n",
"</div>"
]
},
{
"cell_type": "code",
"execution_count": 1,
...
...
%% 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__importdivision
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.