Presentation is loading. Please wait.

Presentation is loading. Please wait.

Python – It's great By J J M Kilner. Introduction to Python.

Similar presentations


Presentation on theme: "Python – It's great By J J M Kilner. Introduction to Python."— Presentation transcript:

1 Python – It's great By J J M Kilner

2 Introduction to Python

3 What is Python Python is an interpreted language At runtime, ASCII is parsed by a program called python which interprets the commands and does stuff Erm, so it's just a language. Yes and a slow one as well! So.... why are you here? Because it is fantastic!

4 Why is Python a Good Thing?

5 Concise reader friendly language Once you get past a small learning curve, it is very easy to write small, clear, correct programs Useful built in types and operations It is a computer scientists language with e.g. array creation that looks like formal set-builder notation Many well-designed libraries Almost everything you could want to do has been done, every library you want to use has bindings for Python. Due to the first two benefits it is often very easy to get two libraries that you want to use to talk to each other.

6 Let's look at some python def function(x): for y in range(1,10): x = x * y return x a = map(function, range(10)) print a

7 Useful basic data types Int, char, double, etc. Strings are immutable, but do all the things you would expect (concatenation, length, tokenize, format etc.) Lists are like c++ vectors – but you can have a mix of things in them Tuples are like immutable lists Dictionaries are like c++ maps (key/value pairs) Sets are.. sets

8 Examples ex_list = [1,2,3,“four”,5.001] ex_dictionary = {“key”: “value”} ex_tuple = (23, 128) 'this string will say “hello” here -> {0}'.format(“hello”) a = [0,1,2,3,4,5,6,7,8,9] a = range(10) a = [x for x in range(10)] a = (x for x in xrange(10)) # a bit different

9 Familiar basic operations Define a function with def and a class with class Classes have methods and fields accessed as normal with the. operator Looping with for in : Fast looping with map Conditionals with if, elif and else

10 Modern Language Iterators and generators Object-oriented with classes and inheritance No need for generics as all typing is at run-time! Modules and packages for managing software and interfaces Well integrated and easy to use documentation tools, unit tests etc. Packing and unpacking Iterator tools like enumerate and zip

11 Using other libraries The python system is set up to know where to look for all your python files Using tools like py-distutils you always install to the right place So to use an existing library (so long as it installed) is as easy as: import Image i = Image.open(“myfile.png”)

12 How to work with python Create.py files and run from the command line $>python mycode.py Work on code in an interpreter $>python >>x = [y**2 for y in range(400)] Both Using Ipython In an IDE PyDev is great

13 Pythonic Good code is referred to as “pythonic” No hard and fast rules but: Clear Concise Standard

14 Python World Current “real” version is 2.7 Python 3.0 is quite different in some fundamental ways and the Python world is currently in the process of transitioning If you run “python” from the command line you are running c-python (i.e. python implemented in C). This gives you access to C, C++ and Fortran based extensions There is also Jython (Python implemented in Java) and Iron Python (Python implemented in.Net / Mono) which allow you to access libraries on the JVM or.Net VM Python is embedded in many other pieces of software i.e. nuke, maya, blender etc.

15 Binding to libraries When a C / C++ library is exposed to Python, the code that does this is known as a binding If you need to use a library that has not already been bound then there are automated tools for binding to C / C++. SWIG is very popular, but my current favourite is PyPlusPlus (a Python binding generator written in Python)

16 ● Python is too slow for my application Really? 1. You will write your code faster giving you more time to optimise it 2. You have easy high-level access to optimised libraries which will probably do the low-level work much faster than any code you are likely to write 3. Tools such as Cython (not to be confused with the c-python interpretor) can compile python to machine code 4. Tools such as scipy.weave allow you to insert snippets of C++ directly into your code so you can hand-optimise inner loops

17 I already know MATLAB Converting MATLAB code to Python/Numpy is pretty easy MATLAB is only useful for MATLAB and is not a proper language for reading files / writing UI Knowing Python will let you build powerful build systems, write user interfaces, script many commercial and open source applications and manage your MP3 collection as well as allowing you to do numerical work!

18 What we aren't going to talk about (but could) 3D Graphics with pyOpenGL Building a website with Django Controlling your build system with scons Image processing with PIL Python for scripting Python for UI with wxWidgets Python for Computer Vision with OpenCV

19 Numpy

20 Numpy brings powerful numerical and linear algebra tools to Python Front-end to BLAS/LAPAC but so much more Foundation for Scipy and used / interfaced to by pretty much all other Python numerical libraries (PIL, OpenCV)

21 Numpy arrays a = [1,2,3,4] b = numpy.array([1,2,3,4]) b[2] b[-3] b[2:-1] b[1:-1:2] b[a]

22 Broadcasting a = numpy.array([1,2,3,4]) b = a * 10 print a + b c = a[:,numpy.newaxis] + b[numpy.newaxis,:] print c print c[c%3 == 0]

23 numpy.linalg Linear algebra library Svd Eigen vectors / values Norms Lots of other stuff

24 Examples Dimensionality Reduction ICP HMM

25 MATLAB Google “Numpy for MATLAB users” There is a lot of one for one substitution Not everything in MATLAB is in Numpy If you are just using basic linear algebra you should be covered Lots of other stuff in Scipy and other libraries – easy to access / use with Numpy data

26 SAGE Tries to bring everything together in one teaching / research web-based “notebook”


Download ppt "Python – It's great By J J M Kilner. Introduction to Python."

Similar presentations


Ads by Google