(Blog) Object-Oriented. Python basics

Two things to know about Python, and why you should know them, before you do anything else:

  • Python is an interpreted programming language

  • Python is an object oriented programming language

Python is called an interpreted language because an interpreter (not a human, but the Python program itself) reads your script line by line as it executes it. This is very useful for beginners to programming because it allows the user to analyze their code at each step. Other examples of interpreted language are Ruby, JavaScript and PHP.  

Comparatively, compiled language (C, C++, Go) programs are read and examined entirely at compile time by the target machine. This means you can not execute the code until a program is completely written. This requires that if there is an error or a change is made, then the entire program must be rebuilt. Granted, compiled language is much more efficient for the computer to execute code. The process of interpreting language during execution requires more processing than simply executing it.  However, if you are beginner, then an interpreted language is preferred in order to adjust your code bit by bit as you make your program.

The second thing you should know about Python is that it is an object-oriented programming language. This usually throws folks for a whirl until they begin to use the language. It is easier to comprehend when you start see how objects are made and used. The most common object types are: 

  • Strings(str) 

Words, text digits or specialized characters.  

  • Integers(int) 

Numbers without a decimal (no fractions). -3, -2, 1, 0, 1, 2, 3…. etc.  

  • Floats 

Numbers with decimals (aka, real numbers). 2.2, -1.0, 5.758391… etc.  

  • Bool 

True and False 

Object-oriented programming is the method by which we simplify and minimize the amount of programming needed. 

Let's say you were writing code to create numerous smaller tables of data from a dateset and then make charts from those tables. You could write out the step-by-step process each time, God forbid. Or, you could write the code once, define the variables(aka, properties), and define the methods as objects. Then, the next time you wish to perform that same step, you simple call that object which you already made. Objects are made in order to be reused.

There is one more level of hierarchy to mention. An object contains properties and methods that make a type of data useful, and it is stored within (or, belongs to) a class. Think of a class like you think of a student belonging to the Science Class, or a frog belonging to the Amphibians Class. The Science Class would be made of the students (object students), the desks (object desks), the posters (object posters) and teacher (object teacher).

Now let’s say we wanted to create a program that performed chess. We'd define a Class and call it Chess. The Class: Chess would include the pieces of chess, the board setup, and the black and white players, as well as, the methods in which the objects interact with each other (i.e, how the pieces move along the board). The pieces of chess (Knight, Queen, Pawn, etc.) would be defined as objects. Each object, e.g. Black Knight, would carry within it all the information about that piece: 

Creating Class Chess with Objects Pieces

Creating Class Chess with Objects Pieces

Object Black Knight

  • Color: Black 

  • Shape: Horse 

  • Movement allowed:  Two squares vertically and one square horizontally, or two squares horizontally and one square vertically 

    With those two things in mind. You’re ready to understand more concepts like tuples, loops, functions, etc!!! Exciting…

Elizabeth OlstroemPython