thank you thank you thank you very much thank you thank you very much welcome to python 102 and to the first session of pike on after the cleaner great to see you all here thanks for coming this is Python 102 so this is a introductory level or it’s a it’s a slightly above the introductory level particularly aimed at those that are self-taught in Python I’m going to make sure that you know all the essential stuff that you really need to know that will help make a lot of the rest of the sessions go well so that you understand them a little bit about me I am people of it second-generation programmer and my father was a programmer I’ve been programming being paid to program since 1982 anyone here born after 82 excellent I’ve been programming longer than longer than that 31 years i am i’m also curiously 31 in 31 years old that is in hexadecimal or 23 base 23 which is a curious bit of curiosity I professionally run training courses so I’ve worked as a programmer since 1982 these days about half of my time is working as a trainer so I go to corporations and run courses in a variety of languages C C++ Perl Python Java XML SQL PowerShell etc unix shell script that’s what I do and I’ve been running training courses since 1985 running Python training courses since 2007 and I love programming anyone else here love programming it’s a joy I love what Alex said about the art I just love that aspect of programming and that’s one of the things that i love about python is that it gives us the ability to be very expressive in an artistic way if i cannot be too weird and I love python python is just one of those very few languages of the languages that i use that gives us an enormous breadth of scope from very beginners that it still works as much better learning language than basic or Pascal or one of those sorts of knots where this is being recorded one of those are less lovely languages visual basic and it’s a lot easier than C++ or Java or those nasty languages that are full of challenge I do still do a lot of work in c++ and other languages I’m here as hello on behalf of my company plus plus and as I said I’ve been running training courses since 85 in a variety of languages and if you want to contact me after the after the course this place to get me is through my website + + calm day you and I will just make a quick mention that I am doing a special so if any of you go like what you learn here and get back to your company and go we need to learn a whole lot more I’m very open to coming to your company and running a training course and i will give you a special discount for any new on-site bookings if you say I saw you at PyCon and say that you loved me so an overview of what we’re going to cover this morning now this is the classroom track so this is a lot of a lot of actual hands-on stuff if you’ve got a laptop this is a great time to whip it out get it going far up your path and interpreter get your editor going because there’s going to be a lot of opportunity for you to do hands-on stuff if you don’t have a laptop take lots of notes in fact what I’ll do if you don’t have a laptop even if you do have a laptop everyone wins a free pen so take a pin and pass the box along so take lots of notes as far as the aim here I’m starting I’m predominantly targeting to point most of my clients are still running to point something I’ve got a few still on 2.4 any 2.4 is here good 2.5 is much better I’ll be running 2.7 but I’m going to throw bit of three in there as well I got a couple of clients that are now commercially using 3 so 3 is at a stage now where it’s useful to work in so i’ll be throwing a few bits of three in there there’s permission to ask questions such as what if the Romans ever done for us what have they done for us anyone the roads sanitation education yes but apart from the roads education sanitation and

peace can’t forget peace just a reminder that python of course is not named after the animal that can kill but rather is named after monty python the English comedy troupe and so in in them in deference to them I will be continuing the very low standard of jokes and apologies in advance for all of that my aim for you at the end of this is to get you to a level where the rest of the conference makes more sense so that you can solidify your knowledge and understanding of Python and then use that as a launching pad for other sessions the target audience is as I said for beginning to intermediate happy person so the topics that we’re going to cover our we’re going to start off with just a very very quick recap of the ground level essentials so maybe I should check their about you that’s a bit about me this is a bit about the core about the about the session I need to find out a bit about you so who would classify themselves as a regular user of Python you would use it okay okay yep who’s never seen Python no okay yeah that’s fine if you’ve never seen or you’ve only dabbled a little bit in Python there’s just sort of dabbled in Python Python sir good and I need to get a bit of a background on other languages so who’s familiar with other languages the SE C C++ okay lovely Java people boo visual basic ah they’re even admitting it well done you are brave what does we got Ruby esoteric languages lua Haskell okay what else did I’m COBOL all right fortran nice okay good thank you but how could I forget i I’ve got a bit of a fondness around pearl I used pearl extensively before I got to python and I do still actually support a few things that run pill my website still runs pill pill programmers yes okay just a couple don’t bother for those that don’t know pearl just stay with Python and I’d like to get a little bit of information about your your target so who’s using Python or intends to use Python for web-based stuff coming for okay okay quite a lot of you yep as it turns out actually don’t do a lot of Python web stuff getting into that a bit more now using Python for sysadmin sort of tasks yep my get rid of bash get rid of those sorts of things good scientific mathematical simulations okay quite a few of you good and and for using them Python for build process testing creating other systems yep okay I actually use Python extensively for my sleep plus plus build process it’s just a very nice tool to use for that as well good thanks for that now I’m also quite happy that tell you about the structure here is that we’ve got 90 minutes so there’ll be two 45-minute sessions and we’ll be having a break in the middle for some of you you might go after the first half I’ve got enough I understand what I need to know I’m happy for you to leave then if everyone leaves maybe if you could tell me before then that you need more so I’ll be giving you lots of opportunities to ask questions and while i’m aiming at a beginner intermediate level i’m quite happy to go to the binger noodles sort of end for those of you that want to go down that down that path I thought that’s not my my primary focus so we’re going to do a recap of the essentials for those that aren’t terribly familiar with the the essentials understanding the object model is a crucial aspect of Python lots of people that have come from other languages don’t quite get how pythons objects work and even wow how variables work and I don’t even like to talk about variables in Python I don’t think that’s a helpful concept or context um we’re going to do a bit on objects the joy of dictionaries you can’t be a Python programmer and not have an intimate love for dictionaries dictionaries are part of the heart and soul of Python I’ve heard some I haven’t actually verified this but I heard someone say that the code that is in corp ifen for handling dictionaries is the most optimized code in the world I don’t have any benchmark for establishing that but it’s really really good and therefore it’s really really fast and it’s really really efficient

