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,

Post on 23-Jan-2020

24 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

Transcript

IntroductiontoPython

DidzisGosko

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

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)

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

Eachtaskhasit’sowntool

ShellScripting:• programexecution,input/outputcapturingandchaining(with

pipes).

Python:• filecontent/dataprocessing(fromperspectiveofshell

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

IngeneralPythondoesn’tcomparewithShellScripting.

Pythonisgoodfor

• Conceptchecking• Fastprototyping• Anyperformancenon-essentialprogrammingtask

• Stringprocessing

• Areadable,dynamic,pleasant,• flexible,fastandpowerfullanguage

Overview

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

WhatisPython

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

Features

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

WhoUsesPython

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

Releases

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

Syntax

HelloWorld

hello_world.py

Indentation

• Mostlanguagesdon’tcareaboutindentation

• Mosthumansdo• Wetendtogroupsimilarthingstogether

Indentation

The else here actually belongs to the 2nd if statement

Indentation

The else here actually belongs to the 2nd if statement

Indentation

Some do like this

Indentation

You should always be explicit

Indentation

Text

Python embraces indentation

Comments

Types

Strings

Numbers

Null

Lists

Lists

Dictionaries

DictionaryMethods

Booleans

Operators

Arithmetic

StringManipulation

LogicalComparison

IdentityComparison

ArithmeticComparison

ControlFlow

Conditionals

ForLoop

ExpandedForLoop

WhileLoop

ListComprehensions

• Usefulforreplacingsimplefor-loops.

Functions

BasicFunction

FunctionArguments

ArbitraryArguments

Fibonacci

FibonacciGenerator

Classes

ClassDeclaration

ClassAttributes

• Attributesassignedatclassdeclarationshouldalwaysbeimmutable

ClassMethods

ClassInstantiation&AttributeAccess

ClassInheritance

Python’sWay

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

• Specialclassmethodsstartandendwithdoubleunderscores.– __init__,__doc__,__cmp__,__str__

Imports

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

Imports

MoreImports

ErrorHandling

Documentation

Docstrings

Tools

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

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

• CPython• Jython• IronPython• PyPy• Cython

SciPy

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

WebFrameworks

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

IDEs

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

PackageManagement

Resources

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

Example

GoingFurther

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

OtherScriptingoptions

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

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

top related