{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Visto tipi numero intero e stringa, vediamo anche i numeri reali, sono indicati con float" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "5.0" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "float(5)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "19.666666666666668" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "59/3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python vecchio divisione fra interi dava intero, ora da un float, per fare divisione intero\n", "uso // e per il modulo uso %" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "59//60" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "59" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "59%60" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### FUNZIONI\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Accetta qualcosa in ingresso, restituisci qualcosa in uscita e può avere degli effetti collaterali. NB: restituire un valore significa darlo disponibile per il programma, print per esempio non restituisci nessun valore ma ha come effetto collaterlae di stampare" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python ha sia un approcio matematico, nomeFunz(nomeVar), che quello ad oggetti nomeVar.nomeFunz(). Metodo principalmente usato dagli altri linguaggi di programmazione!" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": true }, "outputs": [], "source": [ "s = \"stringa\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "s è istanza della classe stringa. schiacciando il punto dopo s \n", "e tab vi viene fuori una lista di funzioni della classe stringa \n", "che potete usare. Dilettatevi.\n", "Per voi una classe è un insieme di oggetti che hanno delle caratteristiche comuni e \n", "hanno dei metodi predefiniti specifici. \n", "Ricordarsi le parentesi dopo il nome del metodo" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.isdecimal()" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on built-in function isdecimal:\n", "\n", "isdecimal(...) method of builtins.str instance\n", " S.isdecimal() -> bool\n", " \n", " Return True if there are only decimal characters in S,\n", " False otherwise.\n", "\n" ] } ], "source": [ "help(s.isdecimal)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": true }, "outputs": [], "source": [ "a='10'" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a.isdecimal()" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": true }, "outputs": [], "source": [ "x='1'" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ciao\n" ] } ], "source": [ "b = print('ciao')" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "collapsed": true }, "outputs": [], "source": [ "b" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x.isdecimal()" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on built-in function isdecimal:\n", "\n", "isdecimal(...) method of builtins.str instance\n", " S.isdecimal() -> bool\n", " \n", " Return True if there are only decimal characters in S,\n", " False otherwise.\n", "\n" ] } ], "source": [ "help(x.isdecimal)" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'sfringa'" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.replace('t','f')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "I metodi sulle stringhe creano nuove stringhe, la stringa non viene modificata" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'stringa'" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Vedremo più avanti come definire noi una funzione" ] }, { "cell_type": "code", "execution_count": 89, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 89, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.count('a')" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "-1" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.find('o') \n", "# indice numerico di r sulla stringa s, prima volta che lo trova" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### LIBRERIE\n", "Per usare le librerie si usa il comando import" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import math" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Come per i metodi sulle stringhe, mettendo il punto e tab posso vedere tutti i metodi dentro a math" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "1.0" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "math.cos(0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Per non dover ripetere math ogni volta che voglio una funzione di math si usa il comando qui sotto" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from math import *" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Dove * significa tutto" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "1.0" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cos(0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Posso fare composizione di funzioni" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "0.0" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "acos(cos(0))" ] }, { "cell_type": "code", "execution_count": 36, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import random" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "112" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ord('p') # posizione dove è salvato il carattere p" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'p'" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "chr(112) # carattere salvato in posizione 112" ] }, { "cell_type": "code", "execution_count": 38, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from random import *" ] }, { "cell_type": "code", "execution_count": 41, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on method randint in module random:\n", "\n", "randint(a, b) method of random.Random instance\n", " Return random integer in range [a, b], including both end points.\n", "\n" ] } ], "source": [ "help(randint)" ] }, { "cell_type": "code", "execution_count": 41, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "randint(1,10)" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "### ESPRESSIONE BOOLEANA\n", "Il tipo booleano ha dominio {True, False}, è un dominio finito. Operatori: and, or, not.\n", "NB: in python un'espressione vera ha valore 1, la falsa ha valore zero, in più ogni valore diverso da zero viene considerato vero. Questo può generare errori. \n" ] }, { "cell_type": "code", "execution_count": 42, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5 and True" ] }, { "cell_type": "code", "execution_count": 43, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "0 and True" ] }, { "cell_type": "code", "execution_count": 46, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on built-in function isalpha:\n", "\n", "isalpha(...) method of builtins.str instance\n", " S.isalpha() -> bool\n", " \n", " Return True if all characters in S are alphabetic\n", " and there is at least one character in S, False otherwise.\n", "\n" ] } ], "source": [ "help(s.isalpha)" ] }, { "cell_type": "code", "execution_count": 44, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'stringa'" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s" ] }, { "cell_type": "code", "execution_count": 47, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.isalpha()" ] }, { "cell_type": "code", "execution_count": 49, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "not False" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Operatori di confronto: operatori che ritornano un valore booleanio: >,<,>=,<=, ==,!=" ] }, { "cell_type": "code", "execution_count": 55, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5 == 7" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### IF ELSE ELIF\n", "Esecuzione condizionale. Primo costrutto di programmazione. " ] }, { "cell_type": "code", "execution_count": 56, "metadata": { "collapsed": false }, "outputs": [], "source": [ "x=5 \n", "y=-5" ] }, { "cell_type": "code", "execution_count": 64, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5 è maggiore di zero\n", "ciao\n" ] } ], "source": [ "if x>0:\n", " print(x, 'è maggiore di zero')\n", "print('ciao')" ] }, { "cell_type": "code", "execution_count": 66, "metadata": { "collapsed": true }, "outputs": [], "source": [ "y=5" ] }, { "cell_type": "code", "execution_count": 71, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5 è maggiore di zero\n", "ciao\n" ] } ], "source": [ "if y>0:\n", " print(y, 'è maggiore di zero')\n", "else:\n", " print(y, 'è minore o uguale a zero')\n", "print('ciao')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Solo in python si può usare insieme <,<, eg 0≤x≤5" ] }, { "cell_type": "code", "execution_count": 72, "metadata": { "collapsed": true }, "outputs": [], "source": [ "z = 7" ] }, { "cell_type": "code", "execution_count": 73, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "7 è compreso tra ]5,10]\n" ] } ], "source": [ "if 010:\n", " print(z, 'è maggiore di 10')\n", " else:\n", " print(z, 'è minore o uguale a zero')\n", " " ] }, { "cell_type": "code", "execution_count": 74, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "7 è compreso tra ]5,10]\n" ] } ], "source": [ "if 010:\n", " print(z, 'è maggiore di 10')\n", "else:\n", " print(z, 'è minore o uguale a zero')" ] }, { "cell_type": "code", "execution_count": 75, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "7 è compreso tra ]5,10]\n" ] } ], "source": [ "if z>5 and z <= 10:\n", " print(z, 'è compreso tra ]5,10]')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Confronto fra reali\n", "il numero di bit dedicato ai reali è finito, c'è un approssizmazione\n", "e spesso == non va bene" ] }, { "cell_type": "code", "execution_count": 76, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 76, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sqrt(2)**2==2" ] }, { "cell_type": "code", "execution_count": 80, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 80, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2.0=='s'" ] }, { "cell_type": "code", "execution_count": 77, "metadata": { "collapsed": false }, "outputs": [], "source": [ "epsilon = 1e-15" ] }, { "cell_type": "code", "execution_count": 78, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 78, "metadata": {}, "output_type": "execute_result" } ], "source": [ "abs(sqrt(2)**2-2) str\n", " \n", " Return a copy of the string S with leading and trailing\n", " whitespace removed.\n", " If chars is given and not None, remove characters in chars instead.\n", "\n" ] } ], "source": [ "help(s.strip)" ] }, { "cell_type": "code", "execution_count": 93, "metadata": { "collapsed": true }, "outputs": [], "source": [ "s = ' ciao io mi chiamo gigi '" ] }, { "cell_type": "code", "execution_count": 94, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'ciao io mi chiamo gigi'" ] }, "execution_count": 94, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s.strip()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Esercizio\n", "Fare un programma che prende in input 3 numeri interi e controlla se possono essere la lunghezza dei lati di un triangolo e controllare che tipo di triangolo è." ] }, { "cell_type": "code", "execution_count": 97, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on method raw_input in module ipykernel.kernelbase:\n", "\n", "raw_input(prompt='') method of ipykernel.ipkernel.IPythonKernel instance\n", " Forward raw_input to frontends\n", " \n", " Raises\n", " ------\n", " StdinNotImplentedError if active frontend doesn't support stdin.\n", "\n" ] } ], "source": [ "help(input)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "celltoolbar": "Raw Cell Format", "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.0" } }, "nbformat": 4, "nbformat_minor": 2 }