Skip to content

Commit 8b9d9fa

Browse files
committed
week 2 exercises first draft
1 parent e5203dc commit 8b9d9fa

File tree

5 files changed

+1036
-0
lines changed

5 files changed

+1036
-0
lines changed

week_2/data_types.ipynb

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Data Types (mostly carried forward from week 1 notebook)"
8+
]
9+
},
10+
{
11+
"cell_type": "markdown",
12+
"metadata": {},
13+
"source": [
14+
"##### Operators and types\n",
15+
"##### We have already used Operators, ie special tokens that represent computations like addition, multiplication and division. The values the operator works on are called operands.\n",
16+
"##### 'Integer' is a whole number which is expressed without any decimal places, eg 1, 6, 23, etc. 'Floating point' number has numbers after the decimal point - even zero counts, eg 1.3, 6.278, 23.0 etc\n",
17+
"##### In Python 3 the division operator / produces a floating point result (even if the result is an integer; 4/2 is 2.0). If you want truncated division, which ignores the remainder, you can use the // operator (for example, 5//2 is 2). "
18+
]
19+
},
20+
{
21+
"cell_type": "markdown",
22+
"metadata": {},
23+
"source": [
24+
"##### What do you expect the following to produce - try before you run it?"
25+
]
26+
},
27+
{
28+
"cell_type": "code",
29+
"execution_count": null,
30+
"metadata": {},
31+
"outputs": [],
32+
"source": [
33+
"print(9 / 5)\n",
34+
"print(5 / 9)\n",
35+
"print(9 // 5)"
36+
]
37+
},
38+
{
39+
"cell_type": "markdown",
40+
"metadata": {},
41+
"source": [
42+
"##### The truncated division operator, //, also works on floating point numbers. It truncates to the nearest integer, but still produces a floating point result. Thus 7.0 // 3.0 is 2.0. What do you expect the following to produce - try before you run it?"
43+
]
44+
},
45+
{
46+
"cell_type": "code",
47+
"execution_count": null,
48+
"metadata": {},
49+
"outputs": [],
50+
"source": [
51+
"print(7.0 / 3.0)\n",
52+
"print(7.0 // 3.0)\n"
53+
]
54+
},
55+
{
56+
"cell_type": "markdown",
57+
"metadata": {},
58+
"source": [
59+
"##### The modulus operator, sometimes also called the remainder operator or integer remainder operator works on integers and yields the remainder when the first operand is divided by the second. In Python, the modulus operator is a percent sign (%). What will the following produce?"
60+
]
61+
},
62+
{
63+
"cell_type": "code",
64+
"execution_count": null,
65+
"metadata": {},
66+
"outputs": [],
67+
"source": [
68+
"print(7 % 3) "
69+
]
70+
},
71+
{
72+
"cell_type": "markdown",
73+
"metadata": {},
74+
"source": [
75+
"##### What do you think the following will produce:\n",
76+
"- print(18 / 4)\n",
77+
"- print(18.0 // 4)\n",
78+
"- print(18 % 4)\n",
79+
"\n",
80+
"##### Now check your answers by writing some Python code"
81+
]
82+
},
83+
{
84+
"cell_type": "markdown",
85+
"metadata": {},
86+
"source": [
87+
"#### Variable names cannot start with a number - not a particularly helpful error message in this case!. Similarly, they cannot contain spaces - try printing out a variable name containing a space to demonstrate it."
88+
]
89+
},
90+
{
91+
"cell_type": "code",
92+
"execution_count": null,
93+
"metadata": {},
94+
"outputs": [],
95+
"source": []
96+
}
97+
],
98+
"metadata": {
99+
"kernelspec": {
100+
"display_name": "Python 3",
101+
"language": "python",
102+
"name": "python3"
103+
},
104+
"language_info": {
105+
"codemirror_mode": {
106+
"name": "ipython",
107+
"version": 3
108+
},
109+
"file_extension": ".py",
110+
"mimetype": "text/x-python",
111+
"name": "python",
112+
"nbconvert_exporter": "python",
113+
"pygments_lexer": "ipython3",
114+
"version": "3.7.4"
115+
}
116+
},
117+
"nbformat": 4,
118+
"nbformat_minor": 2
119+
}

week_2/dicts.ipynb

