Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
Programming course
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
courses
Programming course
Commits
d45c7259
Commit
d45c7259
authored
7 years ago
by
Mihai Lefter
Browse files
Options
Downloads
Patches
Plain Diff
Moved the boolean exercise.
parent
a4f70cf5
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
introduction/02_introduction_to_python_2.ipynb
+18
-18
18 additions, 18 deletions
introduction/02_introduction_to_python_2.ipynb
with
18 additions
and
18 deletions
introduction/02_introduction_to_python_2.ipynb
+
18
−
18
View file @
d45c7259
...
@@ -1176,6 +1176,24 @@
...
@@ -1176,6 +1176,24 @@
" i += 1"
" i += 1"
]
]
},
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"Hands on!\n",
"===\n",
"\n",
"Try to guess the outcome of the following statements:\n",
"\n",
" 2 * 3 > 4\n",
" 2 * (3 > 4)\n",
" 2 * (4 > 3)"
]
},
{
{
"cell_type": "markdown",
"cell_type": "markdown",
"metadata": {
"metadata": {
...
@@ -1774,24 +1792,6 @@
...
@@ -1774,24 +1792,6 @@
"**Hint:** All elements in a set are unique."
"**Hint:** All elements in a set are unique."
]
]
},
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"$\\S$ Exercise: Boolean comparison\n",
"===\n",
"\n",
"Try to guess the outcome of the following statements:\n",
"\n",
" 2 * 3 > 4\n",
" 2 * (3 > 4)\n",
" 2 * (4 > 3)"
]
},
{
{
"cell_type": "markdown",
"cell_type": "markdown",
"metadata": {
"metadata": {
...
...
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
<span style="font-size: 200%">Introduction to Python (2)</span>
<span style="font-size: 200%">Introduction to Python (2)</span>
===
===
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Sequence types
Sequence types
===
===
Lists
Lists
---
---
Mutable sequences of values.
Mutable sequences of values.
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
l
=
[
2
,
5
,
2
,
3
,
7
]
l
=
[
2
,
5
,
2
,
3
,
7
]
type
(
l
)
type
(
l
)
```
```
%% Output
%% Output
list
list
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Lists can be heterogeneous, but we typically don't use that.
Lists can be heterogeneous, but we typically don't use that.
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
a
=
'
spezi
'
a
=
'
spezi
'
[
3
,
'
abc
'
,
1.3e20
,
[
a
,
a
,
2
]]
[
3
,
'
abc
'
,
1.3e20
,
[
a
,
a
,
2
]]
```
```
%% Output
%% Output
[3, 'abc', 1.3e+20, ['spezi', 'spezi', 2]]
[3, 'abc', 1.3e+20, ['spezi', 'spezi', 2]]
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Sequence types
Sequence types
===
===
Tuples
Tuples
---
---
Immutable sequences of values.
Immutable sequences of values.
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
t
=
'
white
'
,
77
,
1.5
t
=
'
white
'
,
77
,
1.5
type
(
t
)
type
(
t
)
```
```
%% Output
%% Output
tuple
tuple
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
color
,
width
,
scale
=
t
color
,
width
,
scale
=
t
width
width
```
```
%% Output
%% Output
77
77
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Sequence types
Sequence types
===
===
Strings (1/2)
Strings (1/2)
---
---
Immutable sequences of characters.
Immutable sequences of characters.
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
'
a string can be written in single quotes
'
'
a string can be written in single quotes
'
```
```
%% Output
%% Output
'a string can be written in single quotes'
'a string can be written in single quotes'
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Strings can also be written with double quotes, or over multiple lines with triple-quotes.
Strings can also be written with double quotes, or over multiple lines with triple-quotes.
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
"
this makes it easier to use the
'
character
"
"
this makes it easier to use the
'
character
"
```
```
%% Output
%% Output
"this makes it easier to use the ' character"
"this makes it easier to use the ' character"
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
"""
This is a multiline string.
"""
This is a multiline string.
You see? I continued after a blank line.
"""
You see? I continued after a blank line.
"""
```
```
%% Output
%% Output
'This is a multiline string.\n\nYou see? I continued after a blank line.'
'This is a multiline string.\n\nYou see? I continued after a blank line.'
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Sequence types
Sequence types
===
===
Strings (2/2)
Strings (2/2)
---
---
A common operation is formatting strings using argument substitutions.
A common operation is formatting strings using argument substitutions.
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
'
{} times {} equals {:.2f}
'
.
format
(
'
pi
'
,
2
,
6.283185307179586
)
'
{} times {} equals {:.2f}
'
.
format
(
'
pi
'
,
2
,
6.283185307179586
)
```
```
%% Output
%% Output
'pi times 2 equals 6.28'
'pi times 2 equals 6.28'
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Accessing arguments by position or name is more readable.
Accessing arguments by position or name is more readable.
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
'
{1} times {0} equals {2:.2f}
'
.
format
(
'
pi
'
,
2
,
6.283185307179586
)
'
{1} times {0} equals {2:.2f}
'
.
format
(
'
pi
'
,
2
,
6.283185307179586
)
```
```
%% Output
%% Output
'2 times pi equals 6.28'
'2 times pi equals 6.28'
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
'
{number} times {amount} equals {result:.2f}
'
.
format
(
number
=
'
pi
'
,
amount
=
2
,
'
{number} times {amount} equals {result:.2f}
'
.
format
(
number
=
'
pi
'
,
amount
=
2
,
result
=
6.283185307179586
)
result
=
6.283185307179586
)
```
```
%% Output
%% Output
'pi times 2 equals 6.28'
'pi times 2 equals 6.28'
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Sequence types
Sequence types
===
===
Common operations (1/2)
Common operations (1/2)
---
---
All sequence types support concatenation, membership/substring tests, indexing, and slicing.
All sequence types support concatenation, membership/substring tests, indexing, and slicing.
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
[
1
,
2
,
3
]
+
[
4
,
5
,
6
]
[
1
,
2
,
3
]
+
[
4
,
5
,
6
]
```
```
%% Output
%% Output
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6]
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
'
bier
'
in
'
we drinken bier vanaf half 5
'
'
bier
'
in
'
we drinken bier vanaf half 5
'
```
```
%% Output
%% Output
True
True
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
'
abcdefghijkl
'
[
5
]
'
abcdefghijkl
'
[
5
]
```
```
%% Output
%% Output
'f'
'f'
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Sequence types
Sequence types
===
===
Slicing
Slicing
---
---
Slice
`s`
from
`i`
to
`j`
with
`s[i:j]`
.
Slice
`s`
from
`i`
to
`j`
with
`s[i:j]`
.
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
'
abcdefghijkl
'
[
4
:
8
]
'
abcdefghijkl
'
[
4
:
8
]
```
```
%% Output
%% Output
'efgh'
'efgh'
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
'
abcdefghijkl
'
[:
3
]
'
abcdefghijkl
'
[:
3
]
```
```
%% Output
%% Output
'abc'
'abc'
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
We can also define the step
`k`
with
`s[i:j:k]`
.
We can also define the step
`k`
with
`s[i:j:k]`
.
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
'
abcdefghijkl
'
[
7
:
3
:
-
1
]
'
abcdefghijkl
'
[
7
:
3
:
-
1
]
```
```
%% Output
%% Output
'hgfe'
'hgfe'
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Sequence types
Sequence types
===
===
Common operations (2/2)
Common operations (2/2)
---
---
Contrary to strings and tuples, lists are mutable. We can also get their length, smallest/largest item, and number/position of certain items.
Contrary to strings and tuples, lists are mutable. We can also get their length, smallest/largest item, and number/position of certain items.
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
len
(
'
attacgataggcatccgt
'
)
len
(
'
attacgataggcatccgt
'
)
```
```
%% Output
%% Output
18
18
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
max
([
17
,
86
,
34
,
51
])
max
([
17
,
86
,
34
,
51
])
```
```
%% Output
%% Output
86
86
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
(
'
atg
'
,
22
,
True
,
'
atg
'
).
count
(
'
atg
'
)
(
'
atg
'
,
22
,
True
,
'
atg
'
).
count
(
'
atg
'
)
```
```
%% Output
%% Output
2
2
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Sequence types
Sequence types
===
===
Additional operations with lists
Additional operations with lists
---
---
We can replace, add, remove, reverse and sort items in-place.
We can replace, add, remove, reverse and sort items in-place.
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
l
=
[
1
,
2
,
3
,
4
]
l
=
[
1
,
2
,
3
,
4
]
l
[
3
]
=
7
l
[
3
]
=
7
l
.
append
(
1
)
l
.
append
(
1
)
l
[
1
:
3
]
=
[
3
,
2
]
l
[
1
:
3
]
=
[
3
,
2
]
l
.
sort
()
l
.
sort
()
l
.
reverse
()
l
.
reverse
()
```
```
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
l
l
```
```
%% Output
%% Output
[7, 3, 2, 1, 1]
[7, 3, 2, 1, 1]
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Dictionaries
Dictionaries
===
===
Dictionaries map *hashable* values to arbitrary objects
Dictionaries map *hashable* values to arbitrary objects
---
---
*
All built-in immutable objects are hashable.
*
All built-in immutable objects are hashable.
*
No built-in mutable objects are hashable.
*
No built-in mutable objects are hashable.
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
d
=
{
'
a
'
:
27
,
'
b
'
:
18
,
'
c
'
:
12
}
d
=
{
'
a
'
:
27
,
'
b
'
:
18
,
'
c
'
:
12
}
type
(
d
)
type
(
d
)
```
```
%% Output
%% Output
dict
dict
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
d
[
'
e
'
]
=
17
d
[
'
e
'
]
=
17
'
e
'
in
d
'
e
'
in
d
```
```
%% Output
%% Output
True
True
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
d
.
update
({
'
a
'
:
18
,
'
f
'
:
2
})
d
.
update
({
'
a
'
:
18
,
'
f
'
:
2
})
d
d
```
```
%% Output
%% Output
{'a': 18, 'b': 18, 'c': 12, 'e': 17, 'f': 2}
{'a': 18, 'b': 18, 'c': 12, 'e': 17, 'f': 2}
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Dictionaries
Dictionaries
===
===
Accessing dictionary content
Accessing dictionary content
---
---
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
d
[
'
b
'
]
d
[
'
b
'
]
```
```
%% Output
%% Output
18
18
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
d
.
keys
()
d
.
keys
()
```
```
%% Output
%% Output
['a', 'c', 'b', 'e', 'f']
['a', 'c', 'b', 'e', 'f']
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
d
.
values
()
d
.
values
()
```
```
%% Output
%% Output
[18, 12, 18, 17, 2]
[18, 12, 18, 17, 2]
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
d
.
items
()
d
.
items
()
```
```
%% Output
%% Output
[('a', 18), ('c', 12), ('b', 18), ('e', 17), ('f', 2)]
[('a', 18), ('c', 12), ('b', 18), ('e', 17), ('f', 2)]
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Sets
Sets
===
===
Mutable unordered collections of hashable values without duplication
Mutable unordered collections of hashable values without duplication
---
---
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
x
=
{
12
,
28
,
21
,
17
}
x
=
{
12
,
28
,
21
,
17
}
type
(
x
)
type
(
x
)
```
```
%% Output
%% Output
set
set
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
x
.
add
(
12
)
x
.
add
(
12
)
x
x
```
```
%% Output
%% Output
{12, 17, 21, 28}
{12, 17, 21, 28}
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
x
.
discard
(
21
)
x
.
discard
(
21
)
x
x
```
```
%% Output
%% Output
{12, 17, 28}
{12, 17, 28}
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Sets
Sets
===
===
Operations with sets
Operations with sets
---
---
We can test for membership and apply many common set operations such as union and intersect.
We can test for membership and apply many common set operations such as union and intersect.
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
17
in
{
12
,
28
,
21
,
17
}
17
in
{
12
,
28
,
21
,
17
}
```
```
%% Output
%% Output
True
True
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
{
12
,
28
,
21
,
17
}
|
{
12
,
18
,
11
}
{
12
,
28
,
21
,
17
}
|
{
12
,
18
,
11
}
```
```
%% Output
%% Output
{11, 12, 17, 18, 21, 28}
{11, 12, 17, 18, 21, 28}
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
{
12
,
28
,
21
,
17
}
&
{
12
,
18
,
11
}
{
12
,
28
,
21
,
17
}
&
{
12
,
18
,
11
}
```
```
%% Output
%% Output
{12}
{12}
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Booleans
Booleans
===
===
Boolean values and operations
Boolean values and operations
---
---
The two boolean values are written
`False`
and
`True`
.
The two boolean values are written
`False`
and
`True`
.
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
True
or
False
True
or
False
```
```
%% Output
%% Output
True
True
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
True
and
False
True
and
False
```
```
%% Output
%% Output
False
False
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
not
False
not
False
```
```
%% Output
%% Output
True
True
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Booleans
Booleans
===
===
Comparisons
Comparisons
---
---
Comparisons can be done on all objects and return a boolean value.
Comparisons can be done on all objects and return a boolean value.
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
22
*
3
>
66
22
*
3
>
66
```
```
%% Output
%% Output
False
False
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
We have two equivalence relations: value equality (
`==`
) and object identity (
`is`
).
We have two equivalence relations: value equality (
`==`
) and object identity (
`is`
).
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
a
,
b
=
[
1
,
2
,
3
],
[
1
,
2
,
3
]
a
,
b
=
[
1
,
2
,
3
],
[
1
,
2
,
3
]
a
==
b
a
==
b
```
```
%% Output
%% Output
True
True
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
a
is
b
a
is
b
```
```
%% Output
%% Output
False
False
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Booleans
Booleans
===
===
`if` statements
`if` statements
---
---
(The
`print`
statement writes a string representation of the given value.)
(The
`print`
statement writes a string representation of the given value.)
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
if
26
<=
17
:
if
26
<=
17
:
print
'
Fact: 26 is less than or equal to 17
'
print
'
Fact: 26 is less than or equal to 17
'
elif
(
26
+
8
>
14
)
==
True
:
elif
(
26
+
8
>
14
)
==
True
:
print
'
Did we need the ` == True` part here?
'
print
'
Did we need the ` == True` part here?
'
else
:
else
:
print
'
Nothing seems true
'
print
'
Nothing seems true
'
```
```
%% Output
%% Output
Did we need the ` == True` part here?
Did we need the ` == True` part here?
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Booleans
Booleans
===
===
`while` statements
`while` statements
---
---
Our first looping control structure just repeats until the given expression evaluates to
`False`
.
Our first looping control structure just repeats until the given expression evaluates to
`False`
.
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
i
=
0
i
=
0
while
i
<
5
:
while
i
<
5
:
print
i
print
i
i
+=
1
i
+=
1
```
```
%% Output
%% Output
0
0
1
1
2
2
3
3
4
4
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Hands on!
===
Try to guess the outcome of the following statements:
2 * 3 > 4
2 * (3 > 4)
2 * (4 > 3)
%% Cell type:markdown id: tags:
Notes about syntax
Notes about syntax
===
===
Indentation
Indentation
---
---
Python uses indentation to delimit blocks
Python uses indentation to delimit blocks
*
Instead of
`begin ... end`
or
`{ ... }`
in other languages.
*
Instead of
`begin ... end`
or
`{ ... }`
in other languages.
*
Always increase indentation by
*4 spaces*
, never use tabs.
*
Always increase indentation by
*4 spaces*
, never use tabs.
*
In any case, be consistent.
*
In any case, be consistent.
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
if
False
:
if
False
:
if
False
:
if
False
:
print
'
Why am I here?
'
print
'
Why am I here?
'
else
:
else
:
while
True
:
while
True
:
print
'
When will it stop?
'
print
'
When will it stop?
'
print
"
And we
'
re back to the first indentation level
"
print
"
And we
'
re back to the first indentation level
"
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Some editors can be configured to behave just like that.
Some editors can be configured to behave just like that.
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Notes about syntax
Notes about syntax
===
===
Comments
Comments
---
---
Comments are prepended by
`#`
and completely ignored.
Comments are prepended by
`#`
and completely ignored.
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
# Add 42 to this list.
# Add 42 to this list.
l
.
append
(
42
)
l
.
append
(
42
)
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
`pass` statements
`pass` statements
---
---
If you ever need a statement syntactically but don't want to do anything, use
`pass`
.
If you ever need a statement syntactically but don't want to do anything, use
`pass`
.
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
while
False
:
while
False
:
# This is never executed anyway.
# This is never executed anyway.
pass
pass
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Useful built-ins
Useful built-ins
===
===
Getting help
Getting help
---
---
You can get help on almost any object with
`help`
.
You can get help on almost any object with
`help`
.
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
help
(
range
)
help
(
range
)
```
```
%% Output
%% Output
Help on built-in function range in module __builtin__:
Help on built-in function range in module __builtin__:
range(...)
range(...)
range([start,] stop[, step]) -> list of integers
range([start,] stop[, step]) -> list of integers
Return a list containing an arithmetic progression of integers.
Return a list containing an arithmetic progression of integers.
range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.
range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.
When step is given, it specifies the increment (or decrement).
When step is given, it specifies the increment (or decrement).
For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!
For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!
These are exactly the valid indices for a list of 4 elements.
These are exactly the valid indices for a list of 4 elements.
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
In IPython you can do it faster by typing:
In IPython you can do it faster by typing:
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
range
?
range
?
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Useful built-ins
Useful built-ins
===
===
We'll shortly use the following built-in functions.
We'll shortly use the following built-in functions.
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
range
(
5
,
16
)
range
(
5
,
16
)
```
```
%% Output
%% Output
[5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
[5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
zip
([
'
red
'
,
'
white
'
,
'
blue
'
],
range
(
3
))
zip
([
'
red
'
,
'
white
'
,
'
blue
'
],
range
(
3
))
```
```
%% Output
%% Output
[('red', 0), ('white', 1), ('blue', 2)]
[('red', 0), ('white', 1), ('blue', 2)]
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
list
(
'
abcdefghijk
'
)
list
(
'
abcdefghijk
'
)
```
```
%% Output
%% Output
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k']
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k']
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Iteration
Iteration
===
===
Iterating over a sequence
Iterating over a sequence
---
---
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
colors
=
[
'
red
'
,
'
white
'
,
'
blue
'
,
'
orange
'
]
colors
=
[
'
red
'
,
'
white
'
,
'
blue
'
,
'
orange
'
]
cities
=
[
'
leiden
'
,
'
utrecht
'
,
'
warmond
'
,
'
san francisco
'
]
cities
=
[
'
leiden
'
,
'
utrecht
'
,
'
warmond
'
,
'
san francisco
'
]
```
```
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
The
`for`
statement can iterate over sequence items.
The
`for`
statement can iterate over sequence items.
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
for
color
in
colors
:
for
color
in
colors
:
print
color
print
color
```
```
%% Output
%% Output
red
red
white
white
blue
blue
orange
orange
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
for
character
in
'
blue
'
:
for
character
in
'
blue
'
:
print
character
print
character
```
```
%% Output
%% Output
b
b
l
l
u
u
e
e
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Iteration
Iteration
===
===
Python anti-patterns
Python anti-patterns
---
---
These are common for programmers coming from other languages.
These are common for programmers coming from other languages.
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
i
=
0
i
=
0
while
i
<
len
(
colors
):
while
i
<
len
(
colors
):
print
colors
[
i
]
print
colors
[
i
]
i
+=
1
i
+=
1
```
```
%% Output
%% Output
red
red
white
white
blue
blue
orange
orange
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
for
i
in
range
(
len
(
colors
)):
for
i
in
range
(
len
(
colors
)):
print
colors
[
i
]
print
colors
[
i
]
```
```
%% Output
%% Output
red
red
white
white
blue
blue
orange
orange
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
We call them
*unpythonic*
.
We call them
*unpythonic*
.
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Iteration
Iteration
===
===
Using values *and* indices
Using values *and* indices
---
---
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
for
i
,
color
in
enumerate
(
colors
):
for
i
,
color
in
enumerate
(
colors
):
print
i
,
'
->
'
,
color
print
i
,
'
->
'
,
color
```
```
%% Output
%% Output
0 -> red
0 -> red
1 -> white
1 -> white
2 -> blue
2 -> blue
3 -> orange
3 -> orange
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Taking two sequences together
Taking two sequences together
---
---
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
for
city
,
color
in
zip
(
cities
,
colors
):
for
city
,
color
in
zip
(
cities
,
colors
):
print
city
,
'
->
'
,
color
print
city
,
'
->
'
,
color
```
```
%% Output
%% Output
leiden -> red
leiden -> red
utrecht -> white
utrecht -> white
warmond -> blue
warmond -> blue
san francisco -> orange
san francisco -> orange
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Iteration
Iteration
===
===
Other iterables
Other iterables
---
---
Iterating over a dictionary yields keys.
Iterating over a dictionary yields keys.
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
for
key
in
{
'
a
'
:
33
,
'
b
'
:
17
,
'
c
'
:
18
}:
for
key
in
{
'
a
'
:
33
,
'
b
'
:
17
,
'
c
'
:
18
}:
print
key
print
key
```
```
%% Output
%% Output
a
a
c
c
b
b
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Iterating over a file yields lines.
Iterating over a file yields lines.
%% Cell type:code id: tags:
%% Cell type:code id: tags:
```
python
```
python
for
line
in
open
(
'
data/short_file.txt
'
):
for
line
in
open
(
'
data/short_file.txt
'
):
print
line
print
line
```
```
%% Output
%% Output
this short file has two lines
this short file has two lines
it is used in the example code
it is used in the example code
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
There are many more useful iterables in Python.
There are many more useful iterables in Python.
%% 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>
Make a list with 10 integer elements. Sum all the items in the list.
</li>
<li>
Make a list with 10 integer elements. Sum all the items in the list.
</li>
<li>
Make a new list from the above one that does not include the 0th, 4th and 5th elements.
<li>
Make a new list from the above one that does not include the 0th, 4th and 5th elements.
<li>
Sum only the elements from the first list which are between the 2nd and 6th elements.
<li>
Sum only the elements from the first list which are between the 2nd and 6th elements.
<li>
Make a new list that includes only the elements that are greater than 10 from the first list.
<li>
Make a new list that includes only the elements that are greater than 10 from the first list.
<li>
Food.
<li>
Food.
<ul>
<ul>
<li>
Create a dictionary for food products called "prices" and put some values in it, e.g., "apples": 2, "oranges": 1.5, "pears": 3, ...
</li>
<li>
Create a dictionary for food products called "prices" and put some values in it, e.g., "apples": 2, "oranges": 1.5, "pears": 3, ...
</li>
<li>
Create a corresponding dictionary called "stocks" and put the stock values in it, e.g., "apples": 0, "oranges": 1, "pears": 10, ...
</li>
<li>
Create a corresponding dictionary called "stocks" and put the stock values in it, e.g., "apples": 0, "oranges": 1, "pears": 10, ...
</li>
<li>
Print stock and price information for each food item.
</li>
<li>
Print stock and price information for each food item.
</li>
<li>
Determine and print how much money you would make if you sold all of your food products.
<li>
Determine and print how much money you would make if you sold all of your food products.
</ul>
</ul>
</li>
</li>
</ol>
</ol>
</div>
</div>
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
Homework assignment
Homework assignment
===
===
https://classroom.github.com/a/QU2iPYKn
https://classroom.github.com/a/QU2iPYKn
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
$\S$ Exercise: Iterate over a list
$\S$ Exercise: Iterate over a list
===
===
First we are going to make a list and fill it with a simple sequence. Then we are going to use this list to print something.
First we are going to make a list and fill it with a simple sequence. Then we are going to use this list to print something.
*
Make a list containing the numbers 0, 1, ... 9.
*
Make a list containing the numbers 0, 1, ... 9.
*
Print the last 10 lines of the song ''99 bottles of beer'' using this list.
*
Print the last 10 lines of the song ''99 bottles of beer'' using this list.
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
$\S$ Exercise: Analyse a repeat structure
$\S$ Exercise: Analyse a repeat structure
===
===
We are going to make a repeating DNA sequence and extract some subsequences from it.
We are going to make a repeating DNA sequence and extract some subsequences from it.
*
Make a short tandem repeat that consists of three "ACGT" units and five "TTATT" units.
*
Make a short tandem repeat that consists of three "ACGT" units and five "TTATT" units.
*
Print all suffixes of the repeat structure.
*
Print all suffixes of the repeat structure.
**Note:**
A suffix is an ending. For example, the word "spam" has five suffixes: "spam", "pam", "am", "m" and "".
**Note:**
A suffix is an ending. For example, the word "spam" has five suffixes: "spam", "pam", "am", "m" and "".
*
Print all substrings of length 3.
*
Print all substrings of length 3.
*
Print all unique substrings of length 3.
*
Print all unique substrings of length 3.
**Hint:**
All elements in a set are unique.
**Hint:**
All elements in a set are unique.
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
$\S$ Exercise: Boolean comparison
===
Try to guess the outcome of the following statements:
2 * 3 > 4
2 * (3 > 4)
2 * (4 > 3)
%% Cell type:markdown id: tags:
$\S$ Exercise: Combining lists
$\S$ Exercise: Combining lists
===
===
Calculate all coordinates of the line x=y with x < 100.
Calculate all coordinates of the line x=y with x < 100.
**Note:**
This is the sequence (0, 0), (1, 1), ... (99, 99)
**Note:**
This is the sequence (0, 0), (1, 1), ... (99, 99)
%% Cell type:markdown id: tags:
%% Cell type:markdown id: tags:
$\S$ Exercise: Dictionaries
$\S$ Exercise: Dictionaries
===
===
We are going to store the output of a function ($f(x) = x^2$) together with its input in a dictionary.
We are going to store the output of a function ($f(x) = x^2$) together with its input in a dictionary.
*
Make a dictionary containing all squares smaller than 100.
*
Make a dictionary containing all squares smaller than 100.
*
Print the content of this dictionary in english, e.g., "4 is the square of 2".
*
Print the content of this dictionary in english, e.g., "4 is the square of 2".
%% 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 0x1873a50>
<IPython.core.display.HTML at 0x1873a50>
%% 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
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment