{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Python basics\n",
"\n",
"These Jupyter Notebooks to present Python code. Execute blocks of code with **ctrl+enter**"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Reading:\n",
"[Python tutorial](https://docs.python.org/3.7/tutorial/) 3.1, 4.1 - 4.7, 5.1 - 5.6"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Variable types\n",
"Python will infer the type when you define the variable. With `x = 2`, `x` is set to an **int**. With `x = 2.0`, `x` is set to a **float**.
\n",
"`type(...)` displays a variable's type (useful for understanding the details of the code when debugging)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- **int**
\n",
"A (signed) integer ...−2,−1,0,1,2,..."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x = 2\n",
"print(type(x))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- **float**
\n",
"A real number with 16 digits of precision. (Equivalent to \"double\" in other languages. Python does not natively support single-precision floating point numbers.)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"y = 2.0\n",
"print(type(y))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- **str**
\n",
"A string, a sequence of 0 or more characters. Enclosed within a pair of single quotes `'` or a pair of double quotes `\"`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"a = \"Hello World!\"\n",
"print(type(a))\n",
"print(a)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"__Exercise__ : Read 3.1.2 in the [Python tutorial](https://docs.python.org/3.7/tutorial/introduction.html) and check the output of the following. (Double click the cell to see the actual code.)\n",
"- print(\"McDonald's\")\n",
"- print(\"McDonald\\'s\")\n",
"- print('McDonald\\'s')\n",
"- print('McDonald's')\n",
"- print(r\"McDonald\\'s\")\n",
"- print(3*\"McDonald\\'s\")\n",
"- print(\"McDonald\" + \"\\'s\")\n",
"- print(\"McDonald\" \"\\'s\")\n",
"- print(\"\"\"McDonald\n",
" \\'s\"\"\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(\"\"\"McDonald\n",
" multiline string\n",
" \\'s\"\"\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Indices refer to positions **between** characters. The left edge of the first character numbered **0**. Then the right edge of the last character of a string of length **n** characters has index **n**. \n",
"\n",
"
\n", " +---+---+---+---+---+---+\n", " | P | y | t | h | o | n |\n", " +---+---+---+---+---+---+\n", " 0 1 2 3 4 5 6\n", "-6 -5 -4 -3 -2 -1\n", "\n", "\n", "Python also uses negative indexes.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To access a single element of `seq` immediately after index `ind`, use `seq[ind]`.\n", "\n", "__Exercise__: If `s = \"abcdefg\"`, what are the outputs to the following code?\n", "- print(s[0])\n", "- print(s[6])\n", "- print(s[7])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s = \"Python\"\n", "print(s[:])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The slice notation specifies two index positions separated by a colon (`:`) to access subsequences.