Intro to Python ? – Part 2

You can find Part 1 here.

Intro

In this part of the series we are going to cover the very basics of Python. You may be asking: “Its Python, how hard can it get ? Its just sudo code” well that is both true and false to a degree.

Variables

Python is a dynamically typed language, however it is strongly typed. This just means that you can set a variable to any sort of data type you want without declaring it first. However it doesn’t implicitly convert types for you.

Collections

Python also contains different types array like structures. And they are all get dynamically sized of course. You have Lists and Dictionaries, they all work the way you intuitively think do, coming from another programming language. The only new sort of collection type, you will encounter is the Tuple. A Tuple is pretty much the same thing as a List however they are immutable and can not contain repeats of the same value.

Control Flow

It’s pretty basic.

Loops

Python does not find “for” loops pythonic that is way it favors “For In” loops instead. maybe asking: “What if I just want to print something 3 times ? do I have to make a list with three elements ?” The answer is no, you can use a “generator” shown in the gists below. Python also has something called “list comprehension” its very nifty to write compact and concise code.

 

You can go here to find all the relevant code, in order.

Going from Apple fanboy to hater to fanboy again

When I was kid just going to primary school, I never knew what a computer was, how it worked, or what it could do. My family had a computer at home but it was just a big grey box to me. It just sat on a desk.

However on my first day of school we had time with computers, we had this really great computer lab full of macs, and we had carts full of mac laptops. They looked cool, and you could do a lot with them. Even when being a 6 year old kid, I could appreciate the design and work that went into them. It felt like someone put in a lot of time to just make sure everything was perfect.

So after using them for awhile, I asked my dad if we could get one. I went into full sales pitch mode, and told him about all the cool things that it could do, and how well it was designed. However being new immigrates to Canada, that computer was out of our price range, and he said to just use the one we already had. My dads response was, “Why don’t you use the computer we already have ?”. And that computer was a Comqa running Windows 95. The experience of using that machine made me into an even bigger Apple fan.

As the years passed and I got to high school I still hadn’t owned an apple product, and started to rationalize why I didn’t have an apple product. I chalked it up to the good old Marxist ideas of the “haves” and the “have nots”. I was a “have not”. So my views on Apple changed from a being “Its such a cool company that makes awesome products” to “They make play things for idiots that choose to buy it”

That ideology lasted for a pretty long time, till I started making money. A year after my first internship I had quite a bit cash saved up even after paying for university. So in my second to last year (or what I had thought to be my second to last year), I called my bank, raised my credit limit, and bought a bueatiful 2016 13′ MacBook Pro. And I was a fan again 🙂, to long had I been using a windows machine.

What most people don’t realize is that Apple really cares about two things, build quality, and the little things. That laptop felt like a heavy solid chunk of metal, that just happened to be a laptop. The build quality was insanely good.

When a person pays a crazy sum of money for a fairly basic computer, most people are would say it’s dumb to do so. Others would say maybe he or she is just that rich. Most people equate value with quantity. However in Apples case, it’s build quality, and effort put into places you wouldn’t even think of.

And that right there is what Apple is unapologetically all about.

OOP Explained

Preface

OOP is a way of thinking / design pattern. It can be understood by way of a number of different analogs. Not all the analogs described here are perfect but it makes some of the concepts easier to understand.

Blueprints:

Blueprints tell people how to make things, from your stove to your car. It has a clearly planned out way making sure everything works the way it is supposed to, and makes sure it satisfies it purpose.

You can think about “Classes” in the same way, expect what it builds is not something material, but an entity in computer. So if you wanted to make a car class you would thing about the things it should do. Such as accelerate, brake, turn on and off, open the door, and etc. Notice how all these things involve a verb , these actions that the car can take can be thought of as the methods in class. And then you have the stats of the care, such as the make, model, owner, and etc. These are all nouns and can be thought of as the variables of the classes.

There for when you instantiate a class, your telling the computer to build that objects using the guide (“Class”)

Collections of things:

People collect a lot of different things, from action figures, to stamps. These things all have common figures. Action figures are usually of a fictional character, are made of plastic, can be moved into different positions, and are usually a certain height .

And classes are the same, they hold variables and methods that come together since they are all kind of related. For example a coin classes could have a variable called “metal type” and a method called “flip”.

Cookie Cutters:

Cookie Cutters let you take cookie dough and make interestingly shaped cookies. Which is same thing as what a class does, except the cookie dough is your computers memory, and the cookie cutter is your class. Every time you instantiate a class,  your take some memory forcing it through the classes and making a object.

Why I don’t like Python

Yes I know that statement above is pretty much unheard of. Before I explain, let me give you some background.

So as we all know there is no such thing as the “best” language, i.e the language that you should use for everything. Each language has its own uses in its own specific problem domain. Since each language has its own unique strengths, weakness, and paradigms that it caters to, like Java with OOP or Scala with functional programming.

For example if you want to write fast, highly efficient code that can run on embedded systems like those found in a CAN-Bus hub, you should use C, C++, or assembly. Where as if you wanted to write super interactive web applications you should use some form of Java Script, or something that ends up compiling to Java Script.

However in this day and age, almost every single widely used programming language is multi paradigm and can compile to something that can run on multiple platforms.

So the question is “With so much selection, why use Python ?”.

Python is a language that is almost universally loved by small children, to Google engineers. One of the main reasons for it adoption and wide use is that it’s syntax looks like pseudo code. Which is a great thing, since it lets you tell a machine what to do in a almost human like language. Thus the difference between what you want the computer to do, and what you have to write in order for the computer to do it, becomes minimal.

My biggest problem with Python is that it forces you to write code in a certain way. The pythonic way aka the thoughtful, zen like way, stressing “less is more” and “clean code is good code”. This way of programming does not suit us all. Some of us are cowboys or cowgirls, we shoot first and think later. Us cowboys and cowgirls like to really experiment with what works and what does not work, we really like to tinker with our code till we get it right.

Other languages don’t really care, they know that they are. They are simply a means to an end. You can be that gun-sling cowboy or cowgirl that codes before even having a good idea about what he or she has to do. You can get into a stack-overflow frenzy of copy pasting code, ripping out what works and what doesn’t. And you eventually end up with something that works. You may ask “can’t you do that with python too ?” well kinda… see python does not have the greatest piece of programming syntax ever. It doesn’t have the “}” . So if you trying to be that cowboy or cowgirl you will end up with parsing errors galore or end up have to re-indent all of your code, just to get it to run.

Honestly I think python would end up being the greatest programming language ever, if it just had curly braces 😦