{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Introduction to the Python language" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "We introduce here the Python language. Only the bare minimum\n", "necessary for getting started with Numpy and Scipy is addressed here.\n", "To learn more about the language, consider going through the\n", "excellent tutorial http://www.scipy-lectures.org/intro/index.html. Dedicated books\n", "are also available, such as http://www.diveintopython.net/.\n", "\n", "Python is a **programming language**, as are C, Fortran, BASIC, PHP,\n", "etc. Some specific features of Python are as follows:\n", "\n", "* an *interpreted* (as opposed to *compiled*) language. Contrary to e.g.\n", "C or Fortran, one does not compile Python code before executing it. In\n", "addition, Python can be used **interactively**: many Python\n", "interpreters are available, from which commands and scripts can be\n", "executed.\n", "\n", "* a free software released under an **open-source** license: Python can\n", "be used and distributed free of charge, even for building commercial\n", "software.\n", "\n", "* **multi-platform**: Python is available for all major operating\n", "systems, Windows, Linux/Unix, MacOS X, most likely your mobile phone\n", "OS, etc.\n", "\n", "* a very readable language with clear non-verbose syntax\n", "\n", "* a language for which a large variety of high-quality packages are\n", "available for various applications, from web frameworks to scientific\n", "computing.\n", "\n", "* a language very easy to interface with other languages, in particular C\n", "and C++.\n", "\n", "* Some other features of the language are illustrated just below. For\n", "example, Python is an object-oriented language, with dynamic typing\n", "(the same variable can contain objects of different types during the\n", "course of a program).\n", "\n", "\n", "See https://www.python.org/about/ for more information about\n", "distinguishing features of Python." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Hello world\n", "\n", "you type in the cells, execute commands with shift + Enter\n", "\n", "```print(\"Hello world\")```" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print(\"Hello world\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Basic types\n", "\n", "## Integers" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "1+1" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "We can assign values to variables with `=`\n", "\n", " a = 4\n", " type(a)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "a = 4" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "type(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that one does not declare the type of a variable before assigning its value. In C, conversely, one should write:\n", "\n", "```int a = 4;```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Floats\n", "There exists a floating point type that is created when the variable has decimal values:\n", "\n", " c = 2.1\n", " type(c)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "c = 2.1" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "type(c)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Boolean\n", "Similarly, boolean types are created from a comparison:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "3 > 4" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " test = (3 > 4)\n", " type(test)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "test = (3 > 4)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "type(test)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "_Tip_:A Python shell can therefore replace your pocket calculator, with the basic arithmetic operations ``+``, ``-``, ``*``, ``/``, ``%`` (modulo) natively implemented" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Type conversion (casting)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "a = 1\n", "type(a)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "b = float(a)\n", "type(b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise <^^>: Compute the floating point expression of 3 / 2" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Containers\n", "\n", "_Tip_: Python provides many efficient types of containers, in which collections of objects can be stored.\n", "\n", "## Lists\n", "\n", "A list is an ordered collection of objects, that may have different types. For example:\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " colors = ['red', 'blue', 'green', 'black', 'white']\n", " type(colors)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "colors = ['red', 'blue', 'green', 'black', 'white']" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "type(colors)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Indexing: accessing individual objects contained in the list::" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "colors[2]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "