Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
Python - fresh examples of all the fundamentals

Some more new examples in Python - from this week's course.

From my Introduction to Python / simple example to show the power of the language, I present my example that parsed a big data (log) file and counter and sorted by number of accesses the hits from various remote hosts. A long report, ending as follows:
216.129.119.10 549
216.129.119.44 580
66.249.71.164 587
66.249.68.89 2975
217.160.182.81 3215
72.30.142.107 3250
77.88.28.246 4815
66.249.71.55 5457
Dorothy-2:p2 grahamellis$

With the program source available [here].

The some shorter / easier stuff from the first day:

An program to provide a chart of exchange rates - the sort you would give your children when you take them overseas and want to teach them about the local money, and have them work out what is good value. [here].

A short example of how to save a multiline string in a variable in Python. [here].

A prompt ... read ... calculate ... report program in Python. [here].

In Python, you can save yourself writing loops to go through each element of a list by using list methods and operators. We wrote a 'control' example showing how it would work in any language [here], and went on to reduce the looping dramatically [here]. In both cases, the programs produce two lists of 365 elements - representing days of the year. One list has 31 1s, 28 2s and so on - to convert day of year to month of year, and the other list has the numbers 1 to 31, then 1 to 28, and so on, to convert day of year to day of month.

Newcomers often get confused between assigning to the whole of a list (replacing the whole thing) and assigning to an element (replacing just that element and leaving the rest in tact. Our final first day example - [here] illustrates both in source code, with
listname = replacing the whole list and
listname[6] = replacing just the 7th element.

Examples from the second day

Functions, Modules, Packages, and Object Orientation in Python!

In most languages, functions run to completion before they return anything, but in Python you can write a generator which passes back a series of values at yield statements - thus providing an iterator. This is especially useful where you have a big data set - you don't have to read the whole dataset into memory in one go and then process it bit by bit, nor have the read and process in one function. We wrote a 'control' case [here] showing how this would be in a conventional language, and then did it the python way with a generator [here]. You can do this in Lua - using coroutines - too.

The map function converts every member of a list through a function which you pass in as a parameter, and the filter function return you a list of (only) matching itemes, as identified by a callback function in the call. Consider these to be the equivalents of map and grep in Perl if you like ... and see the example source code [here].

As programs grow, you need to separate out your variables into different namespaces - you need to be able to tell "John Smith" from "John Jones" so in Python, you write smith.john of jones.john. And you load namespaces from different files. Example - the file that does the loading [here], and the file it loaded [here] - that latter complete with its own test code to allow you to check it works on its own before integrating it with your main code.

In Java, defining and using a class would be 2 separate files with lots of "public"s and "private"s. And in C++ you would have half a dozen files. Which is why Python's OO model is so sweet - the whole thing can be done in one file - and we have an example [here]. We also have an example with a static (class) variable [here] and we even have examples with multiple classes - look forward to day 3, but they're still potentially all in the one file if they're going to be coded, used and maintained in tandem.

Day 3 - a bit beyond the basics

Some string handling and formatting - [here] is the use of the % operator both for modulo (integer) and formatting - the operator is overloaded - and a regular expression example which shows you how you can split a string at either an exact match, or at a pattern - [here].

Polymorphism and Inheritance - the way data types can all be defined from the same starting point, with only the differences being coded, and then how those subtley different objects can automatically behave in the way designated for them - comes naturally in Python. We have examples [here] which show multiple related classes, and comparators, and [here] showing how they can be wrapped into a module.

Regular expressions ... we cover quite a lot there ... included a new example [here] where I looked for all the postcodes in a string.
(written 2009-08-20, updated 2009-08-23)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
Y102 - Python - Fundamentals
  [3917] BODMAS - the order a computer evaluates arithmetic expressions - (2012-11-09)
  [3886] Formatting output - why we need to, and first Python example - (2012-10-09)
  [3551] Some terms used in programming (Biased towards Python) - (2011-12-12)
  [3278] Do I need to initialise variables - programming in C, C++, Perl, PHP, Python, Ruby or Java. - (2011-05-05)
  [3181] Beware - a=a+b and a+=b are different - Python - (2011-02-23)
  [3083] Python - fresh examples from recent courses - (2010-12-11)
  [2778] Learning to program in Python 2 ... and / or in Python 3 - (2010-05-24)
  [2442] Variable storage - Perl, Tcl and Python compared - (2009-10-08)
  [1878] Pascals Triangle in Python and Java - (2008-11-10)
  [1461] Python - input v raw input - (2007-12-06)
  [1448] Question on division (Java) - Also Perl, PHP, Python ... - (2007-11-28)
  [1430] Integer v float - Python - (2007-11-12)
  [956] Python security - trouble with input - (2006-11-30)
  [748] Getting rid of variables after you have finished with them - (2006-06-06)
  [633] Copying a reference, or cloning - (2006-03-05)
  [328] Making programs easy for any user to start - (2005-05-29)

Y104 - Python - Lists and Tuples
  [4027] Collections in Python - list tuple dict and string. - (2013-03-04)
  [3763] Spike solutions and refactoring - a Python example - (2012-06-13)
  [3669] Stepping through a list (or an array) in reverse order - (2012-03-23)
  [3348] List slices in Python - 2 and 3 values forms, with an uplifting example - (2011-07-06)
  [3257] All possible combinations from a list (Python) or array (Ruby) - (2011-04-23)
  [3118] Arrays of arrays - or 2D arrays. How to program tables. - (2011-01-02)
  [2996] Copying - duplicating data, or just adding a name? Perl and Python compared - (2010-10-12)
  [2719] Traffic lights in Python - (2010-04-13)
  [2284] Strings as collections in Python - (2009-07-12)
  [2280] Creating and iterating through Python lists - (2009-07-12)
  [1789] Looking for a value in a list - Python - (2008-09-08)
  [1641] Tektronix 4010 series / Python Tuples - (2008-05-13)
  [1220] for loop - how it works (Perl, PHP, Java, C, etc) - (2007-06-06)
  [955] Python collections - mutable and imutable - (2006-11-29)
  [899] Python - extend v append on a list - (2006-10-20)
  [657] The ternary operator in Python - (2006-03-25)
  [383] Overloading of operators on standard objects in Python - (2005-07-19)

Y107 - Python - Dictionaries
  [4029] Exception, Lambda, Generator, Slice, Dict - examples in one Python program - (2013-03-04)
  [3934] Multiple identical keys in a Python dict - yes, you can! - (2012-11-24)
  [3662] Finding all the unique lines in a file, using Python or Perl - (2012-03-20)
  [3555] Football league tables - under old and new point system. Python program. - (2011-12-18)
  [3554] Learning more about our web site - and learning how to learn about yours - (2011-12-17)
  [3488] Python sets and frozensets - what are they? - (2011-10-20)
  [3464] Passing optional and named parameters to python methods - (2011-10-04)
  [2994] Python - some common questions answered in code examples - (2010-10-10)
  [2986] Python dictionaries - reaching to new uses - (2010-10-05)
  [2915] Looking up a value by key - associative arrays / Hashes / Dictionaries - (2010-08-11)
  [1145] Using a list of keys and a list of values to make a dictionary in Python - zip - (2007-04-13)
  [1144] Python dictionary for quick look ups - (2007-04-12)
  [103] Can't resist writing about Python - (2004-10-29)

Y109 - Python - Exceptions
  [4161] Python varables - checking existance, and call by name or by value? - (2013-08-27)
  [3930] Reporting the full stack trace when you catch a Python exception - (2012-11-22)
  [3913] How many times ... has this loco headed west through Tenby? - Python exceptions - (2012-11-05)
  [3664] Error checking in a Python program - making your program robust via exceptions - (2012-03-22)
  [3441] Pressing ^C in a Python program. Also Progress Bar. - (2011-09-15)
  [3433] Exceptions - a fail-safe way of trapping things that may go wrong - (2011-09-11)
  [3177] Insurance against any errors - Volcanoes and Python - (2011-02-19)
  [2998] Using an exception to initialise a static variable in a Python function / method - (2010-10-13)
  [2622] Handling unusual and error conditions - exceptions - (2010-02-03)
  [2408] Robust user input (exception handling) example in Python - (2009-09-17)
  [2281] Python - using exceptions to set a fallback - (2009-07-12)
  [2018] UnboundLocalError - Python Message - (2009-01-31)
  [1236] Trying things in Python - (2007-06-18)
  [1042] Nested exceptions in Python - (2007-01-18)
  [381] Exceptions in Python - (2005-07-17)

Y112 - Python - Objects - Intermediate
  [4094] Python Properties - how and why - (2013-05-18)
  [4028] Really Simple Class and Inheritance example in Python - (2013-03-04)
  [3887] Inheritance, Composition and Associated objects - when to use which - Python example - (2012-10-10)
  [3796] Backquote, backtic, str and repr in Python - conversion object to string - (2012-07-05)
  [3524] Metaclasses (Python) and Metatables (Lua) - (2011-11-17)
  [3472] Static variables in functions - and better ways using objects - (2011-10-10)
  [3442] A demonstration of how many Python facilities work together - (2011-09-16)
  [3002] A list of special method and attribute names in Python - (2010-10-17)
  [2905] Defining static methods in Python - (2010-08-05)
  [2889] Should Python classes each be in their own file? - (2010-07-27)
  [2785] The Light bulb moment when people see how Object Orientation works in real use - (2010-05-28)
  [2764] Python decorators - your own, staticmethod and classmethod - (2010-05-14)
  [2722] Mixins example in Python - (2010-04-14)
  [2720] Multiple inheritance in Python - complete working example - (2010-04-14)
  [2717] The Multiple Inheritance Conundrum, interfaces and mixins - (2010-04-11)
  [2693] Methods that run on classes (static methods) in Python - (2010-03-25)
  [2485] How do I set up a constant in Python? - (2009-10-31)
  [2409] TypeError: super() argument 1 must be type, not classobj (Python) - (2009-09-18)
  [1819] Calling base class constructors - (2008-10-03)
  [1661] Equality, sameness and identity - Python - (2008-05-31)
  [1644] Using a utility method to construct objects of different types - Python - (2008-05-17)
  [1517] Python - formatting objects - (2008-01-24)
  [1217] What are factory and singleton classes? - (2007-06-04)
  [1146] __new__ v __init__ - python constructor alternatives? - (2007-04-14)
  [964] Practical polymorphism in action - (2006-12-04)
  [903] Pieces of Python - (2006-10-23)
  [831] Comparison of Object Oriented Philosophy - Python, Java, C++, Perl - (2006-08-13)
  [656] Think about your design even if you don't use full UML - (2006-03-24)
  [477] Class, static and unbound variables - (2005-10-25)
  [296] Using a Python dictionary as a holder of object attributes - (2005-04-30)

Y303 - Python Network Programming
  [4087] Python network programming - new FTP and socket level examples - (2013-05-14)
  [2765] Running operating system commands from your Python program - (2010-05-14)
  [2695] TCP v UDP / Client v Server - Python examples - (2010-03-25)
  [2694] Multiple processes (forking) in Python - (2010-03-25)
  [2365] Counting Words in Python via the web - (2009-08-18)


Back to
Learning to program - how to jump the first hurdles
Previous and next
or
Horse's mouth home
Forward to
Using a cache for efficiency. Python and PHP examples
Some other Articles
Public Transport from (and to) Melksham on Sundays
Quiet summer days? I think not!
C++, Python, and other training - do we use an IDE
Using a cache for efficiency. Python and PHP examples
Python - fresh examples of all the fundamentals
Learning to program - how to jump the first hurdles
At the end of the course
Getting it right from the start - new programmers
Alpaca Case or Camel Case
4315 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/2368_Pyt ... ntals.html • PAGE BUILT: Thu Sep 18 15:30:25 2014 • BUILD SYSTEM: WomanWithCat