campus-fryslan-introduction.../Week-2/00. Simple Expressions.ipynb
2018-09-14 17:33:07 +02:00

318 lines
6.8 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This exercise shows you how to work with variables and expressions, Let's start, press CTRL+ENTER to run the cell.\n",
"\n",
"Everytimes there is a `None`, you need to change that to the correct expression.\n",
"\n",
"PS: this is exercise `0`, because computers start counting at `0` instead of `1`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"x = 13\n",
"print('x: ', x)\n",
"print('type(x): ', type(x))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"**Excercise:** Apply integer division by `2` on `x` using `//`, replace `None` with the correct expression."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"x = None\n",
"print('x: ', x)\n",
"print('type(x): ', type(x))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Notice that `x` is still an integer\n",
"\n",
"**Excercise:** Apply floating point division by `2` on `x` using `/`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"None\n",
"print('x: ', x)\n",
"print('type(x): ', type(x))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Notice that `x` is a floating point number now, even though `6` is divisible by `2`.\n",
"\n",
"**Excercise:** Create a new variable `y`, with value `9`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"None\n",
"print('x: ', x, ', y: ', y)\n",
"print('type(x): ', type(x), ', type(y): ', type(y))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Notice that x is still a float, and y an integer.\n",
"\n",
"**Exercise:** Assign the sum of `x` and `y` to `y`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"None\n",
"print('x: ', x, ', y: ', y)\n",
"print('type(x): ', type(x), ', type(y): ', type(y))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Notice that `x` did not change, and that y becames a float.\n",
"\n",
"**Exercise:** Square `y` using `**`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"None\n",
"print('x: ', x, ', y: ', y)\n",
"print('type(x): ', type(x), ', type(y): ', type(y))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now, let's work with strings.\n",
"\n",
"**Exercise:** Assign the text value `'https://www.rug.nl/'` to the variable `url`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"None\n",
"print('url: ', url, ', type(url): ', type(url))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Exercise:** Use `+` and `*` to add `10` asterisks (`'*'`) to the url"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"None\n",
"print('url: ', url, ', type(url): ', type(url))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Exercise:** Use `url.find( ... )` to locate the first asterisk, assign that position to `asterisk_at`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"None\n",
"print('url: ', url, ', type(url): ', type(url))\n",
"print('asterisk_at: ', asterisk_at, ', type(asterisk_at): ', type(asterisk_at))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Exercise:** Use `[: ... ]` to extract the first `asterisk_at` characters of `url`, removing the asterisks"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"url = url[: None]\n",
"print('url: ', url, ', type(url): ', type(url))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Not everything can be solved by multiplication, addition and such operations, even in mathematics.\n",
"\n",
"Even though this is not entirely true, let us assume it is for practical purposes.\n",
"\n",
"Some functionality comes as a *function*. One example of such a function is `print`. You use this function with brackets. \n",
"\n",
"**Exercise:** get the type of `print` using `type( ... )`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"type(None)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The syntax for *calling* functions is similar to that of mathematical functions, like sine and cosine. Python also has those."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from math import sin, cos, sqrt"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Exercise:** calculate the sine and cosine of 0, and the square root of 16."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"cosine_at_0 = None\n",
"sine_at_0 = None\n",
"sqrt_of_16 = None\n",
"\n",
"print('cosine at 0: ', cosine_at_0)\n",
"print('sine at 0: ', sine_at_0)\n",
"print('square root of 16: ', sqrt_of_16)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You've learned about\n",
"\n",
" * Expressions: `x * (5 + y)`\n",
" * Assigments: `x = 5`\n",
" * Strings: `'Mark ate a burger yesterday'`\n",
" * Functions: `print('hello')`, `sin( ... )`\n",
" * Types: `type( ... )`"
]
}
],
"metadata": {
"hide_input": false,
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.4.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}