{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Python basics" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Nikolay Koldunov\n", "\n", "\n", "koldunovn@gmail.com" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is part of [**Python for Geosciences**](https://github.com/koldunovn/python_for_geosciences) notes." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "================" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Variables" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python uses [duck typing](http://en.wikipedia.org/wiki/Duck_typing)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Int" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "a = 10" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Float" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10.7" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "z = 10.7\n", "z" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "float" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(z)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### String" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'2'" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b = '2'\n", "b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Some operations are not allowed on different types:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "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\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0ma\u001b[0m\u001b[0;34m+\u001b[0m\u001b[0mb\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": [ "a+b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "But some of them are allowed:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'2222222222'" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a*b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Might be a source of confusion :)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "String variables can be combined:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "' guys walk into a bar'" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c = ' guys walk into a bar'\n", "c" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'2 guys walk into a bar'" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b+c" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In order to include variable of another type in to string you have to convert it:" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'10 guys walk into a bar'" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "str(a)+c" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Everything is an object" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In IPython you can get the list of object's methods and attributes by typing dot and pressing TAB:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "c." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Methods are basically default functions that can be applied to our variable:" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "' GUYS WALK INTO A BAR'" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c.upper()" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "' Guys Walk Into A Bar'" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c.title()" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c.count('a')" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "11" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c.find('into')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you need help on method in IPython type something like:" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "c.find?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Or open bracket and press SHIFT+TAB:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "c.find(" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Int variable is also an object:" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.bit_length()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Methods can be combined (kind of a pipeline)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c.title().count('a').bit_length()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Lists" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are several other interesting variable types in Python, but the one we would need the most is the list." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In order to create list put coma separated values in square brackets:" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5]" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l = [1,2,3,4,5]\n", "l" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Sort of similar to Matlab variables, but not exactly." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Values in list can be any type:" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['one', 'two', 'three', 'four', 'five']" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l = ['one', 'two', 'three', 'four', 'five']\n", "l" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Combined" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['one', 2, 'three', 4.0, 5]" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l = ['one', 2, 'three', 4.0, 3+2]\n", "l" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Any type means ANY type:" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['one', 2, 'three', [1, 2, 3, 4, 5], 5]" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l = ['one', 2, 'three', [1,2,3,4,5], 3+2]\n", "l" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can access list values by index:" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'one'" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l[0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Oh, yes, indexing starts with zero, so for Matlab users the zero is the new one :) See discussion on the matter [here](http://en.wikipedia.org/wiki/Zero-based_numbering)." ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l[1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's have a look at the 4th element of our list:" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5]" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l[3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It's also a list, and its values can be accessed by indexes as well:" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l[3][4]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You also can acces multiple elements of the list using slices:" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['one', 2, 'three', [1, 2, 3, 4, 5], 5]" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2, 'three']" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l[1:3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Slice will start with the first slice index and go up to but not including the second slice index. " ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5]" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l[3]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Control Structures" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### For loop:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This loop will print all elements from the list *l*" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "one\n", "2\n", "three\n", "[1, 2, 3, 4, 5]\n", "5\n" ] } ], "source": [ "l = ['one', 2, 'three', [1,2,3,4,5], 3+2]\n", "\n", "for element in l:\n", " print(element)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Two interesting thins here. First: indentation, it's in the code, you must use it, otherwise code will not work:" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "ename": "IndentationError", "evalue": "expected an indented block (, line 2)", "output_type": "error", "traceback": [ "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m2\u001b[0m\n\u001b[0;31m print(element)\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mIndentationError\u001b[0m\u001b[0;31m:\u001b[0m expected an indented block\n" ] } ], "source": [ "for element in l:\n", "print(element)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Second - you can iterate through the elements of the list. There is an option to iterate through a bunch of numbers as we used to in Matlab:" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "one\n", "2\n", "three\n", "[1, 2, 3, 4, 5]\n", "5\n" ] } ], "source": [ "for index in range(5):\n", " print(l[index])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "where *range* is just generating a sequence of numbers:" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 2, 3, 4]" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(range(5))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Branches" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We are not going to use branches in this notes, but this is how they look like just as another example of indentation use:" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Freezing\n" ] } ], "source": [ "x = -1\n", "if x > 0:\n", " print(\"Melting\")\n", "elif x == 0:\n", " print(\"Zero\")\n", "else:\n", " print(\"Freezing\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Modules" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Pure python does not do much. To do some specific tasks you need to import modules. Here I am going to demonstrate several ways to do so." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The most common one is to import complete library. In this example we import *urllib2* - a library for opening URLs using a variety of protocols." ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [], "source": [ "import requests" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here we get information from [FESOM](http://fesom.de/) website site. Note how function *get* is called. We have to use name of the library, then dot, then name of the function from the library:" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'Date': 'Tue, 28 Apr 2020 15:58:46 GMT', 'Server': 'Apache', 'Content-Language': 'en', 'Vary': 'Accept-Encoding', 'Content-Encoding': 'gzip', 'Cache-Control': 'max-age=0', 'Expires': 'Tue, 28 Apr 2020 15:58:46 GMT', 'X-UA-Compatible': 'IE=edge', 'X-Content-Type-Options': 'nosniff', 'Content-Length': '7375', 'Keep-Alive': 'timeout=3, max=500', 'Connection': 'Keep-Alive', 'Content-Type': 'text/html; charset=utf-8'}" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "response = requests.get('http://fesom.de/')\n", "response.headers" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Another option is to import it like this:" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [], "source": [ "from requests import *" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this case all functions will be imported in to the name-space and you can use *get* directly, without typing the name of the library first:" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'Date': 'Tue, 28 Apr 2020 15:58:48 GMT', 'Server': 'Apache', 'Content-Language': 'en', 'Vary': 'Accept-Encoding', 'Content-Encoding': 'gzip', 'Cache-Control': 'max-age=0', 'Expires': 'Tue, 28 Apr 2020 15:58:48 GMT', 'X-UA-Compatible': 'IE=edge', 'X-Content-Type-Options': 'nosniff', 'Content-Length': '7375', 'Keep-Alive': 'timeout=3, max=500', 'Connection': 'Keep-Alive', 'Content-Type': 'text/html; charset=utf-8'}" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "response = get('http://fesom.de/')\n", "response.headers" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "But generally this is very bad idea and is not recomended, because your name-space is populated by things that you don't really need and it's hard to tell where the function comes from." ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Variable Type Data/Info\n", "---------------------------------------------------\n", "ConnectTimeout type \n", "ConnectionError type \n", "DependencyWarning type \n", "FileModeWarning type \n", "HTTPError type \n", "NullHandler type \n", "PreparedRequest type \n", "ReadTimeout type \n", "Request type \n", "RequestException type \n", "RequestsDependencyWarning type questsDependencyWarning'>\n", "Response type \n", "Session type \n", "Timeout type \n", "TooManyRedirects type \n", "URLRequired type \n", "a int 10\n", "adapters module es/requests/adapters.py'>\n", "api module ackages/requests/api.py'>\n", "auth module ckages/requests/auth.py'>\n", "b str 2\n", "c str guys walk into a bar\n", "certs module kages/requests/certs.py'>\n", "chardet module ges/chardet/__init__.py'>\n", "check_compatibility function \n", "codes LookupDict \n", "compat module ages/requests/compat.py'>\n", "cookies module ges/requests/cookies.py'>\n", "cryptography_version str 2.9.2\n", "delete function \n", "element int 5\n", "exceptions module /requests/exceptions.py'>\n", "get function \n", "head function \n", "hooks module kages/requests/hooks.py'>\n", "index int 4\n", "l list n=5\n", "logging module 3.7/logging/__init__.py'>\n", "models module ages/requests/models.py'>\n", "options function \n", "packages module es/requests/packages.py'>\n", "patch function \n", "post function \n", "put function \n", "pyopenssl module b3/contrib/pyopenssl.py'>\n", "request function \n", "requests module es/requests/__init__.py'>\n", "response Response \n", "session function \n", "sessions module es/requests/sessions.py'>\n", "status_codes module equests/status_codes.py'>\n", "structures module /requests/structures.py'>\n", "urllib3 module ges/urllib3/__init__.py'>\n", "utils module kages/requests/utils.py'>\n", "warnings module b/python3.7/warnings.py'>\n", "x int -1\n", "z float 10.7\n" ] } ], "source": [ "whos" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can import only function that you need:" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [], "source": [ "from requests import get" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'Date': 'Tue, 28 Apr 2020 15:58:50 GMT', 'Server': 'Apache', 'Content-Language': 'en', 'Vary': 'Accept-Encoding', 'Content-Encoding': 'gzip', 'Cache-Control': 'max-age=0', 'Expires': 'Tue, 28 Apr 2020 15:58:50 GMT', 'X-UA-Compatible': 'IE=edge', 'X-Content-Type-Options': 'nosniff', 'Content-Length': '7375', 'Keep-Alive': 'timeout=3, max=500', 'Connection': 'Keep-Alive', 'Content-Type': 'text/html; charset=utf-8'}" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "response = get('http://fesom.de/')\n", "response.headers" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Or import library as alias in order to avoid extensive typing:" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [], "source": [ "import requests as rq" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'Date': 'Tue, 28 Apr 2020 15:58:51 GMT', 'Server': 'Apache', 'Content-Language': 'en', 'Vary': 'Accept-Encoding', 'Content-Encoding': 'gzip', 'Cache-Control': 'max-age=0', 'Expires': 'Tue, 28 Apr 2020 15:58:51 GMT', 'X-UA-Compatible': 'IE=edge', 'X-Content-Type-Options': 'nosniff', 'Content-Length': '7375', 'Keep-Alive': 'timeout=3, max=500', 'Connection': 'Keep-Alive', 'Content-Type': 'text/html; charset=utf-8'}" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "response = rq.get('http://fesom.de/')\n", "response.headers" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Links:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[Dive Into Python](https://diveintopython3.problemsolving.io/)" ] } ], "metadata": { "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.7.6" } }, "nbformat": 4, "nbformat_minor": 1 }