What I Learned In Coding School Today Is:
So I am going to be starting a new series I will be doing regularly called Coding School. This idea kind of came to me as I was thinking of that episode of Spongebob where he needs to get extra credit to pass. So he has to write about what he learned in Boating School that day. I decided it would be clever to put a spin on the Boating School and make it Coding School! As someone who is studying to become a web developer and consistently striving to learn everyday, I thought it would be a great idea to share my knowledge I learned to possibly help others! Maybe my struggles will help others figure something out they were struggling with as well. Today I am going to be discussing one of the most versatile languages out there, Python. Although it doesn’t really correlate with web development, I feel it is such a great language to know as its capabilities are extensive.
Do note I am in no way, shape, or form an expert on this topic. I am only sharing my knowledge and my ways to solve a problem. What is great about coding is there are so many ways to go about solving a problem. Maybe something else will work for you that I didn’t mention. Feel free to let me know if you have a different solution!
I am somewhat new to the Python language. So as a beginner, I am tackling the basics right now. An easy one I figured I would start things off with is the range() function. Ah, the range() function! There are many uses to this function, but it is mainly used to produce a series of numbers. You then can store these numbers in a list. What you want to do with that list after is up to you though!
I am going to assume you have the understanding of how to set up a python document in your text editor of choice(I am currently using Visual Studio Code). So once you have everything ready, using the function is very simple. You must create a For loop with a value set to whatever you wish to be. The range function can take in three inputs. Right now I am only going to show two values so things do not become confusing too quick. The range() function needs a minimum and then maximum number it can generate between. So if you use range(1, 5) this will generate the numbers 1,2,3,4. This is due to Python(and most languages for that matter) using an off-by-one way of presenting numbers(it is weird but it makes sense.) Let me just go ahead and show you what it should look like:
Now let me explain a little more on why it doesn’t print out 1-5 in the terminal. When using the range() function, Python begins counting on the first value you give it, in this case it is 1. It then stops counting once it reaches its second value you have given it. When it stops, it does not actually count this second value so it is not outputted to the terminal. If we want to get 1-5, a simple fix is to give a range of (1,6) and this will produce the numbers 1,2,3,4,5. With this simple setup you now have an understanding of how to use the Range() function in Python! Hooray! But let’s try to convert our numbers we produce into a list!
Creating a List of Numbers Using Range()
This is a very simple thing to achieve once you have a basic understanding of how to use the Range() function. To accomplish this we simply need to use the list() function and directly insert out range() function inside of it. I will show an example to make it a little easier to understand.
This will output:
And that is all there is to it! As simple as that! But now I am going to show you what the third input value for the Range() function actually does!
Skipping Numbers Using the Range() Function
The third input value for the range() function can actually be used to skip numbers in your range. For an example I will create a list that will produce only even numbers between 2 and 20.
As you can see we added the 2 at the end of our first two values and this will cause Python to add two to every value until it reaches or passes 20. Here is the output:
And that is it! You can also use it to produce odd numbers as well. This is a little bit more complicated I found out though. It requires an if statement and then appending the outputs to a list. Here is how I figured out how to do this:
What this is basically saying is divide the value by 2 and if the remainder is equal to one than it is odd. And if it’s odd than go ahead and append(or aka add it to the end) to the list. So only odd numbers will be added to the list and therefore will be printed. Let me know if you guys have any other ways of solving this, I would love to know!
Well that is all for today’s lesson of Coding School. If you guys have any questions or suggestion let me know. This is something new and I am not sure if I am doing the best at explaining things to please let me know if there is anything I can do to explain things better! Be sure to not be tardy for the next lesson of Coding School!