and it’s a great thing to use a lot of so if you’re coming from the C C++ background you’re probably not doing a lot of dictionary stuff and it’s a lovely thing to get used to we’re going to do quite a bit of stuff on building on type so aggregating types will start off with the simple types in strings maybe a bit on Long’s floats and then we’ll get into lists tuples and then build on those what if I need a list of lists list of tuples two-dimensional lists how are they work how do they how do they work we’re going to create our own types object-oriented programming is something that’s innate to Python programmers because well everything is an object that’s the way it works in Python everything is an object and when I say everything is an object what I mean is everything is an object that is Insur objects functions or objects modules or objects and therefore can be passed around we can take references to them and and near the end I’ll give you a quick of the standard library as most of you are familiar the batteries are included which is a that is you can arrive I can go to a client and if they’re got Python on there I know I can do my job I know I’ve got a I’ve got all the stuff i need to do web connections i can pull stuff down I can manage data I can work with CSV files I can do everything if I’ve got Python on there I might not have every single module that I want some of my clients have some great challenges in getting stuff installed on their production servers you may work in that sort of an environment so if you’ve got pipe and well at least you can do your essential work without too much too much drama so that’s the that’s the overview step 1 if you haven’t got Python you can probably even pull it down now if you’re on anything except windows you’ve probably already got it yay actually I should I should just do a quick check about platforms who predominantly develops on SE OS x max okay ubuntu other linux windows not too bad good thank you I’m actually running windows here I’m a lot of my corporate clients run windows if you haven’t got python python org is where you can get it if you’re on Windows the active state active python is just Python but it does come with a couple of very good extensions the win32 API modules means that i can get at the registry i can do all of that sort of stuff and that actually is pre-installed with active python picky version which ever you going to use to point or three the other challenge that we got particularly on some platforms is whether you going for 32-bit or 64-bit you need to make that decision I’m running I’m running a 64-bit operating system as we almost all are but Python 32-bit is a little better sorted for some external modules particularly things like pie game on things like that and choose your tools now intentionally for this particular session I’m going with the real bare bones I’m not using any ideas I’ve got a couple that I particularly like I might give you some demos of some of those Eclipse pydev pycharm is a lovely one from JetBrains for my java friends that have used IntelliJ lovely lovely excellent ide Komodo from active state is a cross-platform as as are those other two as well ideas for this for this session i’m just going to stick with some some real basics i’m just going to use notepad in a command window so the essentials okay let’s get going so what a Python programmer always has is a python shell we’ve ought you just you just have that up and running always it’s like I can’t program in Python if I don’t have a Python shell just sitting there running how does that work oh I don’t know jump over to the shell ah that’s what that does what are the methods that oh I can just hammer it in there’s my answer I’ll just bend that out a little so as most of you know you can you can do a print in Python and you enclose strings in them what is it double quotes or single quotes and the answer is what do you use doubles if you’re a sea program out you’re probably still using doubles just because that’s what you do Python people tend to use single quotes reason for that is it uses

some less toner when you go to print it it’s very very good for the Greenies it uses a less electricity on the on the on the overhead on the screen and also you don’t need your shift key I can prove that Python likes single quotes because if I did a string of hello and then I had a look at the string Python comes back with single quotes the quick thing to remember is that there’s actually no difference at all just whatever you start with you got to finish with I tend to use single quotes or everything except for when I’m doing comparisons to empty strings to single quotes looks like one double quote so I’ll use double double quotes to make it really obvious that that’s an empty string if I was doing something like that and the other place of course is um if I was doing gah day then of course that’s broken because you can’t have an embedded ‘ I guess I could always escape it yeah that’s looking ugly but what would be simpler would be to use the double quotes and that way I don’t need to escape my ‘ curiously that’s what the interpreter does as well um I can do as I saying I can print hello I can print some arithmetic 1 plus 2 ne1 three excellent in the interactive shell you don’t even need the print so you can one plus two oops and you can hit back / for line continuation you can multiply 2 times 3 getting up there now any 16 awesome it’s probably a little hut too easy for you let me bump it up what’s 5 to the power of 225 I can still see that some of you are still beyond this so let me bump it up a bit what’s 123456789 to the power of a hundred some light now there could be some options here my C programmers ago it’s obvious overflow this isn’t see though my friends that’s the answer and if it’s not obvious it Chuck’s an L on the end just to remind you that this is a long int 1 oh really that’s a long one that’s a big number by the way so is that so is that there will be a point it’s probably there I haven’t actually broken it still going a bit of steam coming out of the back of their but that’s perfectly fine that’s way bigger than any of you maths people are ever going to be dealing with one of the difference is there between Python 2 and Python 3 is that Python 3 doesn’t have the long type in sought ematic Lee internalized they’re still in there just get internally represented as longer ones here that in Python 2 there is a small but not really significant difference between the long and the the int I’m going to let that just run the F wallet might just crash it um okay so let me leave that running and go and grab I want to do some programming so let me go and grab say I want a program that will let me just grab another shell that should have been set up before sorry to something like that and I’ve got that just as a side note here using Windows PowerShell much better than the ordinary shell if you on Windows you owe it to yourself to get get used to the windows powershell lovely for my unix people you’ve got tab completion hmm you’ve got LS whoo you’ve got cat um and here i will run my python script by just running the one that I wanted enter a car model anyone what’s a good car model BMW small W hmm cost 1000 and there’s the output so simple little program there good practice even on Windows use your hash bang here I’m using user bin python the alternative would be to Ian Viet the advantage there is if your pythons not in the right spot on Windows

