{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# The basics of Python programming" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this notebook the basics of Python programming are familiarized and basic examples are provided. Because programming is easiest to learn by doing, this notebook is recommended to be read with Jupyter Notebook (http://jupyter.org/). In Jupyter the examples in the notebook can be run and customated interactively. Also own notes and code can be added beside this text. There is also a short exercise at the end of this document.\n", "\n", "If you haven't installed Jupyter yet, you can also open the free test version at https://try.jupyter.org/." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Python – what is it?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python is a programming language which newest version at the moment is Python 3 released in 2008. Python is quite easy to learn since the syntax of it is relatively simple. However Python is capable for more complex programming too.\n", "\n", "In this notebook the basics of Python and some examples are introduced. The main point is to offer a compact overview of the most common things in Python in the sense of using it with CMS Open Data. This notebook won't cover every feature of Python. The official Python documentation is available at https://docs.python.org/3/." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Hello, world!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's start with the traditional \"Hello, world\" example. Printing in Python is possible with the _print( )_ function. The function takes a printable object (e.g. a string, a number) as an argument inside the brackets.\n", "\n", "Try to run the code cell below by first clicking it active and then pressing _Ctrl_ + _Enter_." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello, world!\n" ] } ], "source": [ "print('Hello, world!')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can try to write to the code cell below a code that will print your name. Try to run the code after writing." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# These #-separated lines are comments. They will not affect to the code.\n", "# Write a code that will print your name.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Variables in Python" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In programming one can save different values that are wanted to be used later to the __variables__. Different variable types are for example integer (int), floating-point number (float) and character string (string).\n", "\n", "In Python it is easy to create variables since user doesn't have to set the type of the variable. Only the name of the variable and the value to it are needed. For example let's create a variable named _particles_ and save the number 2700 to it:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "particles = 2700" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "After doing that we can use the variable e.g. with printing the value saved to it. Note that now there are no quote marks inside the brackets of the _print( )_ function. The quote marks determine a string but now we want to print a variable so we will not use the quote marks. Of course you can modify the code and test what happens if they are added." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2700\n" ] } ], "source": [ "print(particles)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Saving a string to a variable is done similarly. Let's create a variable called _welcoming_ and let's save the string \"Welcome to Python. Don't worry, it won't bite!\". Let's also print that." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Welcome to Python. Don't worry, it won't bite!\n" ] } ], "source": [ "welcoming = \"Welcome to Python. Don't worry, it won't bite!\"\n", "\n", "print(welcoming)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Calculating with Python" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "With Python it is possible to do many kinds of calculations. Simplest calculations work by writing the calculation in the code and then running the code. The code will then return the result of the calculation." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "8" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 + 5" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "-2\n", "15\n", "1.4\n" ] } ], "source": [ "print(3 - 5)\n", "print(3 * 5)\n", "\n", "# In Python 2 the result of the division is given as an integer. Python 3 can also return a float.\n", "# In Python 2 this can be fixed by writing \"from __future__ import division\" in the code cell before the calculation.\n", "\n", "print(7 / 5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In more complex calculations variables and lists can be used. In addition to the basic operations there are many ready made mathematical functions and commands available by using __modules__. The usage of modules is handled in the section _Modules_ below." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Functions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "With __functions__ it is possible to do more complex things than e.g. printing or calculating one value. The function is a subprogram that will be called in the actual program to do the wanted thing. Practically the function is a piece of code that will do the task that it's written to do.\n", "\n", "For example below we will recall the _abs( )_ function of the Python that will return the absolute value of the number (or the variable) given to it. The value given to the function is called __an argument__. " ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "abs(-2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The function _abs( )_ is an example of a built-in function of Python. Built-in functions are always available in Python. The list of the Python built-in functions can be found at https://docs.python.org/3/library/functions.html.\n", "\n", "It is also possible to define functions by oneself. With Python 3 this can be done with the _def_ command. Let's for example define a function that will calculate a square root of an argument given to it and also print a short text. Let's name our function as _squaring_." ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def squaring(a):\n", " print(a*a)\n", " print(\"You just used the function!\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we can use the function we created by calling it with the name of it and by giving some argument to it." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "16\n", "You just used the function!\n" ] } ], "source": [ "squaring(4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Modules" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are lots of different functions already done for different purposes. Often the built-in functions won't be enough, so more functions can be added with __modules__. Modules are practically code files that include functions.\n", "\n", "Modules can be imported with the _import_ command. For example let's import the _math_ module and use the function _sqrt( )_ of that module. When we call the function _sqrt( )_ we have to tell that we are calling it from the module _math_. This is done by writing the name of the module separated with the dot before the name of the function." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3.0" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import math\n", "\n", "math.sqrt(9)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "How is it then possible to know which modules there are? One list of the Python modules can be found at https://docs.python.org/3/py-modindex.html. Also lots of __packages__ can be used with Python. Packages include modules to Python. One list of different Python packages can be found at https://pypi.python.org/pypi.\n", "\n", "Web search is also useful when it is wanted to find out which modules and packages there are. With couple of search words it is easy to find a module or a package which will do the mission that is wanted." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Exercise – the basics of Python" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Write in the code cell below a program that will do the following steps:\n", "- create a variable _changing_ and save a string \"There are now text in our variable.\" to it\n", "- print the content of the variable _changing_\n", "- make the code change the value of the _changing_ to the number 8.256\n", "- print again the content of the variable _changing_" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "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.6.1" } }, "nbformat": 4, "nbformat_minor": 1 }