{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Python 1-0-1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Printing a string" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Printing a string is possible with the _print( )_ function." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Let's print some text!\n" ] } ], "source": [ "print(\"Let's print some text!\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__Excercise:__ Write a code that will print your name." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Variables" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In Python types of variables don't need to be set. Let's create two different type of variables and print them." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "450\n", "Welcome to Python. It won't bite!\n" ] } ], "source": [ "count = 450\n", "words = \"Welcome to Python. It won't bite!\"\n", "\n", "print(count)\n", "print(words)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__Excercise:__ Save a number 900 to a variable (you can name it whatever you like). After that print a number that one will get when one adds a number 100 to the variable that you created." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Calculations" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Simple calculations can be done straight in the code. With more complex calculations the modules can be used." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "8" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 + 5" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "15" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 * 5" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1.4\n", "1.4\n" ] } ], "source": [ "# 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)\n", "\n", "from __future__ import division\n", "print(7 / 5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Functions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Functions are subprograms that can be called to do some specific task that they are made to do. For example let's use the function _abs( )_ that will calculate the absolute value of the argument given to it. Let's also use the function _len( )_ that will return the length of the argument given to it." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "abs(-9)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "20" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(\"Particle accelerator\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__Excercise:__ Try the function _len( )_ and define the number of the characters in your name with it. Does the function count also spaces?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Creating own functions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It is also possible to create own functions. Let's create a function _squaring( )_ that will calculate and return a square root of the argument that is given to it and print a text \"You just used the function.\" Let's use that function for the number 4." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "16\n", "You just used the function.\n" ] } ], "source": [ "def squaring(a):\n", " print(a*a)\n", " print(\"You just used the function.\")\n", " \n", "squaring(4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "__Excercise:__ Create a function that will multiply the argument given to it by the number 2." ] }, { "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 }