that will be completely ignored so I can just leave it there triple quoted verbatim block can be used anywhere but has a special meaning as the first thing inside a anything first thing inside a module first thing inside a function first thing inside a class gives me the documentation for that best practice the use of the blank line there is optional and even I could even pop it on just one line would also be fine doesn’t matter getting some input here using raw or input I’m sure you’ve used raw input for getting input as anyone ever used input to get input accidentally if there’s a roaring put there also must be an input that’s that decimal that’s my that’s my attempt at logic if there’s a raw input there must be a cooked input don’t ever use the cooked input if you’re not familiar with what cooked input is I can do an input and say please enter something please enter something I type in 23 and a is now 23 okay so why don’t we use input all the time well if I said please enter 1 plus 2 it actually evals the entry equals horrific Lee bad means if I said please into something and i typed in my name Peter it’s actually trying to get the variable called Peter bad tip never use input always use raw input except in Python 3 where input gets thrown away and raw input gets turned into input eval is a great security hole getting some variables converting it there with the convert it to an int printing the output easy way to print is to always use the comma because you don’t need to worry about the types of your arguments there well that’s a simple program but I’ve got more than one car so I’ll need to to bump it up here’s some options while if your a/c programmer you could always go and wrap your tests in / ends but this isn’t C Python requires indenting the standard indent is always four spaces always always always always always tabs are evil never use tabs see me afterwards if you want to use tabs if you if you want to use tabs I must address Pep eight if you’re not familiar with Pep eight you must be familiar with Pepe so for those who’ve used python before pepper eight says use for spaces never mix tabs and spaces and all new projects strongly recommend use spaces so that’s the Python way it you don’t have to truck you don’t have to believe me but you do owe something to follow greater van rossum the author of Python the benevolent dictator for life so if you’re using an editor you always just want to go and set your settings so that not that setting you want to set your settings to tab settings just use spaces get it to hit the tab key gives me spaces you’ll have less trouble um the problem with a true lip capital T for true capital F for false the problem with the true loop is that it’ll loop forever if I want to get out the easiest way not necessarily saying this is the best way but the easiest way would be to compare that against an empty string and if that’s nothing then leave the loop so when that program runs into a model a one there it is enter another one be too and that will keep on looping of course it’s not actually printing them because i’m not actually storing them anywhere so where am i going to store them i’m glad you asked the nicest easiest place to store them would be in a list lists are lists a lovely i love lists lists are good coming from a su programmers background we’re writing a linked list is part of the joy of being a sea programmer Oh new program new client can’t bring my tools in great i’ll write a linked list program again C++ programmers yeah good good luck with the stl there that’s it’s it’s better than writing it yourself but there’s got its own challenges java people have vectors and other other options but what i really want to hear is have myself a list so I’ve got the model of a car so this is not the best option but I’ll see where I can go with this I’ll have a list quite common best practice is the individual is the singular and the container is the plural

so I’ll get a model if it’s empty i’ll leave and if it’s not empty I will get the cost of the car and then I will go and append I love a pen depend is very handy now what I’m ending up with here is a coordinated list I’ll have the costs append the cost what have I forgotten probably need a list up there empty one PS that’s not a declaration I’m not declaring the variable Python doesn’t have variables it’s just a name but I need to have something that’s got an append to which I can append and lists have the append method and I probably want to go and plunk some of that stuff before i go and store it get do the stuff added in printing it out then will be another loop now what what could i do here i’ll just to start with make it awful and print all of the models and print all of the costs and of course i think some of you will know oh yeah it’s going to look lovely ps4 testing doing this a bee business i see people doing is just not a great idea in fact i see people test their mathematical programs an average program they type in loco type in some numbers 11 11 I the average is 1 good tests pass hmm I think we can come up with better tests than that here I’ve actually already got myself a text file which I would call a response file and even on Windows even if you’re not using PowerShell you can pipe stuff and I can pipe it into you can’t use the association of the file but I can do car one I can’t pipe it that way and I am missing a extra blank one on the end this is the response file types in types and presses enter types in presses enter types in presses enter I need the blank one on the end so that it’s got something to give us the terminating list it’s not real pretty as far as the input goes but anything that you can do that will help your testing is awesome I’m actually not going to talk a lot about methodologies and test driven development and all that sort of stuff but and there’s the please into the model the cost and then there’s the list of models and there’s the list of costs but oh it doesn’t really quite show me which one is which although lists are ordered they first one is first one always so what i could do is heck I’m a/c programmer let me go and use a loop so if your a/c programmer you will have a counter and the counter would be called I awesome and I would keep on looping while the counter is less than the length of the list and then I would print models subscript I and costs subscript I to get the ice element of course counting begins at zero I might even just print them out following each other and that way there the P nicely next to each other anything I’ve forgotten increment I thank you + + good I’m looking for her no Peter don’t know Domino + + does that work I’ve actually got a lightning talk I’m gonna do on this tonight but just just to make it plea plane I gets 5 plus plus I’m good I now has a value of oh come on I’m after a bit of is it six no llama platen does not have a plus plus operator to make a pleat plane what’s the negative of I what’s the negative of the negative of I the negative of what’s the negative of the negative of a negative of a

