python-course/1_Introduction.ipynb

479 строки
26 KiB
Plaintext
Исходник Постоянная ссылка Обычный вид История

{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"# Introduction to Programming with Python\n",
"\n",
"# Unit 1: Introduction\n",
"\n",
2021-11-20 01:34:17 +03:00
"Hello, and welcome to the *Introduction to Programming with Python* course! \n",
"\n",
2021-11-20 01:34:17 +03:00
"In this course, you will learn the basics of computer programming using one of the most popular and powerful languages: [Python](https://www.python.org/). We will try to make this experience enjoyable, learning how to create some nice visual drawings and some computer-generated art. Despite the fact that in this course we will be solving relatively simple problems, Python as a language is very powerful, and can be used in may areas, from Data Science and AI to developing web sites.\n",
"\n",
2021-11-20 01:34:17 +03:00
"The document you are reading now is called a **Notebook** (more precisely, a *Jupyter Notebook*). It contains **cells**, and some of them are **text cells** (they are also called **Markdown** in the drop-down box at the top, because they can contain some formatting in so-called Markdown format), and some are **executable code**. To execute the code, select a cell and click **RUN** button in the top toolbox, or press **Shift-Enter**. \n",
"\n",
2021-11-20 01:34:17 +03:00
"Try executing the next cell which contains the code to compute how many hours there are in a year:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"trusted": true
},
"outputs": [
{
"data": {
"text/plain": [
"8760"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"365*24"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2021-11-20 01:34:17 +03:00
"Once you execute the code, you can see the result immediately following the cell. Sometimes, you also see the result from the previous execution because those results are stored together with the document. The order in which you execute cells is important, because if you compute a result in one cell it might later be used in another cell. Most of the notebooks are written in such a way that you should be able to execute all cells in order from top to bottom without any problems.\n",
"\n",
2021-11-20 01:34:17 +03:00
"You can insert new cells using **+** button at the top, and you should be able to edit this document at any time. \n",
"\n",
2021-11-20 01:34:17 +03:00
"> **If you are not able to edit the document** it means you are looking at the original collection of notebooks and not at your own copy. You should **CLONE** this collection in GitHub to get your own editable copy.\n",
"\n",
"As a first excercise, write the code in the empty cell below to compute the number of minutes in a year. Once you are done - press **RUN** or **Shift-Enter** to execute your code and see the result:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"trusted": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Simplest Program\n",
"\n",
2021-11-20 01:34:17 +03:00
"As you can see, the simplest Python code is just an arithmetic expression, which is computed and printed on the screen. In general, **a program is a series of commands** called **operators**. For example, one of the operators is called `print`, and it can be used to print something. When you learn programming, usually you start with writing a program that prints *Hello, world!* message, so this program in Python looks like this:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"trusted": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello, world!\n"
]
}
],
"source": [
"print(\"Hello, world!\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In our case, it is not really a program, but a part of a notebook. However, you can write a separate Python program by putting the text into a text file with `.py` extension. For example, if you create a file called `hello.py`, containing the line above with the `print` command, you can then run the program using the following command and see the message on the screen:\n",
"```sh\n",
"python hello.py\n",
"```\n",
"\n",
"<img src=\"./images/pythonhelloconsole.gif\"/>\n",
"\n",
2021-11-20 01:34:17 +03:00
"For this command to work, you need to have Python installed on your computer and you need to be able to use the *command-line interface*, which is kind of complicated for our purposes. So don't worry about that for now, just keep in mind that Python is not a toy language in which you can only program small *cells*, but rather a full-scale programming language in which you can write almost anything. And many applications, including web sites or games, would be written in a slightly different way using some sort of *development environment*, and not a notebook. However, no matter the authoring environment, the Python language itself is always the same, and that is exactly what we will be learning."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Variables\n",
"\n",
2021-11-20 01:34:17 +03:00
"Like in mathematics, you can use *names* to associate them with some data. For example, you can define `seconds_in_a_day` to be equal to the number of seconds in one day, and then you can use this value in your calculations:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"trusted": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
2021-11-20 01:34:17 +03:00
"Seconds in a year: 31536000\n"
]
}
],
"source": [
"seconds_in_a_day = 24*60*60\n",
2021-11-20 01:34:17 +03:00
"print('Seconds in a year: ',seconds_in_a_day*365)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2021-11-20 01:34:17 +03:00
"Those named values are called **variables**, and you can visualize a variable as a kind of bucket that can store **values** of different kinds (called **types**). The most common types are:\n",
"\n",
" * Integer values (1,2,13,...)\n",
" * Real (or floating point) values: 3.1415, 0.5\n",
" * Strings: \"hello\"\n",
" \n",
"For example, we can use a variable to store our name, and extend the *hello-world* example:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"trusted": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello, Peter\n"
]
}
],
"source": [
"name = \"Peter\"\n",
"print(\"Hello, \"+name)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We use the `+` operation here to add (or, as programmers say, *concatenate*) two strings together. Adding strings is not the same as adding numbers, as you can see from the example below:"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"trusted": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"137\n",
"20\n"
]
}
],
"source": [
"print(\"13\"+\"7\")\n",
"print(13+7)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To summarize:\n",
" * When you write `\"13\"`, or `'13'` - it means a string of two symbols, `1` and `3`\n",
" * When you write 13 - it means a number 13\n",
" * When you write `\"Paul\"` or `'Paul'` - it means a string of 4 symbols\n",
" * When you write `Paul` - it means a variable named *Paul*, and it will be replaced by the value of the variable \n",
" \n",
"Try to guess what happens, when you want to add string `\"13\"` to a number 1. Or vice versa. Let us do some experimentation:"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"trusted": true
},
"outputs": [
{
"ename": "TypeError",
"evalue": "unsupported operand type(s) for +: 'int' and 'str'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m/var/folders/m5/_8096psx5dn9lt75fj85cc380000gn/T/ipykernel_94923/1279102277.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;36m1\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0;34m\"13\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'int' and 'str'"
]
}
],
"source": [
"1+\"13\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2021-11-20 01:34:17 +03:00
"You see an example of an **Error message**. This message points to the exact piece of code that causes the error, and it also gives an explanation. For example, this error tells us that the operation `+` does not support operands of types `int` and `str`, i.e. it does not know how to add strings and numbers together, because this operation does not make sense."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Modules and Libraries\n",
"\n",
2021-11-20 01:34:17 +03:00
"The core Python programming language contains the basic commands, such as printing, doing calculations, and other tasks. For more complex things, like downloading things from the internet or creating applications, there are special **libraries** (they are also called **modules**, or **packages**), that extend the language.\n",
"\n",
2021-11-20 01:34:17 +03:00
"For example, the `math` module contains a lot of mathematical functions. To use this module, we need to **import** it to our program by typing `import math`:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"trusted": true
},
"outputs": [
{
"data": {
"text/plain": [
"1.0"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import math\n",
"math.sin(math.pi/2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2021-11-21 05:35:21 +03:00
"Some modules (like `math`) are built into any Python distribution while others are distributed through the centralized package repository [PyPI](http://pypi.org). You can install any package from that repository using the special operating system command `pip` or, for use with Python3, `pip3`. We need to install the library for turtle graphics that we will see in the next section, so run the following command to install it:"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Defaulting to user installation because normal site-packages is not writeable\n",
"Collecting jturtle\n",
" Using cached jturtle-0.0.3-py3-none-any.whl (4.1 kB)\n",
"Requirement already satisfied: matplotlib in /Users/jenniferlooper/Library/Python/3.8/lib/python/site-packages (from jturtle) (3.5.0)\n",
"Requirement already satisfied: fonttools>=4.22.0 in /Users/jenniferlooper/Library/Python/3.8/lib/python/site-packages (from matplotlib->jturtle) (4.28.1)\n",
"Requirement already satisfied: kiwisolver>=1.0.1 in /Users/jenniferlooper/Library/Python/3.8/lib/python/site-packages (from matplotlib->jturtle) (1.3.2)\n",
"Requirement already satisfied: pillow>=6.2.0 in /Users/jenniferlooper/Library/Python/3.8/lib/python/site-packages (from matplotlib->jturtle) (8.4.0)\n",
"Requirement already satisfied: numpy>=1.17 in /Users/jenniferlooper/Library/Python/3.8/lib/python/site-packages (from matplotlib->jturtle) (1.21.4)\n",
"Requirement already satisfied: setuptools-scm>=4 in /Users/jenniferlooper/Library/Python/3.8/lib/python/site-packages (from matplotlib->jturtle) (6.3.2)\n",
"Requirement already satisfied: cycler>=0.10 in /Users/jenniferlooper/Library/Python/3.8/lib/python/site-packages (from matplotlib->jturtle) (0.11.0)\n",
"Requirement already satisfied: python-dateutil>=2.7 in /Users/jenniferlooper/Library/Python/3.8/lib/python/site-packages (from matplotlib->jturtle) (2.8.2)\n",
"Requirement already satisfied: pyparsing>=2.2.1 in /Users/jenniferlooper/Library/Python/3.8/lib/python/site-packages (from matplotlib->jturtle) (2.4.7)\n",
"Requirement already satisfied: packaging>=20.0 in /Users/jenniferlooper/Library/Python/3.8/lib/python/site-packages (from matplotlib->jturtle) (21.2)\n",
"Requirement already satisfied: six>=1.5 in /Users/jenniferlooper/Library/Python/3.8/lib/python/site-packages (from python-dateutil>=2.7->matplotlib->jturtle) (1.16.0)\n",
"Requirement already satisfied: setuptools in /Users/jenniferlooper/Library/Python/3.8/lib/python/site-packages (from setuptools-scm>=4->matplotlib->jturtle) (59.1.1)\n",
"Requirement already satisfied: tomli>=1.0.0 in /Users/jenniferlooper/Library/Python/3.8/lib/python/site-packages (from setuptools-scm>=4->matplotlib->jturtle) (1.2.2)\n",
"Installing collected packages: jturtle\n",
"Successfully installed jturtle-0.0.3\n",
"Note: you may need to restart the kernel to use updated packages.\n"
]
}
],
"source": [
"pip install jturtle"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Turtle Graphics\n",
"\n",
2021-11-21 05:35:21 +03:00
"The library we have just installed allows us to use some simple drawing commands: so-called **Turtle graphics**. Those commands control an imaginary *turtle*, which can move across the screen, turn around, and so on, leaving the trace behind. For example, if I want to draw a triangle, I can use the following commands:"
]
},
{
"cell_type": "code",
2021-11-21 05:35:21 +03:00
"execution_count": 13,
"metadata": {
"trusted": true
},
"outputs": [
{
2021-11-21 05:35:21 +03:00
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAOcAAADnCAYAAADl9EEgAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8/fFQqAAAACXBIWXMAAAsTAAALEwEAmpwYAAAPx0lEQVR4nO3defCdZXnG8e8VwhICstSIQFXGwULCFvbFIgrSFrEWpQqlVattseoUSiAgFuugIvuqsggDAqIie8tW9sWwyhYCgVTWMrWLTAHZt6t/PAfEmPyS/PI753nOOddnhhlmMnDuf6689/O+9/2+sk1EtGdc7QIiYt4SzohGJZwRjUo4IxqVcEY0KuGMaFTCGdGohDOiUQlnRKMSzohGJZwRjUo4IxqVcEY0KuGMaFTCGdGohDOiUQlnRKMSzohGJZwRjUo4IxqVcEY0KuGMaFRPwilp+V78TsQg6Xo4Ja0NPCPpy5LGd/v3IgZF18Np+4HOv+4M3C1pu27/ZsQgUC/e+C7JlL8IdgKOBO4B9rH9UNd/PKJP9eyGkIsLgCnAbcCtkg7JeTRi3np+t9b2i7YPBtYH3gk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OVmz+a2H6paVPyOhHPISFqP8rxyFWB321fVrSjmJ23tkJD0dknHA1cB51HuwiaYDUs4B5ykJSXtQVnlepXyvPJ4269WLi0WIG3tAJP0x5RVrieAD9m+r3JJsQgSzgEk6Q8oN3vWBqYBF2eyp/+krR0gklaQdDhwE+XLXOva/tcEsz8lnAOgs8r1N5RVrpUpoTzc9kuVS4vFkLa2z0namjJy9zzwUdt3VC4pxkjC2ackv
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"import jturtle as turtle\n",
"turtle.forward(100)\n",
"turtle.right(120)\n",
"turtle.forward(100)\n",
"turtle.right(120)\n",
"turtle.forward(100)\n",
"turtle.right(120)\n",
"turtle.done()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2021-11-21 05:35:21 +03:00
"First of all, the command `import jturtle as turtle` imports the module and changes the name to `turtle` (this name sounds a bit nicer). \n",
"\n",
"> If you want to save on typing, you can say `import jturtle as T`, and use `T` instead of `turtle`.\n",
"\n",
"Commands like `turtle.forward` tell turtle to do something. The most important commands are:\n",
2021-11-21 05:35:21 +03:00
" \n",
" * `turtle.forward(n)` -- move `n` units forward (the turtle moves up)\n",
" * `turtle.backward(n)` -- move `n` units backwards (the turtle moves down)\n",
" * `turtle.left(n)` and `turtle.right(n)` -- turn `n` degrees left or right\n",
2021-11-21 05:35:21 +03:00
" * `turtle.penup()` and `turtle.pendown()` allows you to control whether the turtle leaves the trace behind or not. You need to use those commands if you want to draw several figures not connected to each other\n",
" * `turtle.done()` tells the turtle that the drawing is over, and it should be displayed on the screen\n",
" \n",
2021-11-21 05:35:21 +03:00
" > If you want to see how the drawing was created step-by-step, you can use `turtle.done(step_by_step=True)`, or just `turtle.done(True)`:"
]
},
{
"cell_type": "code",
2021-11-21 05:35:21 +03:00
"execution_count": 21,
"metadata": {
"trusted": true
},
"outputs": [
{
"data": {
2021-11-21 05:35:21 +03:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAV0AAABXCAYAAACnZJZlAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8/fFQqAAAACXBIWXMAAAsTAAALEwEAmpwYAAABf0lEQVR4nO3cQYrCQBBA0ZR4/yuXCw8wIvojk/f2DUURfkMvMrt7ANC4nT0AwJWILkBIdAFCogsQEl2AkOgChEQXICS6ACHRBQiJLkBIdAFCogsQur9zaGZ2d+fTw/A0M5f7C1H1PV1xt8fR7NduX/NWdPk+l9r3XG23ZQzt9m+eFwBCogsQEl2AkOgChEQXICS6ACHRBQiJLkBIdAFCogsQEl2AkOgChEQXICS6ACHRBQiJLkBIdAFCogsQEl2AkOgChEQXICS6ACHRBQiJLkBIdAFCogsQEl2AkOgChEQXICS6ACHRBQiJLkBIdAFCogsQEl2AkOgChEQXICS6ACHRBQiJLkBIdAFCogsQEl2AkOgChEQXICS6ACHRBQiJLkBIdAFCogsQEl2AkOgChEQXICS6ACHRBQiJLkBIdAFCogsQEl2A0P3sAYD/Y2b27Bl+3ezaEUDF8wJASHQBQqILEBJdgJDoAoREFyAkugAh0QUIiS5ASHQBQqILEHoAau4irQdpshUAAAAASUVORK5CYII=",
"text/plain": [
"<Figure size 432x288 with 4 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
2021-11-21 05:35:21 +03:00
"# go up\n",
"turtle.forward(100)\n",
2021-11-21 05:35:21 +03:00
"# turn right\n",
"turtle.right(90)\n",
2021-11-21 05:35:21 +03:00
"# go forward\n",
"turtle.forward(100)\n",
2021-11-21 05:35:21 +03:00
"# turn right\n",
"turtle.right(90)\n",
2021-11-21 05:35:21 +03:00
"# go down\n",
"turtle.forward(100)\n",
2021-11-21 05:35:21 +03:00
"# turn right\n",
"turtle.right(90)\n",
2021-11-21 05:35:21 +03:00
"# go left\n",
"turtle.forward(100)\n",
2021-11-21 05:35:21 +03:00
"# show each step separately\n",
"turtle.done(True)"
]
},
{
"cell_type": "markdown",
"metadata": {
"trusted": false
},
"source": [
"Here, we can see that the turtle first moved up 100 points, then turned right, went forward for another 100 points, and so on. Repeating those actions 4 times we end up with a square.\n",
"\n",
2021-11-21 05:35:21 +03:00
"Those were very simple drawings, but as we will see later, computers are very good at repeating and composing together simple steps to get more complex results. We will see some examples of very beautiful drawings created using the same approach of turtle graphics."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2021-11-21 05:35:21 +03:00
"## 🚀 Challenges\n",
"\n",
"Before going to the next chapter, try one of these challenges.\n",
"\n",
"### A Math Challenge\n",
"Suppose you put a certain amount of money into a bank deposit (say, \\$100). This amount increases at the rate 2.5% per year. Calculate the amount that you will have after 1, 2 and 3 years.\n",
"\n",
2021-11-21 05:35:21 +03:00
"### An Art Challenge \n",
"\n",
2021-11-21 05:35:21 +03:00
"To practice turtle graphics, draw a simple house, like the one shown below. Please, note that in order to move the turtle without leaving a trace, you need to execute `turtle.penup()` command.\n",
"\n",
"![Simple House](./images/house.png)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"trusted": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"interpreter": {
"hash": "767d51c1340bd893661ea55ea3124f6de3c7a262a8b4abca0554b478b1e2ff90"
},
"kernelspec": {
"display_name": "Python 3.7.4 64-bit ('base': conda)",
"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.8.9"
}
},
"nbformat": 4,
"nbformat_minor": 2
}