" <li>Make a list with 10 integer elements. Sum all the items in the list.</li>\n",
" <li>Make a new list from the above one that does not include the 0th, 4th and 5th elements.\n",
" <li>Sum only the elements from the first list which are between the 2nd and 6th elements.\n",
" <li>Make a new list that includes only the elements that are greater than 10 from the first list.\n",
" <li>Food.\n",
" <ul>\n",
" <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>\n",
" <li>Create a corresponding dictionary called \"stocks\" and put the stock values in it, e.g., \"apples\": 0, \"oranges\": 1, \"pears\": 10, ...</li>\n",
" <li>Print stock and price information for each food item.</li>\n",
" <li>Determine and print how much money you would make if you sold all of your food products.\n",
" </ul>\n",
" </li>\n",
"</ol>\n",
"\n",
"</div>"
]
},
{
"cell_type": "markdown",
"metadata": {
...
...
@@ -1865,21 +1902,21 @@
"metadata": {
"celltoolbar": "Slideshow",
"kernelspec": {
"display_name": "Python 2",
"display_name": "Python 3",
"language": "python",
"name": "python2"
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.12"
"pygments_lexer": "ipython3",
"version": "3.5.2"
}
},
"nbformat": 4,
...
...
%% Cell type:markdown id: tags:
<span style="font-size: 200%">Introduction to Python (2)</span>
===
%% Cell type:markdown id: tags:
Sequence types
===
Lists
---
Mutable sequences of values.
%% Cell type:code id: tags:
``` python
l=[2,5,2,3,7]
type(l)
```
%% Output
list
%% Cell type:markdown id: tags:
Lists can be heterogeneous, but we typically don't use that.
%% Cell type:code id: tags:
``` python
a='spezi'
[3,'abc',1.3e20,[a,a,2]]
```
%% Output
[3, 'abc', 1.3e+20, ['spezi', 'spezi', 2]]
%% Cell type:markdown id: tags:
Sequence types
===
Tuples
---
Immutable sequences of values.
%% Cell type:code id: tags:
``` python
t='white',77,1.5
type(t)
```
%% Output
tuple
%% Cell type:code id: tags:
``` python
color,width,scale=t
width
```
%% Output
77
%% Cell type:markdown id: tags:
Sequence types
===
Strings (1/2)
---
Immutable sequences of characters.
%% Cell type:code id: tags:
``` python
'a string can be written in single quotes'
```
%% Output
'a string can be written in single quotes'
%% Cell type:markdown id: tags:
Strings can also be written with double quotes, or over multiple lines with triple-quotes.
%% Cell type:code id: tags:
``` python
"this makes it easier to use the ' character"
```
%% Output
"this makes it easier to use the ' character"
%% Cell type:code id: tags:
``` python
"""This is a multiline string.
You see? I continued after a blank line."""
```
%% Output
'This is a multiline string.\n\nYou see? I continued after a blank line.'
%% Cell type:markdown id: tags:
Sequence types
===
Strings (2/2)
---
A common operation is formatting strings using argument substitutions.
%% Cell type:code id: tags:
``` python
'{} times {} equals {:.2f}'.format('pi',2,6.283185307179586)
```
%% Output
'pi times 2 equals 6.28'
%% Cell type:markdown id: tags:
Accessing arguments by position or name is more readable.
%% Cell type:code id: tags:
``` python
'{1} times {0} equals {2:.2f}'.format('pi',2,6.283185307179586)
```
%% Output
'2 times pi equals 6.28'
%% Cell type:code id: tags:
``` python
'{number} times {amount} equals {result:.2f}'.format(number='pi',amount=2,
result=6.283185307179586)
```
%% Output
'pi times 2 equals 6.28'
%% Cell type:markdown id: tags:
Sequence types
===
Common operations (1/2)
---
All sequence types support concatenation, membership/substring tests, indexing, and slicing.
%% Cell type:code id: tags:
``` python
[1,2,3]+[4,5,6]
```
%% Output
[1, 2, 3, 4, 5, 6]
%% Cell type:code id: tags:
``` python
'bier'in'we drinken bier vanaf half 5'
```
%% Output
True
%% Cell type:code id: tags:
``` python
'abcdefghijkl'[5]
```
%% Output
'f'
%% Cell type:markdown id: tags:
Sequence types
===
Slicing
---
Slice `s` from `i` to `j` with `s[i:j]`.
%% Cell type:code id: tags:
``` python
'abcdefghijkl'[4:8]
```
%% Output
'efgh'
%% Cell type:code id: tags:
``` python
'abcdefghijkl'[:3]
```
%% Output
'abc'
%% Cell type:markdown id: tags:
We can also define the step `k` with `s[i:j:k]`.
%% Cell type:code id: tags:
``` python
'abcdefghijkl'[7:3:-1]
```
%% Output
'hgfe'
%% Cell type:markdown id: tags:
Sequence types
===
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.
%% Cell type:code id: tags:
``` python
len('attacgataggcatccgt')
```
%% Output
18
%% Cell type:code id: tags:
``` python
max([17,86,34,51])
```
%% Output
86
%% Cell type:code id: tags:
``` python
('atg',22,True,'atg').count('atg')
```
%% Output
2
%% Cell type:markdown id: tags:
Sequence types
===
Additional operations with lists
---
We can replace, add, remove, reverse and sort items in-place.
%% Cell type:code id: tags:
``` python
l=[1,2,3,4]
l[3]=7
l.append(1)
l[1:3]=[3,2]
l.sort()
l.reverse()
```
%% Cell type:code id: tags:
``` python
l
```
%% Output
[7, 3, 2, 1, 1]
%% Cell type:markdown id: tags:
Dictionaries
===
Dictionaries map *hashable* values to arbitrary objects