negative of i-5 very good I’m happy with that um I i did this demo all the time 50 percent of the time I get it right you know so I look like a god and I’m not but I just got lucky today therefore + + is syntactically valid but does nothing so don’t get caught you plus of course post increment is syntactically invalid so where was I don’t forget your + + that’s not how you do it you could do i get i plus 1 if you’ve been never programmed much at all use the shorthand operator whoops and that then gives me the result of alpha zonda r zarate but I’ve got to tell you I’ve got to tell you that’s just if you’re writing code like that I’m you’re in the right course because that is awful code that is awful that’s not really Python at all that’s just see or COBOL even done using Python syntax so if you’re doing that you shouldn’t be doing that instead well at least give me a for loop now just a reminder that the for loop in python is not a for loop the for loop in python is really a for each for each item in a container so a typical container if you want numbers could be something like that for each count or I in the range and of course the lovely thing is is that you don’t have to remember to fill not forget thee plus equals art range is used to create a list it takes a start and an end but every one counts from zero programmers always count from zero I have three kids numbered 0 1 & 2 that’s the way that you would count from there to there and then I get the right result well that’s one option but if you’re thinking like that you’re still thinking a bit too much like a sea programmer so an alternative would be to have something like for each model in models and then I can just print the model let me take those two out oops a dollar sign for my pearl friends and whoops preach models in models that doesn’t make any sense that’s better ah but I didn’t get my costs as it turns out it’s actually not a really pretty thing to do so I’m going to leave that bit there and come back to it when I’ve those of you with more experience should be thinking oh maybe a list of tuples would be nice maybe a dictionary could help maybe a list of dictionaries would be nice maybe a list of objects of class car would be a sweet solution but I’m going to leave that for now so where was it that was a lot that’s the essentials we looked at the in strings lists tests loops ah I didn’t do a function but if you wanted a function pop your function up there depreciate takes the cost that it was and returns the cost times whatever point 9 oops point 9 and of course if you’re doing that you would course of course would put a comment in yes of course we would good idea Pete and it always goes as the first thing and it must also be indented depreciate the cost passed to it or something like that um now I should let functions file handling I might come back to file handling because what I’m gonna do now is we need to look at the object model I i I’ve sort of been saying declare it’s not really a variable how does this stuff work a Python people need to know this so let’s go back to our shell and see what we can come up with here in the interactive shell if I set a to a list say a list of numbers one two three hey looks like that let me set B to a if we have a look at a it looks like that we look at be looks like that let me go and mutate or change a by appending afford to it say hey looks like that the big question is

what is B 1 2 3 is 4 1 2 3 1 2 3 1 2 3 4 not terribly certain anyone to 35 this is a crucial thing to know you must get this right and to explain it maybe i would say what does that do well that is the bit that actually creates the list this populates the the symbol table with a name called a and name points at for my C C++ programmers for C++ Java friends a is a reference to that list I could even do it like this so if I was to huh where’d you go I won’t do it yes i’m likejust sorry pop it over to here and have a look at him so a is a shall we say pointer at the list when I do begets a what it does is that if I mutate a that’s what I get and if I come back to my list B has changed but it’s probably wrong for me to even say B has changed B didn’t change in the same way that a didn’t really change a a a is B and I can prove that by doing um by using say the ID function to get the ID of the object that’s it’s gooey it’s globally unique identifier it’s not quite globally unique but it’s unique enough be has the same ID it’s got the same idea it’s the same object a lovely thing to know about which Python people should know about is that is statement is is just a glorious operator it tells me whether this operator with this object or this reference points at the same object as the other pointer is is something to know about I don’t use it a lot in real code but because it’s not used for equal equal it’s not used for equivalence it’s used to see is this object the same object is that object is they pointing it the same and yes they is pointing at the same object compare and contrast to if I am well now I realize that that doesn’t create a copy do I make a copy I’m glad you asked I guess I could make a copy by sea is a new list made of the first element of a and the second element of a and the third element of a and just because just for the fun of it the last element of a minus one for the last element minus two for the second last element and see looks like that it looks like the same that’s a is see no they are different objects up PS even though they’re different objects they are value-wise equivalent as i said i don’t use is very much in real code but it’s nice when we use it against a type that we’d called a singleton object so for example the nun object that some functions return is a singleton there’s only one nun object so I wouldn’t use equal equal for testing against none I’d use is for testing against none if you’ve got the singleton type so that’s how you can make a copy no llama how else could I make a copy well I could go and have a counter no Peter and create an empty list and then a while loop while I is less than the length of a um digo an append the earth element of a and then increment i increment I PS in the interactive show you can even do loops you can declare functions classes everything right there inside the interactive shell half the time you don’t even need to write a program for particularly for sysadmin stuff Saudi looks like that dee dee looks like that but D is a no it’s a different object it would make much more sense to have an empty list and then use my friend for each item in that list go and append that item I don’t need to worry about indexes get away from indexes and then he looks like that but he is a

