Python: Basics

If you are continuing from the last guide, you setup a workspace and learned about projects and modules. If you already have a workspace and you want to learn about Python, this is a good place to start. For starters, there will be some references to terms used by coders, and I will define them, if not right away, then in a small description after. We will start off simple, and move quickly into many modules with references to each other, creating a project that does something. If you were following my last guide, you already have a project created, with a module create. For everyone else, you may create a project and module and name it whatever you like, however understand that I will be referencing the names in MY project.

For starters, we will write some comments. Comments are notes written from coders for either other coders or for themselves when they return to their project. In Python, this is done with either three single quotations, comments below it, then another three single quotations between. This is the multiple line comment format. When you have a hashtag, and a comment on the same line, this is the single line comment. Both are equally applicable, but for simplicity, the single line comment will be seen often within my projects.

The compiler, as shown in the image above, is what reads and checks the code

As a strong recommendation, if something is not obvious at first sight, comment above it. This is good for describing method or class purpose and describing actions at certain locations.

Next, we look at variables. This is key across any language to understand, as many of the data types you can find are global and some do not exist. Variables are a place to store data of a specific type, be it number or letter, or something else. In Python, we create these data types by giving them a name or alias to call upon when we want to use it. Simple, but as fun as it might be to enter something random and funny as your variable name, it is useful to name them based on the data and what it is used for. The variable will understand what the data type is and store it in the program. As an example, create a variable from the project we started by entering something like someInt = 25. Again, simple.

This can be continued to add the examples like those shown to the left. But now you are wondering what is difference between some, if not all, of the variables and values. Why can’t they be the same thing? Simply, because they can all do different things. Some variable types to use are int (Integers) which is a whole number value and only a whole number value, double (decimal numbers), char (characters) which are not specifically of the alphabet but of ASCII values, and String. The ASCII values are translated values used universally in languages to define a specific character such as a space “ ” or characters “-” or numbers or letters. If you want to know more, go here. Strings are a “string” of characters stored as one. Look more into primitive variables when you have the chance.

As seen above, to create a char variable, use single quotations around the value. When using a String, surround it in double quotations, and be careful when using backslashes (“\”) as the compiler uses these for different things within a string. The compiler will know the difference between an int and a double when you use a decimal in your number. Also, you cannot have these values flipped! 25 = someInt is invalid, as you cannot assign a number to an alias. In other languages, however, you can create components at seperate times. Just type the name and on another line, type that alias again and make it a value!

Another thing to note, the two components to this is called declaration and initialization. Declaration is creating an alias, while initialization is giving it a value. So as mentioned before, you can declare a variable name, then initialize whatever item to it. This can only be done in other languages, though.

What if we want to print these, or more specifically a math statement, to what is called the console? We use the method print()! Just enter a variable name in the brackets, or write a String if you would like. You can even write both, just add a plus sign between the two!

What’s this, An error? I lied to you?

Well, there is one more thing. For int and double variables to be printed, we have to tell the compiler that it is going to be a String with str(), where the value is in the brackets.

It worked!

Alright! now we can make values and print them! Let’s do a bit of math! What is (3*4)/2? That’s pretty simple, right? we can create three variables, then create a math statement like this, then either save it and print it or use it in the print! Here is how that would look:

Also added Strings to help read the output!

As you can see, they print out a number, the same one, so that must mean the math statement is right! But why did it make it a double, a decimal? Python is a little exclusive when it comes to doing math. Python keeps its values as doubles if divided for better accuracy. If we wanted a number without a decimal after division, we would actually type // where we are doing division for a “floor” value, or lowest whole number. You can try this on your own and see the results! The math operations we can use math for are:

  • + (Addition)
  • - (Subtraction)
  • * (Multiplication)
  • / (Division)
  • % (Modulus, or the remainder’s value in division)
  • ** (Power, so “3 to the power of 4” is also “3**4”)
  • // (Floor Division, as mentioned above)

Some of these seem pretty similar if you have done math before, but modulus may be different from what you have seen. This simply returns the remainder of a division statement, which can be quite useful when determining unknown number values, like if a number is positive or negative.

Moving into a bigger component, I mentioned earlier about Methods. These are little statements which can be called like a variable, but can contain a whole block of code. This can be created by entering def then a name for it, then just enter the opening bracket, as the rest is auto-filled. The compiler knows what is part of this method if it has at least one white space after that line, but as a habit, I will be putting four spaces or, the equivalent, a tab. Adding a few things inside this method definition (the short is def, explains why we type that), and…

Voila! Not much different than what we did before!

This seems useless when being used in the same module, but when we create another module, we can call that method! This starts to become more and more in depth and eventually most actions will be cause by a method, by input from someone or otherwise. We can cover this in another post. For now, we can call everything from the same module. Last few things to learn are Loops and If statements. These are useful to check for certain cases, loop the same lines of code (so we don’t have to keep writing it), and as decisions.

Let’s start with If statements, as they are simply true or false questionnaires! Is 0 > 1? False. Is true true? True! Is the letter “c” the same as the letter “C”? False! That answer may be confusing, but it relates to the ASCII chart mentioned before. To create these statements, we enter if a < b: or something similar. We can also check many cases at once by using an Else If statement written as elif a > b or something similar, and if all else in the statement is false, we use the Else statement, written as else: exactly like that. For example:

Of course we can change the variables and get different results! Try it!

Always be careful of order placement, indent with spaces or tabs, and make sure the statement will check the right data types! The compiler won’t like it when you compare a number to a letter!

Last, but not least, Looping statements. The first of two is the For loop, which is very simple in concept. Loops are written like If statements with a few extra words, for a in range(1, 10):. That looks kind of funny. What this statement is saying is that for every number in the range, starting at 1 and ending at 10, we use “a” to represent the current number. The keys to make a For loop work are “for” at the beginning, “in” to compare, and “:” at the end.

Where is 10, you may ask? For this method called range(#, #) does not include the last number, so if we wanted to include 10 in that list of numbers, we would type range(1,11). This is the case for a lot of programming and ranges of numbers, as the first number is inclusive, while the last number is exclusive.

As well as these For loops, we can do While loops. To write a While loop, we use “while” and “:”, with the space in between for anything you want to compare or check, which is show below.

Well, there is still something missing. I had to stop this program because it was running on an infinite loop. Some programmers will have this issue when they do not want it, and even now I create these problematic loops every once in a while. Sometimes this is something we want, but you will find uses for these cases and how to end them when needed. In this case, we just want to make sure we make loopNumber go up by one every time. We can make this number go up or down, or even multiply or divide, but it must reach the condition we have given it.

A little big, but you get the point.

Again, the 25 does not show up, because this loop sees 25 and compares it: (25 < 25) Is false! Therefore it doe not do the code inside the while loop to produce it! If we wanted to include 25 we would simply add an equals sign (simple math equations).

It would be best to review everything that we just talked about, and try them out with different variables and conditions to fully understand what all this Python programming is.