Programming languages are weird, and I’m going to tell you right now that learning a programming language are not for everyone. Programmers have a saying”

There are only two hard things in Computer Science: cache invalidation and naming things.

PYTHON

“Naming things means finding a good descriptive name for your variables, functions, procedures…etc. Even seasoned programmers make mistakes here from time to time as it’s really easy to fall into the rut of using the same names over and over again without realizing it. This is why many computer languages use Hungarian Notation, aka “Variable Capitalization” which capitalizes the first letter of every word in your variable name. It also helps IDEs understand what types are being used in strings or integers or floats or Booleans or strings. Learning a programming language that doesn’t use Hungarian Notation, like Python, will require you to pay much closer attention to how you’re naming things.

PYTHON is not only named weirdly but it’s also designed in such a way that makes it seem weird when you try to learn it after learning other languages like C++ or Java. Many people get turned off by all the strange white space rules and the keywords being full words rather than abbreviated functions/methods made up of 2-4 letters. It all sounds too strange. Trust me, I know! But once you get past the initial hurdle of figuring out what’s going on with PYTHON’s design decisions there are many, many benefits to using it.

One of the most powerful design decisions about PYTHON is that ANYTHING can be executed as a piece of code (called an “expression”). The most basic type of expression in PYTHON is the assignment statement: variable = value. This might seem simple enough but all sorts of things are expressions in PYTHON. For example, you can make another assignment inside an if statement or pass variables to functions/methods or use them as arguments or put them on the left side of an assignment statement. So what’s really happening behind the scenes when you do this?

Wherever there is a variable name THERE IS A COPY OF THE VALUE BEING ASSIGNED

Everything in PYTHON is an expression which means everything gets copied when it’s used. Want to print something? There’s a function for that!

Print (value)

This prints the value inside of quotes after being converted to a string. If you want to have multiple things printed with one line, just put them all in quotes and they’ll get printed out one after another when the function is called. For example >>>

>>> print (“A “,”BC”) A B C

Also if you try to use a word that doesn’t exist in PYTHON as a variable name or keyword, it turns into what is called an expression statement:

>>> If this does not exist if this does not exist… Trace back (most recent call last): File “<stdin>”, line 1, in <module> Name Error: name ‘this’ is not defined

The reason why the above code tried to run if this does not exist… (Note that if is a keyword) was because Python interpreted it as

If (expression-statement)

Here’s where the power of PYTHON starts to show up. You can actually put multiple expressions inside parentheses and make them all be expressions! That means you can turn one statement into two or more statements by putting it inside an if statement. Let’s try to make our own version of the print function. >>> Def hello (): … print (“Hello”) … >>> def world (): … print (“World”) … >>> hello () Hello >>> world () World >>>

Above we defined two new functions, hello and world that both print to the screen. So what happens when you call them? When there is more than one expression inside parentheses it runs them all in order from left to right. The results of each expression get copied onto a stack which is a list of values. Then when it’s done running all the expressions it pops each value off the stack in reverse order and assigns them to whatever variable names were around before starting the if statement. Now you might be wondering why anyone would want to make yet another function just to make printing things easier. Well you can actually change this behavior using something called a “nonlocal declaration” but that’ll have to wait until another article.

HERE’S WHERE IT GETS INTERESTING!

Let’s take what we’ve learned so far and apply it to something simple like writing a for loop for getting input from the user. A variable in PYTHON is created when you give it a value of any kind. In fact everything in Python is an object, including functions/methods! This means if you want to say “use this function” or “call that method” you can actually use the functions/methods themselves as arguments which is called “passing by reference”.

Conclusion:

Functions/Method is objects so you can pass them by reference!

This is really powerful because it means you can write functions to do different things depending on what function/method they use as an argument. This works even if you call the same function with different values each time, or if the function takes multiple arguments.

Leave a Reply

Your email address will not be published. Required fields are marked *