no that’s okay a nicer way a much nicer way would be for me to take a step back and see if I’ve got a integer if i print num variable or I is I that puts a space in comma always put spaces in yum if you don’t want to space you do string concatenation concatenate with I I can try but you cannot concatenate string objects and int X because it doesn’t know whether to do arithmetic or string concatenation so instead i’ll use the str function to take the argument the int and turn it into a string which I’m then able to do concatenations of so if you make a new string using str if i happen to have a string that looks like a number but which is actually type function is not a number is a string to do some arithmetic print three times s of course you’ve got to spell print right Oh what well multiplication works fine for numbers I use that extensively when I’m now putting some I want 30 dashes and where you go but that’s not what i wanted i wanted to do arithmetic with s if I want to do arithmetic with this I’ll need to turn us into its int form or its long form or its float form which I can do with that so if i use str to make a new string PS i call that the str function and that’s totally wrong str is the constructor for the class str and builds a for my java friends new string object store object if stir bikes are new string if it makes a new int how do I make a new list anyone how do I make a new list the easiest way to make a list is list f gets a new list made from an existing list actually a new iterable anything through which you can iterate which includes list tuples dictionaries even strings if looks like that but if is I know it’s a different object wow he’s getting lots of applause thank you thank you very much so that’s how I can do a list and that’s a little bit about objects ah let me throw some curly ones at you if I is five and Jay gets IJ is five if I increment I I is now six what is Jay hmm 565 I’ve got a few 4 5 1 2 3 4 50 60 my friends the big difference here which is very different to what I did before is what does this line do i gets i plus plus equals 1 1 is really i gets i plus 1 which is really i get a shall we say new int made of that calculation that is i gets 35 or whatever j points at it good they’re the same object but unlike before I am NOT mutating I I is not a new teta ballabh ject that is it’s not mutated immutable ince in python are immutable you can’t change them you cannot add one to an int you can make a new int with a new value and if you do that what you end up with is that because you cannot mutate an int in sir immutable what are the other immutable types core types in Python entz floats the same Long’s if you’re on Python two tuples tuples drupal’s tuple

whatever are immutable and the other core type would be a frozen set all of the in fact we’ve hardly got any mutable types most of the types most of the things in core Python can’t be changed but the only thing or the only thing I’ve shown you so far is a list you can’t change a string therefore string concatenation is really slow in Python if you’ve got a lot of strings to concatenate don’t concatenate a whole lot of strings append it into a list and join it at the end 20 times faster if you’ve got lots of strings to because strings if you concatenate strings that has to build a new string so all of that was to say the few who went 45 for my j j is still pointing at the old value of 5 now this is this is like crucial I mean this is the 102 but that’s 101 stuff you got to get that which is why a python person always has a interactive shell what does that do whoa and use things like ID the ID function there is operator have I got the right thing the same the same difference the same issue applies when you’re passing arguments to functions if you’re passing an argument to a function in Python it’s always always always always no other choice but pass by shall we say pass by reference it’s a byref pass it’s passing a reference to that so if you pass a mutable type like a list to a function it can change it C programmers everything’s by pointers now there’s some argument behind the scenes about whether a reference in python is the same as a reference in other languages but the effect is it’s all by ref and there everything’s a name it’s just a name so if it’s let me let me have a little bit of fun I need a bit of fun sure yeah yeah yeah all right well now if I rebind so if i do J is I the different objects the ID of j it’s still pointing at the thing that it used to point at but I points at a new object which has a different idea it’s a different object if I then went and rebound the name j to point out will be a reference to the same object that I is pointing at well then we’re pointing at the same object now and we we is the same object and therefore but the thing to the crucial thing to note is that you can’t change an int therefore I plus equals 1 is really slow in Python but then Python people that are writing Python like Python should be written with a for loop and without using counters don’t worry about about that because actually I don’t right i plus equals 1 very much in Python if you’re writing a lot of I plus equals 1 yeah you’re doing it right I don’t think so I think you’re thinking like a seat programmer that sounded like an insult but there we go that’s the IDS um I’d see how we going for time good so if we’re doing that then maybe I should go and have a little excursion off into dictionary land square brackets make a list / ends make a couple d is a type couple sweet and the big difference with a tuple is if i try to append a forward to it the attribute are a couple object has no attribute up and you can’t upend to a couple I could prove that by doing a directory list of a couple and it gives me a list of all of the methods and attributes in fact really in Python we just talked about attributes so it’s that’s the list as strings of all of the functions things inside the tuple compared to the directory lists of everything in a list and you’ll see that lists have more stuff including off the side of the screen somewhere where’s your eye pinned insert index count pop to remove an item tuples conversely have basically nothing PS all this underscore underscore stuff is all the special stuff that means it’s got something special that actually tells me that I can add or concatenate two lists you can add strings you can a dense you can add one couple to another couple and it’ll give you a new tupple

