Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Introduction to Python Didzis Gosko Scripting language From Wikipedia: A scripting language or script language is a programming language that supports scripts, programs written for a special run-time environment that can interpret (rather than compile) and automate the execution of tasks that could alternatively be executed one-by-one by a human operator*. ... The term "scripting language" is also used loosely to refer to dynamic high-level general-purpose language, such as Perl, Tcl, and Python, with the term "script" often used for small programs (up to a few thousand lines of code) in such languages ... * Python has interactive shell Shell Scripting (bash etc.) vs Python Shell Scripting (bash etc.): Python: + Shell scripting has simpler notations for I/O redirection. + It is simpler to create pipelines out of existing programs in shell. + General purpose programming language + Easy (elegant) syntax + Complete toolset in a monolith package + Cross-platform + Powerful +/- Ugly syntax of control flow statements - Most of tasks (any data processing) are delegated to external programs (awk, sed, perl, python etc.) - Extensive use of process forking - Compatibility problems between platforms +/- Block indentation - I/O redirection, creating pipelines etc. not as easy as with Shell Scripting - Not all systems have python installed (it’s also true with bash) Bash vs Python #!/bin/bash # comment line echo "what a fine day: " date • Output, when called by “./test.sh”: what a fine day: Thu Oct 28 23:37:39 EEST 2004 #!/usr/bin/env python3 # comment line print("what a fine day:") import os os.system("date") • Output, when called by «./test.py»: what a fine day: Thu Oct 28 23:37:39 EEST 2004 Each task has it’s own tool Shell Scripting: • program execution, input/output capturing and chaining (with pipes). Python: • file content/data processing (from perspective of shell scripting) • any other task... • can be used as scripting language (unlike C/C++, Java etc.) In general Python doesn’t compare with Shell Scripting. Python is good for • Concept checking • Fast prototyping • Any performance non-essential programming task • String processing • A readable, dynamic, pleasant, • flexible, fast and powerful language Overview • • • • • • Background Syntax Types / Operators / Control Flow Functions Classes Tools What is Python • • • • • Multi-purpose (Web, GUI, Scripting, etc.) Object Oriented Interpreted Strongly typed and Dynamically typed Focus on readability and productivity Features • • • • • • Batteries Included Everything is an Object Interactive Shell Strong Introspection Cross Platform CPython, Jython, IronPython, PyPy Who Uses Python • • • • • Google NASA Yahoo Gentoo Linux ... Releases • • • • • • Created in 1989 by Guido Van Rossum Python 1.0 released in 1994 Python 2.0 released in 2000 Python 3.0 released in 2008 Python 2.7 is the last version of Python 2 Python 3.4 is the latest version Syntax Hello World hello_world.py Indentation • Most languages don’t care about indentation • Most humans do • We tend to group similar things together 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 Dictionary Methods Booleans Operators Arithmetic String Manipulation Logical Comparison Identity Comparison Arithmetic Comparison Control Flow Conditionals For Loop Expanded For Loop While Loop List Comprehensions • Useful for replacing simple for-loops. Functions Basic Function Function Arguments Arbitrary Arguments Fibonacci Fibonacci Generator Classes Class Declaration Class Attributes • Attributes assigned at class declaration should always be immutable Class Methods Class Instantiation & Attribute Access Class Inheritance Python’s Way • No interfaces • No real private attributes/functions • Private attributes start (but do not end) with double underscores. • Special class methods start and end with double underscores. – __init__, __doc__, __cmp__, __str__ Imports • Allows code isolation and re-use • Adds references to variables/classes/functions/etc. into current namespace Imports More Imports Error Handling Documentation Docstrings Tools • IPython (http://ipython.org) – better interactive shell and more • Continuum Analytics Anaconda Python distribution (http://www.continuum.io) • CPython • Jython • IronPython • PyPy • Cython SciPy • • • • • • NumPy Matplotlib Sympy pandas scikit-learn IPython Web Frameworks • • • • • Django Flask Bottle.py Twisted ... IDEs • • • • • Emacs Vim Komodo PyCharm Eclipse (PyDev) Package Management Resources • http://python.org/ • http://diveintopython.org/ Example Going Further • • • • • Decorators Context Managers Lambda functions Generators ... Other Scripting options • • • • • • • Ruby Perl JavaScript (Node.js) TypeScript (compiles to JavaScript) CoffeeScript (compiles to JavaScript) PHP ... Glue languages • • • • • • • • • • • • • • • • • • • • Erlang Unix Shell scripts (ksh, csh, bash, sh and others) Windows PowerShell ecl DCL Scheme JCL m4 VBScript JScript and JavaScript AppleScript Python Ruby Lua Tcl Perl PHP Pure Rexx XSLT