Exercises, examples and other material relating to training module Y300. This topic is presented on public courses Learning to program in Python
, Python Programming
, Intermediate Python
Python originated in 1988, and there are some elements dating back to the early days which
will hold back the ongoing development and use of the language 30 years later in today's very
different world. In Python 3, some of these time-expired features have been replaced by
more currently appropriate facilities, yet in a way that code may be moved across with as
little pain as possible. While much development work continues in Python 2, courses continue
to cover that version, but always with Python 3 in mind so that our delegates are writing code
that's good for the future too.
|Articles and tips on this subject||updated|
|4712||A reminder of the key issues to consider in moving from Python 2 to Python 3|
1. New style classes take on the old style class format
But does it matter if you explicity inherit in Python 3??
2. print moves from being a keyword to a function
Single value tuple as parameter will work in both cases
You may also do a sys.stdout.write
3. integer ...
|4711||Convering from Python 2 to Python 3 - an update, and the 2to3 utility|
Python 3 has now been with us for a number of years, but the move across from Python 2 to Python 3 is still far from complete. And as Python 3 is not source code compatible with Python 2, that's no big surprise to anyone. The very good reason for the changes is that the correct decisions made on langauge ...
|4650||Why populate object with values as you construct them?|
Objects are a great way to bundle up a whole lot of values (also known as properties or attributes) into a single container / collection, allowing the the programmer who makes use of the objects to be able to set them up just once and delegate the management of the values to the class / object code. ...
|4649||Object and Static methods - what is the difference; example in Python 3|
There are two types of methods (named pieces of code) that you can use in a class.
Object methds - occasionally called dynamic methods - operate on an object within a class ("one particular thing of a type") and will always have some way of referencing the elements (variables) within an object of the ...
|4590||Progress on moving from Python 2 to Python 3 - training for both versions|
I find the progressing fron one major version of a language to another is often incredibly slow. But I suppose I shouldn't find it "incredible" because lifetime code maintenance and updates account for more time spent on a successfully appied piece of software that the original writing of it. And even ...
|4470||Testing in Python 3 - unittest, doctest and __name__ == __main__ too.|
Python's doctest modules takes an interactive session pasted into your documentations string, analyses it and reruns the Python code you used in your session ... telling you whether you're still getting the same results or not.
Firstly, it's a great way of copying tests you've done and recording the ...
|4469||Sorting in Python 3 - and how it differs from Python 2 sorting|
Sorting has changed between Python 2 and Python 3 ... and it's easy and logical to sort lists in Python 3. Problem is that the Python 3 documentation shows you really complicated examples ...
There are two ways of sorting a list in Python 3:
a) You can use the sorted function, into which you pass a ...
|2871||Moving from Python 2.6 to Python 3|
When should you move your development to Python 3? When you're good and ready and you're certain that you'll not need to run your code on Python 2 again. Up to that point, the 2to3 utility will convert code for you.
Here's an example where I converted a short example script called "london" - using ...
|2778||Learning to program in Python 2 ... and / or in Python 3|
Yesterday - "Learning to Program in Python" - and I'm now teaching the day so that it's both Python 2 and Python 3 compatible. But it's really an intro day, and we'll vary how we go for the following 3 days of the course, majoring in an appropriate direction for our group - that's a luxury we have when ...
|2559||Moving the product forward - ours, and MySQL, Perl, PHP and Python too|
I have just posted a "steady as she goes" message, welcoming the new decade but reassuring our customers that we're carrying on offering very much the same high quality courses, and the same high quality hotel rooms. But things can't stand totally still - they would stagnate, courses would get out ...
|2285||Great new diagrams for our notes ... Python releases|
When training, I project onto a white wall ... and I draw onto the projected image, and add extra notes and diagrams around the side too - after all, the whole wall is a whiteboard. Some of the diagrams and slides that I'll come up with are new / unique for an individual course, and I end up photographing ...
|2277||Python classes / courses - what version do we train on?|
Usually, the release number of a programming language that's used on a 'first level' course doesn't make a big difference as the changes tend to be quite minor - after all, languages cannot change much for who would develop e piece of code using a syntax that was likely to go out of date? But that's ...
|1791||The road ahead - Python 3|
A couple of days ago, I mentioned Python 3 in general terms - and today I'm starting to flesh out the road ahead.
The print operator in Python was always a bit curious, with the trailing comma to signify "no new line" ... and that has been replaced by a print function, with named parameters ...
|1788||Python 2 to Python 3 / Python 3000 / Py3k|
There's a new release of Python on the horizon. Well - it's rather closer than the horizon, as there are beta test releases around, and a final production release is due within the month. It has been known as Python 3000 and Py3k along the way - but the release is numbered 3.0 and it's really known ...
|753||Python 3000 - the next generation|
There is no definite schedule for Python 3 (a.k.a. Python 3000 or Py3K) but a Python Enhancement Proposal (PEP) that details plans exists. There's a guiding principle to reduce duplication by removing old ways of doing things which will break version 2 compatibility. (Heard it before ... Perl 5 to Perl ...
Examples from our training material
|cheznous|| simple inheritance example - Python 2.7 and Python 3|
|george|| Integer v float arithmetic|
|git_2.py|| Generator and xrange - Python 2|
|git_3.py|| Generator in Python 3, range replaces xrange|
|input_2.py|| inputs in Python 2|
|input_3.py|| Inputs in Python 3 - input replaces raw_input|
|jba1|| Class definition and use - Python 3|
|jba2|| Class definition and use with a static method - Python 3|
|jba3|| Construct and Populate methods separated|
|jba_poor|| Poor class definition and use |
|john|| input and raw_input - Python 2 and Python 3|
|london.2|| Python 2.6 for conversion to Python 3|
|london.3|| Python 3 - converted from Python 2.6 example|
|print_2.py|| print in python 2|
|print_3.py|| print in Python 3 - print now a function|
|py2only.py|| Python 2 - ONLY - Hello World|
|py3only.py|| Python 3 - ONLY - Hello World|
|pyboth.py|| Python 2 and Python 3 - Hello World|
|rsy|| Python 3 using classes of objects for data filtering|
Some modules are available for download
as a sample of our material or under an Open Training Notes License
for free download from [here]
Topics covered in this module
Why Python 3?
Print as a function.
Range and other iterators (map, etc).
nonlocal (compared to global).
New style classes.
Strings, bytes, unicode and string formatting.
A note on Decorators.
Versions 2.6 and 2.7 3.0 to 3.3 and 2to3
A Strategy for upgrade.
If you are looking for a complete course and not just a information on a single subject, visit our Listing and schedule
Well House Consultants specialise in training courses in
. We run
throughout the UK (and beyond for longer courses), and
at our training centre in Melksham, Wiltshire, England.
It's surprisingly cost effective to come on our public courses -
even if you live in a different
country or continent to us
We have a technical library of over 700 books on the subjects on which we teach.
These books are available for reference at our training centre.