you can add that is plus two lists dir another crucial bit of python that i use i get a new module I’ll import the module go hmm what’s in there do a dir I’ll import math yeah or import math I’ll import the math library hmm what’s in math that’s what’s in math dir it’s just a tiny little simple little function that I use all the time it’s not as good as help which gives you an alphabetized list with descriptions about everything or help on math squirt for example but dir is the quick and dirty if you know what sort of what you’re doing the first thing that you do as dir and that will give you a list of hmm what have I got here so that was all to say that dictionaries can be created tuples are the other thing about tuples is that you can’t change them did i tell you that tuples are immutable change the first one to 11 I’m afraid not it does not support item assignment well what i want though is a dictionary now dictionaries are created with square brackets sorry curly brackets and they’ll have a key peter is 21 james is 22 and john is 20 making different 19 there’s my dictionary now that’s interesting the first thing to note about a dictionary is that they are not i’ll say they’re not ordered because they’re not really ordered they do have an internal order that they will respond to their actually ordered by the hash of the key the key must be an immutable type that is a string typically joy of dictionaries so we’ve got strings we’ve got keys and values keys and values keys and values but the order is not necessarily alphabetic as that shows you and p.s that order can change from different versions of python different implementations apart and use different hashing algorithms different keyed orders you don’t use a dictionary if you wanted in order although I can go and extract the keys of the dictionary with the dear dictionary give me a list of keys if I’ve got a list of keys I could have I could even go and throw that into a list k is a list k go sort yourself if I sort the list of keys now I’ve got a sorted list of keys I could iterate through the ordered list of keys and extract the items out of the dictionary in order so how do i expect the night amount of a dictionary i use square brackets you create them with curlies but with both lists tuples dictionaries you use square brackets and it is case sensitive to extract the data so the nice thing going back to where I was a long time ago is that own two independent coordinated lists makes extracting the data really mucky really hard not pretty but it would be much better to go and create a dictionary of cars just make it an empty one to start get the model get the cost and then here I can’t upend but what i can do is dictionary key of the car type or the car name value the value oh dear what am i doing their value the value and one of the most beautiful things are reckoned at a beginner’s perspective on python is the ability just to dump an object pretty print it just dump it and you’ll get something useful so if i come back to where i was and run it oh if I that sorry should have been more dill and there we go there’s the dictionary the fact there are phurba tized just lucky I guess if you wanted a little prettier than that I could go and get the get the keys of the car names dictionary get the keys if you’re in the mood for it you could names go and sort the list of keys and then I could loop through for each car

name in the list of names go and print cars go and print the name of the car and the cars and use that key to look back into the dictionary I think I got that right and there we go kaching kaching kaching I different lines there obviously because it’s print if you wanted the same line at a trailing comma stays on the same line I could abbreviate that the saw Ted function would be nice in there big difference between sort and sort head p.s great great mistake for people to make this is that this is a top mistake if you want to if you want a good mistake here’s a here’s a good one um names gets names dot sort that’s a brilliant piece of mistake right there nonetype object is not iterable ah yeah good what’s wrong with that names dot resort does not return anything well actually it does because everything has to so it returns the nun object it returns the nun object to say don’t do this it sorts in place it mutates the list um there we go so that is a dictionary joy of dictionaries why they’re so good and they’re highly efficient now I’ve actually run long for my break so I think now is probably a good place to break take a deep breath let’s take a break for say ten minutes and when we come back I think we need to make our own classes make our own types

if it has that better there’s the models I could also create accesses to get costs get and I shouldn’t I could also have setters um however what I’d actually do I’m actually going to change this so print how you convert yourself to a string and another one of the pretty special methods of a class is the here’s what I look like as a string it must repeat must return a string so I could return myself model concatenated with a space concatenated with the this is what it looks like as a string the selfs cost and there my friends is a sweet bit of sweetness so now I’ve got a class in real life I would put this class class this class in its own file from cars import car and to get it out of here into a multiple source file or i’d give it to somebody else i say you you going right for me this class that takes these arguments in its constructor stores the data however you feel like it and returns a whatever so now this is probably the best solution that i’ve got for this service I’ve got a I’ve got a list of objects of class car and oh by the way I don’t even have to there’s an implicit call to stir on print so what I can in fact do is print car and that’s better the concept of object orientation is to bury the complexity down into the class it should be look after its own self which is what that does so I that’s why I would want to make my own type some simple example so I’ve got a list of dictionaries there I didn’t need to mention on um classes and lists I’ve got a list there I’ve got a list this is a list of what ince yep but really behind the scenes it’s a list of objects so a list is a list of objects it’s a list of anything because in Python everything is an object if everything is an object then I could have another list which is a list of a list of one two three four five six that’ll do if I had a look at the Len of B what’s the Len of b2 there are two items in B b0 and b1 I’m really upset with the ABC for their terrible misconceptions that they’re giving kids it should be b0 and b1 not b1 and b2 that’s a and therefore now I could call this a two-dimensional list but in reality Python doesn’t have two dimensional lists it’s just got a list of objects and each of those objects could be a list and each of those lists could be a list and each of those could be a dictionary with a key of a string and a value of a couple which has all the way down what is b1 2 for a t-shirt yes at the back 6 I should check shouldn’t I 6 Cody can you take that to the man in the plaid shirt at the back um list of lists so I’ve kind of been just scooting through this and we saw creating our own types the special methods underscore underscore in it underscore underscore stir some more complex examples their inheritance is what’s happening here but there’s nothing to stop me inheriting from some

