Top Banner
Introduction to Python Didzis Gosko
71

Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

Jan 23, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

IntroductiontoPython

DidzisGosko

Page 2: Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

Scriptinglanguage

FromWikipedia:Ascriptinglanguage orscriptlanguage isaprogramminglanguagethatsupportsscripts,programswrittenforaspecialrun-timeenvironmentthatcaninterpret(ratherthancompile)andautomatetheexecutionoftasksthatcouldalternativelybeexecutedone-by-onebyahumanoperator*....Theterm"scriptinglanguage"isalsousedlooselytorefertodynamichigh-levelgeneral-purposelanguage,suchasPerl,Tcl,andPython,withtheterm"script"oftenusedforsmallprograms(uptoafewthousandlinesofcode)insuchlanguages ...

*Pythonhasinteractiveshell

Page 3: Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

ShellScripting(bashetc.)vsPythonShellScripting(bashetc.):+ShellscriptinghassimplernotationsforI/Oredirection.+Itissimplertocreatepipelinesoutofexistingprogramsinshell.

+/- Uglysyntaxofcontrolflowstatements

- Mostoftasks(anydataprocessing)aredelegatedtoexternalprograms(awk,sed,perl,pythonetc.)- Extensiveuseofprocessforking- Compatibilityproblemsbetweenplatforms

Python:+Generalpurposeprogramminglanguage+Easy (elegant) syntax+Completetoolsetinamonolithpackage+Cross-platform+Powerful

+/- Blockindentation

- I/Oredirection,creatingpipelinesetc.notaseasyaswithShellScripting- Notallsystemshavepythoninstalled(it’salsotruewithbash)

Page 4: Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

BashvsPython

#!/bin/bash#commentlineecho"whatafineday:"date• Output,whencalledby

“./test.sh”:whatafineday:ThuOct2823:37:39EEST2004

#!/usr/bin/env python3#commentlineprint("whatafineday:")importosos.system("date")• Output,whencalledby

«./test.py»:whatafineday:ThuOct2823:37:39EEST2004

Page 5: Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

Eachtaskhasit’sowntool

ShellScripting:• programexecution,input/outputcapturingandchaining(with

pipes).

Python:• filecontent/dataprocessing(fromperspectiveofshell

scripting)• anyothertask...• canbeusedasscriptinglanguage(unlikeC/C++,Javaetc.)

IngeneralPythondoesn’tcomparewithShellScripting.

Page 6: Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

Pythonisgoodfor

• Conceptchecking• Fastprototyping• Anyperformancenon-essentialprogrammingtask

• Stringprocessing

• Areadable,dynamic,pleasant,• flexible,fastandpowerfullanguage

Page 7: Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

Overview

• Background• Syntax• Types/Operators/ControlFlow• Functions• Classes• Tools

Page 8: Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

WhatisPython

• Multi-purpose(Web,GUI,Scripting,etc.)• ObjectOriented• Interpreted• StronglytypedandDynamicallytyped• Focusonreadabilityandproductivity

Page 9: Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

Features

• BatteriesIncluded• EverythingisanObject• InteractiveShell• StrongIntrospection• CrossPlatform• CPython,Jython,IronPython,PyPy

Page 10: Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

WhoUsesPython

• Google• NASA• Yahoo• GentooLinux• ...

Page 11: Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

Releases

• Createdin1989byGuidoVanRossum• Python1.0releasedin1994• Python2.0releasedin2000• Python3.0releasedin2008• Python2.7isthe lastversionofPython2• Python3.4 isthelatestversion

Page 12: Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

Syntax

Page 13: Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

HelloWorld

hello_world.py

Page 14: Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

Indentation

• Mostlanguagesdon’tcareaboutindentation

• Mosthumansdo• Wetendtogroupsimilarthingstogether

Page 15: Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

Indentation

The else here actually belongs to the 2nd if statement

Page 16: Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

Indentation

The else here actually belongs to the 2nd if statement

Page 17: Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

Indentation

Some do like this

Page 18: Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

Indentation

You should always be explicit

Page 19: Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

Indentation

Text

Python embraces indentation

Page 20: Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

Comments

Page 21: Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

Types

Page 22: Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

Strings

Page 23: Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

Numbers

Page 24: Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

Null

Page 25: Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

Lists

Page 26: Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

Lists

Page 27: Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

Dictionaries

Page 28: Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

DictionaryMethods

Page 29: Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

Booleans

Page 30: Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

Operators

Page 31: Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

Arithmetic

Page 32: Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

StringManipulation

Page 33: Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

LogicalComparison

Page 34: Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

IdentityComparison

Page 35: Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

ArithmeticComparison

Page 36: Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

ControlFlow

Page 37: Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

Conditionals

Page 38: Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

ForLoop

Page 39: Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

ExpandedForLoop

Page 40: Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

WhileLoop

Page 41: Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

ListComprehensions

• Usefulforreplacingsimplefor-loops.

Page 42: Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

Functions

Page 43: Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

BasicFunction

Page 44: Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

FunctionArguments

Page 45: Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

ArbitraryArguments

Page 46: Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

Fibonacci

Page 47: Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

FibonacciGenerator

Page 48: Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

Classes

Page 49: Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

ClassDeclaration

Page 50: Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

ClassAttributes

• Attributesassignedatclassdeclarationshouldalwaysbeimmutable

Page 51: Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

ClassMethods

Page 52: Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

ClassInstantiation&AttributeAccess

Page 53: Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

ClassInheritance

Page 54: Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

Python’sWay

• Nointerfaces• Norealprivateattributes/functions• Privateattributesstart(butdonotend)withdoubleunderscores.

• Specialclassmethodsstartandendwithdoubleunderscores.– __init__,__doc__,__cmp__,__str__

Page 55: Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

Imports

• Allowscodeisolationandre-use• Addsreferencestovariables/classes/functions/etc.intocurrentnamespace

Page 56: Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

Imports

Page 57: Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

MoreImports

Page 58: Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

ErrorHandling

Page 59: Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

Documentation

Page 60: Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

Docstrings

Page 61: Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

Tools

Page 62: Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

• IPython(http://ipython.org)– betterinteractiveshellandmore

• ContinuumAnalyticsAnacondaPythondistribution (http://www.continuum.io)

• CPython• Jython• IronPython• PyPy• Cython

Page 63: Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

SciPy

• NumPy• Matplotlib• Sympy• pandas• scikit-learn• IPython

Page 64: Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

WebFrameworks

• Django• Flask• Bottle.py• Twisted• ...

Page 65: Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

IDEs

• Emacs• Vim• Komodo• PyCharm• Eclipse(PyDev)

Page 66: Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

PackageManagement

Page 67: Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

Resources

• http://python.org/• http://diveintopython.org/

Page 68: Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

Example

Page 69: Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

GoingFurther

• Decorators• ContextManagers• Lambdafunctions• Generators• ...

Page 70: Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

OtherScriptingoptions

• Ruby• Perl• JavaScript(Node.js)• TypeScript(compilestoJavaScript)• CoffeeScript(compilestoJavaScript)• PHP• ...

Page 71: Introduction to Pythonguntis/unix/PythonLekcija.pdf · The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl,

Gluelanguages• Erlang• UnixShellscripts(ksh,csh,bash,sh andothers)• WindowsPowerShell• ecl• DCL• Scheme• JCL• m4• VBScript• JScriptandJavaScript• AppleScript• Python• Ruby• Lua• Tcl• Perl• PHP• Pure• Rexx• XSLT