+157
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Dictionaries"
8+
]
9+
},
10+
{
11+
"cell_type": "markdown",
12+
"metadata": {},
13+
"source": [
14+
"### Start with some practice at getting the value from key:value pairs in a dictionary."
15+
]
16+
},
17+
{
18+
"cell_type": "markdown",
19+
"metadata": {},
20+
"source": [
21+
"##### A dictionary has aleady been defined below. Can you print out the French odd numbers:"
22+
]
23+
},
24+
{
25+
"cell_type": "code",
26+
"execution_count": null,
27+
"metadata": {},
28+
"outputs": [],
29+
"source": [
30+
"engtofr = {'one':'un', 'two':'deux', 'three':'trois', 'four':'quatre', 'five':'quinze'}"
31+
]
32+
},
33+
{
34+
"cell_type": "markdown",
35+
"metadata": {},
36+
"source": [
37+
"##### Now we'll add the next number"
38+
]
39+
},
40+
{
41+
"cell_type": "code",
42+
"execution_count": null,
43+
"metadata": {},
44+
"outputs": [],
45+
"source": [
46+
"engtofr['six'] = 'six'\n",
47+
"print('The new dictionary is', engtofr)"
48+
]
49+
},
50+
{
51+
"cell_type": "markdown",
52+
"metadata": {},
53+
"source": [
54+
"##### Can you add the numbers up to ten in a new cell - assuming you know French!"
55+
]
56+
},
57+
{
58+
"cell_type": "markdown",
59+
"metadata": {},
60+
"source": [
61+
"##### Maybe we want to show both feminine and masculine versions of 'one', so we can modify one of the key:value pairs as follows:"
62+
]
63+
},
64+
{
65+
"cell_type": "code",
66+
"execution_count": null,
67+
"metadata": {},
68+
"outputs": [],
69+
"source": [
70+
"engtofr['one'] = 'un or unne'\n",
71+
"print('The new dictionary is', engtofr)"
72+
]
73+
},
74+
{
75+
"cell_type": "markdown",
76+
"metadata": {},
77+
"source": [
78+
"##### Oops - I wrote 'unne' instead of 'une'! Can you correct it in a new cell below please."
79+
]
80+
},
81+
{
82+
"cell_type": "markdown",
83+
"metadata": {},
84+
"source": [
85+
"##### Removing key:value pairs can be done using 'del' statement:"
86+
]
87+
},
88+
{
89+
"cell_type": "code",
90+
"execution_count": null,
91+
"metadata": {},
92+
"outputs": [],
93+
"source": [
94+
"del engtofr['two']\n",
95+
"print('My shortened list is', engtofr)"
96+
]
97+
},
98+
{
99+
"cell_type": "markdown",
100+
"metadata": {},
101+
"source": [
102+
"##### What happens if you run the previous cell again? Can you see what the problem is?"
103+
]
104+
},
105+
{
106+
"cell_type": "markdown",
107+
"metadata": {},
108+
"source": [
109+
"### Further methods for dictionaries"
110+
]
111+
},
112+
{
113+
"cell_type": "markdown",
114+
"metadata": {},
115+
"source": [
116+
"##### Create a dictionary in a new cell below - whatever you like. Let's assume you call it test_dict"
117+
]
118+
},
119+
{
120+
"cell_type": "markdown",
121+
"metadata": {},
122+
"source": [
123+
"##### Now print out test_dict.keys()\n",
124+
"##### Similarly, print out test_dict.values() and test_dict.items().\n",
125+
"##### These can all be useful, eg extracting the values to plot on a graph"
126+
]
127+
},
128+
{
129+
"cell_type": "code",
130+
"execution_count": null,
131+
"metadata": {},
132+
"outputs": [],
133+
"source": []
134+
}
135+
],
136+
"metadata": {
137+
"kernelspec": {
138+
"display_name": "Python 3",
139+
"language": "python",
140+
"name": "python3"
141+
},
142+
"language_info": {
143+
"codemirror_mode": {
144+
"name": "ipython",
145+
"version": 3
146+
},
147+
"file_extension": ".py",
148+
"mimetype": "text/x-python",
149+
"name": "python",
150+
"nbconvert_exporter": "python",
151+
"pygments_lexer": "ipython3",
152+
"version": "3.7.4"
153+
}
154+
},
155+
"nbformat": 4,
156+
"nbformat_minor": 2
157+
}

0 commit comments

Comments
 (0)