different type like I could have a closed us vehicle which it derives from object and does whatever or nothing and then car would inherit from vehicle and that way I can build on types um I should cover some of the standard library we’re running we’re running close to the wire so let me give you the best resource which is the help now while I’m a bit of a fan of Windows sorry Microsoft the help that comes with active python is actually quite nice because it gives us the Python standard library offline I can just run up here you also find all the documentation at python.org and here we have the the built-in types such as all of the types boolean types truth etc we have strings we have numeric and mathematical modules such as math for my mathematicians complex math etc file and directory access with os os path etc data persistence store the data for next day including SQL data compression yeah he zipped are all of that sort of stuff built in the batteries are included file formats CSVs etc the Crypt support is actually not great but we have the hashing algorithms operating system services internet dat’ll data handling emails Jason you name it base64 conversions all of that’s all built in Internet I can suck stuff down from the web push stuff off in fact Python even comes out of the box with a web server so I’ve got everything I need to really do stuff right right from here and even gooeys so if you’re if you’re creating a web front-end you wouldn’t use that but if you’re doing a little app for somebody to use the TK inter module so Russell’s actually doing a good talk on that tomorrow so I encourage you if you want to know more about gooeys have a look at that or built-in all comes out of the box so that brings me to question time are there any questions at the very back sure the output loop for my car program has oh yeah sorry the question was he wanted to see the output loop for the program there are cars plural is the list of dictionaries but Pierce it’s actually just a from my perspective looking at that it’s just an iterable that is it something that I can iterate through that is it something I can for each item in the container for each car in the iterable line 45 car go print yourself that is it’ll call the implicit sorry for my bad english but that’s really what it does in the object world car go print yourself that is called the store method on the car object away we go at the front ah why wouldn’t you good point no there might be many other things that’s where you would make access ors what we would call getters like get model get and other getters so I could get model and I could get cost and it would return the cost maybe it’ll return the depreciated value of the cost using a global function previously created and then I could say print car model sorry get model kept model is now worth car dot get cost ah and I missed a semicolon there now worth only ninety thousand point zero because it’s a float um you can’t so no nope and Python 2 is

a bit hampered with print as others have said print print is a keyword but it should really be a function because we can’t really do good stuff with it I can’t easily say I’ll print this to a file I could do it using redirection at the operating system level but yes I in which case i’ll probably wouldn’t use print and in a lot of my real life ones i’ve got dumb printers which is my function which would normally print it that sometimes prints it and writes it to a log file or does something else in the stripy jumper ah thank you thank you through the north of you I was just wondering if you could maybe explain a little bit about self and like this ah herself how it works well what happened was the god created us and they’re the the most valuable thing that we have is our consciousness our where awareness and our choice the fact that we can make choices sorry that probably wasn’t what you are asking I’m happy to talk about that afterwards um the self is the reference to the current object so that is it’s me so it’s how I get to my data that’s stored in in me so in other languages like Java and C++ in Java you’ve got how do you get the reference to the self it’s that this reference and in C++ it’s the it’s also called this but it’s a pointer to the self but they’re hidden they’re not shown we’re running a little tight there but um what I would mention to you is very important that you’re all familiar with the Zen you all familiar with the Zen yes oh yes you’ve all imported this anyone ever import this it’s this is the Easter egg that we’ve got also known as the Zen of Python the first one my favorite beautiful is better than ugly why married my wife not sure why she married me but the second one is what I want to draw your attention to to answer this question which is in Java and C++ the reference to the self is hidden it is there but it’s not shown in Python to follow that Zen to follow that shall we say rule or philosophy we have that explicit is better than implicit question at the front say something about memory allocation obviously the way that it ought memory allocation tables I can’t remember yes good question excellent question if I had a look at um if I had a look at if I had a brain sorry i won’t singing singing is extra that’s in the deluxe course don’t get a lot of call for that around here this situation I points that that be points at that so there’s no extra memory allocated when i make i append there’ll be a bit more memory allocated therefore the what’s really a linked list be points at that when innocent scenario jay is still pointing at the number three so its memory hasn’t been deallocate so i haven’t actually had any releasing happen yet in python what happens there’s the zen if i’ve got i gets three here’s the replication of that Jay gets I pointing at the same object I points out a new object JS there’s been no memory released yet here though if I now make j point at something else then the pointer back to the number three the number three object is now no longer needed no references pointing at it and therefore python uses a not terribly sophisticated from other languages perspective memory allocation reference counter and it says oh I can’t reach that the reference count is reduced to zero and now that object is eligible for garbage collection like Java there’s a background task that runs all the time cleaning up you can import GC and do come in but GC doc verbose or something to get more information about what the garbage collector is doing yes so the

