Coding2: Variables, Comments, and Interpreting Errors.
Now talking about how to do variables, comments, and understanding basic syntax errors in R & Python.
As of right now, we have successfully installed R/RStudio, and Python3/Pycharm. We have also learned how to use the print function to get the scripts of code to produce an output. In this post, we’ll introduce the concept of variables, and comments. This is a good introductory post to all future programmers.
For the coding series, we'll primarily use 2 programming languages:
Python
R
Table of Contents
Variables
Comments
Interpreting Error Message
Exercises
1 - Variables
Imagine for a minute you were BowTiedOx. The big Bull man charged you with the task of coming up with an equation. This equation will tell us what a person’s weight should be, given a bunch of information about them.
Some of the information you will need about a person would include:
Gender
Height
Body Type
BMI
Strength at the gym
etc..
Let’s say you are able to spend hours reading research papers on this. Now, let’s say there was a way to store the number you come up with somewhere.
This wayyou can pull it & use it for calculations later on. The ability to store a piece of information on your computer, and then use it later on is what something called variables. To make a variable, we need 2 pieces of information:
The name of the variable
The piece of information it is storing
Sometimes, when we are coding, we will want to grab a piece of paper and basically do some dummy coding. Dummy coding basically means writing some pseudo (fake) code, to get an idea of how everything will fall into place.
1.1 Python
For teaching purposes, I am using an online version of Python: https://www.programiz.com/python-programming/online-compiler/
I recommend sticking with PyCharm for now, as it will get you more familiar with the UI you will use at your role.
In Python we can use the = sign to create a variable. So, if we wanted to make a variable called ‘Mr_Oxie’, and give it the value of the number 69. Here’s how we can do it in Python:
+-COPYSelect LanguageBashCC++C#CSSGoHTMLJavaJavaScriptJSONMarkdownObjective-CPHPPythonRRubyRustSQLSwiftTypeScriptYAMLMr_Oxie=69
Now, if we wanted to use the variable that created and stored for calculation, all we have to do is call upon it:
+-COPYSelect LanguageBashCC++C#CSSGoHTMLJavaJavaScriptJSONMarkdownObjective-CPHPPythonRRubyRustSQLSwiftTypeScriptYAMLMr_Oxie=69
print(351+Mr_Oxie)
This would give us the answer of 420.
Here is a step by Step visualization on how Python will approach the statement:
print(351+Mr_Oxie) ← Since Mr_Oxie has the value of 69, Python will replace the variable with it’s value, and see if it can simplify this further.
print(351+69) ← At this point, this would be a simple math expression to solve, so it will give us the answer of 420.
Here is a screenshot that does all of this:
Note: The = (equal) to sign is sometimes referred to as the assignment operator.
1.2 R
Doing this in R is literally the exact same. Observe below:
Make the Mr_Oxie variable, then print it with +351
2 - Comments
For teaching purposes, I am using an online version of R: https://rdrr.io/snippets/
I recommend sticking with RStudio for now, as it will get you more familiar with the UI you will use at your role.
When we create complex code, and we come back to it several months later. We will sometimes forget what the script we created did. So, if there was a way to write out simple words in English that the program ignores. But it is useful for the human, that would be great.
In fact, this thing already exists, they are called Comments.
Note: Make sure you speak with your boss so you know what the proper procedure to document your code is. If you know the procedure ahead of time, you can implement the documentation fast. This’ll cause you to lose precious time that you could be using to help make the company more revenue => Bigger Bonus.
2.1 Python
In Python, we use the # symbol for making comments. Anything you write after the # Python will completely ignore, and you can pretty much go crazy here, and say whatever you like. You can look at the screenshot below for an example:
2.2 R
R works the exact same way in Python. For now R has been pretty much a duplicate Python, but the divergence is coming soon…
3 - Interpreting Error Message
Luckily for us, the errors we get in both Python, and R are almost identical. This means, if you know one, you know the other. In this example, we'll do a simple example of invalid syntax, and you can see that the error tells us exactly where to look to fix our code.
In this example, I'll get Python to make a variable called Raptor, then I'll make a print statement that will execute on it. But, I will use PRINT(, and not close the brackets. This will cause an invalid syntax error to pop up.
This error makes sense, because the rules are if you open up a parentheses, you also need to close it. You can also see that the error message tells us exactly which line to go to.
4 - Exercises
Here are some questions you can try out for practice:
Make a variable called best_number, and give this variable the value of 69
Now tell me, what is best_number divided by 1.25
Write a comment that has the answer after you are done the step above
Click Here to go to Coding3: All about strings.
I quite enjoyed the specific example of an ox being stuck with code skills not ample
55.2 ✅