Getting started in python

Gregory Ybos
3 min readSep 25, 2021
Photo by Timothy Dykes on Unsplash

If you are making the jump from another programming language, I have good news for you! The gap between the two is smaller than you may think. In the following paragraphs, I will be creating a coder class object and illustrating common functional programming operations, in Python! Overall the differences between the syntax of each are minor but some key concepts differ between python and other languages. In this article, I will be comparing Python to another beginner-friendly language, Javascript.

The first and most obvious difference between the two is that Javascript uses curly braces to define scope while Python uses indentations. Secondly, Javascript does not use semi-colons to denote the end of each line of code. To start with we will instantiate a new Coder class and define the constructor function using “__init__”. Python allows the programmer to reference the instance of this class using the first argument space in __init__’s arguments, similar to Javascript’s “this”. When this class is instantiated the first argument will be ignored and only additional arguments (name in the example above) will be required.

In addition to the name property, we have added coffee level property. Next, we will add a refuel method to our class, once again passing in the self-referencing argument. You will notice the syntax for conditional statements is very similar to Javascript, simply with fewer brackets and curly braces. The “print” function works the same as console.log. You will also notice that python does not have a distinction between strictly and loosely equal such as Javascript’s “==” and “===”. Python’s equality comparator only works in the strict sense.

We next add a skills property to our class. Now we come to our first major departure from Javascript. The closest analogy Python has to an Array is a “list”. Python lists are ordered (indexed), changeable, and allow for duplicate values to be stored within. The syntax for these mirrors Javascript’s arrays. But wait, there is more! Python also uses tuples and sets. Tuples are similar to lists, however, they are unchangeable. Sets are the most different in that they are unordered, unchangeable, and do not allow duplicate values. Within our learn skill method we are using the “in” keyword to determine if a skill is already existing the list of skills we are storing in the class properties. Below that, on line 18 we are type-checking that our passed argument is in fact a string before appending it to the skills list.

Lastly, we will add a sleepDebt property with a Python dictionary. A dictionary operates on the same principles as a Javascript object however you will notice the keys are also wrapped in quotes to denote a string value. In addition to this accessing the key/value pair requires bracket notation to access. The sleep method chooses the correct values to add/subtract from to show the current coder’s sleep debt/credit. Once again notice the use of bracket notation to access the key/value pairs of the dictionary.

--

--