the the the philosophical answer to the question is firstly Python programmers go and care about memory it’s just if you’re running out of memory you just buy more memory is cheap programmers are expensive if program if pythons running too slow you just get a faster computer it’s much faster much easier much cheaper to get a faster computer than get a rewrite it in C++ one Python programmer can do in six months what to C++ programmers can’t do in a year however I’m being a little facetious there if this was something rather large like the range of numbers from one to some large number then there’s actually been a fair bit of memory allocated for my C programmers typically C Python 2 points something is about 20 bytes for on 32-bit for an int ouch so if i go and allocate whatever that was 100 million that’s a lot of bites so if you are wanting to release early which is a noble thing to do then you could do that the simplest way the most obvious way is to point to the object that we’ve always got access to which is the nun object and then that says nobody is now pointing at that unnamed a large list of integers and now i’m releasing my memory questions in the white and brown stripy shirt i often find myself writing a lot of function wondering when i should use a class instead and kind of vice versa and i was wondering if you had any comments on that yes um global function so what I would call so the question is about when would I write functions when would I write classes this is what one might call a global function it belongs to the globe or the module in one sense you in one sense we don’t actually even have global functions in Python a function is a member of our it’s not a member of a class it’s a member of a module and modules actually look a lot like classes in fact is really it’s a little blurry when I start looking at it because the standard declaration when I do you know if I import the math module math dot square root looks a lot like object method so in one sense that method isn’t that function is a method of the module as opposed to a class but to take it back a bit the thing about classes is that you’ll you you need to start thinking about classes before you start coding now I’ve done this exercise before so I sort of knew where I was going but when you start examining your real life system or the real life system scenario that’s when you start going oh well where are the wearer the class what are the types of things that I’m dealing with oh I’ve got a amorphouse car thing which has things that it can do and things that it stores and stuff I can pull out of it and that’s my in the internet well we’ve got a packet and a packet is a thing that has things in it and a source IP in a destination IP and it has a body and a CRC and a that’s in the that’s the thing so I should really be thinking about classes a lot for I start coding and going what are the types in my system that i need to do questions in the white shirt it seems as though a class should have other classes in it yes yes absolutely absolutely absolutely indefinitely so what I’m this is a very simple class although I do have some inheritance happening I’m inheriting oh this is a specialized class of this other class which is a more generic base class so vehicle is a generic class and car is a specialized form of that now that relationship between car and vehicle is known as a is a relationship so a car is a vehicle or is a specialized type of a special as example of a specialized one of however the model and the cost is an example of the hazar relationship a car has a model

A Car has a cost sorry I said that wrong the a car is a model in a car breathe a car has a model and a car has a cost model and costed just objects though so a car could have a ECU and that ECU is a object of a class mmm control computer so the only difference between an int and a object of class control computer is well really nothing they’re both objects of a certain class it’s just that entz come with python and control computer does not come with Python earlier I said that car is a vehicle it’s not car is like a vehicle so what I should have said was that the car class is like a and follows the is like a relay chionship my car cars zero my car that is a object of class car at the front probably the last question i think a quick question um when i create a class and instantiate the object yes um it looks like it’s mute mutable so if i make changes to that object references to that object it’s still the same object yes how do I make it immutable and what are the benefits of having an immutable ah good question excellent so the question has to do with the mutability of my objects and to answer that let me examine the car class that I’ve created and so the question looking at that is is the car class mutable or immutable at this stage it would look like and it would be reasonable to say that it’s an immutable class because there’s no methods like dot append sort reverse that would actually be able to change this notice that I cannot change any of the data in this class it gets set in the constructor I have no setters I’ve gotta get cost but I don’t have a set cost yeah yeah exactly exactly now the thing about these is that I’m saying at first appearance it looks like this is an immutable class and it can’t be changed however particularly for my Java and C++ friends we don’t really have private public kind of thing happening in Python so I can even create classes on the fly so I could go and create a class called see with the contents of our say nothing I can now instantiate that class c looks like that but I can in fact even go and create properties that is attributes on the fly cidade age equals 21 doesn’t actually change how it looks I could go and throw in and you I’d have to use a lambda function make a new method in there that would be a little bit gnarly but that object is mutable I can change it on the fly um to the second part of your question is it worthwhile doing probably yes cuz why are tuples immutable cuz it just it’s just you can’t recommend and sometimes they give rise to efficiencies that might not be possible in otherwise the best way to do it is probably not like this you can hack around with the get at ER and they get attribute methods this underscore underscore set attribute method that’s a bit mucky the best way to do be to inherit from something that’s immutable like couple or further research for you post course look into the ABC module the abstract base class the abstract base class call class since 2.5 I think it would be the best place to start looking too good answers to that okay last question actually not last question unless it’s quick ah clone an object clone oh that’s how you do it

in Java um we don’t really have such things as dot clone we don’t really have such things like copy constructors but you can we don’t have overloading of operators sorry overloading of methods so you can’t overload based on type so I can only have one constructor but you could look you could do a bit of fudgy stuff with the constructor to build a new object from an existing object by looking at did I get an argument default arguments is something to look at in that as well thank you okay well old thank me oh thank me health join me in thanking Peter for his incredible presentation

You Want To Have Your Favorite Car?

We have a big list of modern & classic cars in both used and new categories.