The DIY Guide for Learning to Code - Part 1: Foundations
Derek Kuhnert Nov 18 Updated on Nov 20, 2018
If you have not already, then please read this article about this series as a whole.
This set of tasks will start from knowing absolutely nothing about programming, and take you to a point where you can use code to solve simple, predefined problems. It's best to think of code as a multiuse pocketknife - It's a tool. You will later learn how to use that tool to do complex things, but first you need to learn what the tool can do, and how to use it at all.
Unit 1 - Tools [Yes, you should read this]
This unit is relatively short, and will familiarize you with IDEs.
Research what an IDE is and what it is used for.
Understand that some IDEs are "heavyweight" and do a lot of fancy things for you, including restructuring your code to look nicer or simpler, and others are "lightweight" and are basically a more convenient text editor.
Acknowledge that you can make your development process faster and more convenient by using shortcuts and macros provided by your IDE.
TEST: Find a lightweight, free IDE you can use for Python (Recommended in order of decreasing recommendation: Visual Studio Code [This is NOT just "Visual Studio"], Sublime Text, Notepad++). Install it to your computer.
Unit 2 - The Basics of Coding [Hello World!]
This unit will guide you in learning to make your first computer program. It does almost nothing, but it's a start.
Understand that a computer will do EXACTLY what you tell it to do, nothing more and nothing less. Computers do NOT interpret what you actually meant if you mess something up.
Acknowledge that you need to learn how to do things that don't have a tangible benefit to you or others, before you can learn to do things that DO.
Understand that a program in almost any language will be able to display text to some sort of text console, and that this is typically the first thing you learn to do in that language.
Install the latest version of Python 3 on your computer.
- NOTE: You will see downloads for "latest versions" of both Python 2 and Python 3. It may not make sense to you that there's a "latest version" of Python 2 when Python 3 exists. Ignore this unless you want to go down a rabbit hole of programming politics.
TEST: Create a Python program that writes the text "Hello World!" to the console, in the IDE you installed in Unit 1. Run this program so you can see the results.
Unit 3 - Data Types [The difference between "3" and 3]
This unit will introduce you to different ways that data can be represented in code.
Research what a variable is in a computer program.
Research what data types are in a computer program.
Research the basic data types in Python.
- NOTE: If you see anything about "classes", ignore it. Classes are a way to make your own data type. Right now, we only care about what Python gives you by default.
Learn what happens if you run the following line of Python code: x = 5
Research how to take user input through the console in Python, and save it as a variable.
Research what the difference is between a variable of type "str" (for "string") and a variable of type "int" (for "integer"), even if the "str" variable contains the value "3".
Research how to convert a variable of type "str" to a variable of type "int", if the "str" variable has a valid int value.
- Bonus: Learn what happens if you try to do this on a "str" variable that does NOT have a valid int value.
Research how to add, subtract, multiply, and divide different numbers in Python, and to store the results in a variable.
TEST: Create a Python program that takes a valid whole number through the console input, and writes to the console the result of adding 5 to that number.
- NOTE: This doesn't have to do anything special if you type anything other than a valid whole number. You can just let it error out if you do that.
- NOTE: This should also work for negative whole numbers - Try it!
Unit 4 - String Manipulation [Twisting words]
This unit will introduce your first real "application" to all of this - Manipulating strings. This may not seem very important, but you will be surprised later at how many useful coding applications rely upon knowing how to manipulate strings.
Acknowledge that, in most programming languages, there is a difference between a "string" data type, and a "character" data type. However, in Python, characters are treated as tiny strings.
Research what it means for a list of things to be "zero-indexed". Acknowledge that, in the programming world, most lists of things are zero-indexed.
Acknowledge that a string can be understood as a list of characters.
Research what the word "substring" means, and how to take a substring of a string in Python.
Find the API page for the "str" data type in Python, and skim through all the different things you can do with that data type.
- NOTE: If you don't understand what a particular part of the page means, ignore it. There will be a lot of information there that's beyond your current skill level.
Acknowledge that programming languages typically have API pages online for showing what you can do with a given data type or other construct in the language.
TEST: Create a Python program that takes a string through the console input, replaces all instances of "ABC" with "XYZ", and writes the result to the console.
Unit 5 - Control Flow [If this, then that]
This unit introduces concepts that make your code react to different situations, so that it's not just executing individual lines in sequence. Your code will now be able to do different things depending on the circumstances.
Research what "functions" are in Python. You should know how to define a function that takes a parameter, and how to call a function with an argument.
Research what "scope" is in regards to variables that you create inside of functions. Learn what "returning" a value does, and why it's important.
- NOTE: This is very important in programming, but is also fairly difficult to learn for the first time. You may need to look at several different tutorials online to get a good grasp on this. Make sure you do before moving on.
Research how to make a statement in Python that evaluates to the value True or False (aka a Boolean expression).
- NOTE: This is rather vague; there are many ways to do this. Make sure that by the end of this, you know what "==", "!=", ">", "<", ">=", and "<=" do.
- NOTE: Make sure you understand the difference between "x = 5" and "x == 5".
Research how to make more complex Boolean expressions by using the "and", "or", and "not" keywords.
Experiment with Boolean expressions by making a simple program that says something to the extent of "print([your boolean expression])".
Research what an "if" statement is in Python, and how "elif" and "else" statements fit into that.
Research what a "while" loop is in Python.
Research what the "break" command does in Python.
Research what the "continue" command does in Python, and how it is different from the "break" command.
Research what a "for" loop is in Python.
- NOTE: This one is a little tricky, but essential. First think of it as "For each thing in the given list, do this with it". THEN, learn how you can use that to simulate a request to "Do this thing x times", where x is a number.
Acknowledge that all these types of loops and other control flow constructs exist in virtually all languages.
TEST: Create a Python program that repeatedly asks for user input until that user input is a valid whole number.
Part 1 Final Test
Make a program that takes user input in the form of a mathematical equation (involving plus/minus/times/divided by/equals signs), and prints out whether that equation is true or false. For example, if I type in "2 + 2 = 4", it should print "True". If I type in "3 * 5 = 10", it should print "False". When it prints the value, it should then take another user input, and repeat until the text "exit" is typed.
UPDATE: Upon looking at this again, I've decided to make things a bit easier by saying that Your program does not have to follow the order of operations. It can behave like a simple calculator, just evaluating from left to right. For example, "3 + 4 * 3 = 21" would print "True", as it would first evaluate 3 + 4 (resulting in 7), then evaluate 7 * 3 (resulting in 21), then compare it to 21.
NOTE: As far as I'm aware (thinking off the top of my head), making it follow the order of operations requires the use of lists, and possibly even nested lists, which is my main reason for not requiring it.
NOTE: All the mathematical operators should behave exactly the same way as they do by default in Python. For example, you don't have to accept "x" as a multiplication sign, only "*".
NOTE: Spaces should be ignored. For example, "3 + 5 = 8" should be treated the same as "3 [insert extra spaces here] + 5 =8"
NOTE: Your program shouldn't do anything special with decimals or fractions. If I type in "0.5 * 2 = 1" or "5/2 * 2 = 5", it can error out or do whatever it wants.
NOTE: Your program shouldn't do anything special with input that is not a valid mathematical equation. If I type in "5 + + 5 = / 2", it can error out or do whatever it wants.
NOTE: The actual logic of deciding whether a mathematical expression is true or false should happen in a function that is called within a "while" loop.
NOTE: If you know what the "eval" function is, don't use it. For the purposes of this test, that's cheating.
NOTE: I would recommend breaking up the workload as such:
- Remove spaces from the equation entirely
- Split the equation into two halves, one on each side of the equals sign
- For each of these halves, start from the first number, the first operator, and the second number (For example, "3 * 5"). Evaluate that, and replace those three items with the result ("21"). Repeat until you only have a number left.
- Compare the two numbers you get from this process.
This will be tougher than anything you've done so far. Once you can do this, you'll have a really good grasp on how to write code itself, and can move on to the next part, where you'll learn how some more advanced programming constructs work, like data structures and classes. This will allow you to write code that models real-world applications (like representing a school course that has some students in it, and those students all have a grade in the course). You will also be learning a new language called Java, as Java lends itself well to learning how to code in this way.
If you are really stuck on this, then that's fine, and you can take a look at a solution I've written up. A few caveats:
Please don't look at this unless you have attempted to write some code. I debated providing the solution here because it's extremely easy to just say "Well I understand it enough, I'll just look and see if my idea was right." Even if you think you have an idea of how it would look, you probably don't, until you try to put it down as code.
There are plenty of simpler ways to do this, but this solution only uses concepts you will have learned by this part. I've also tried to make things explicit where I can, to make sure you know what's going on with my code. There are ways to make the code simpler and still solve the same problem, but they either aren't as clear on what's happening, or involve concepts you haven't learned yet.
There are a LOT of comments here. I haven't formally stated what comments in code are, but essentially, if the computer sees a line that begins with the hash sign (#), then it will ignore that line entirely. And if there's a hash sign in the middle of the line, then it will ignore anything that comes after the hash sign. This is used to show notes on what the code is doing.
WITH ALL OF THAT SAID, if you still want to see the solution, click here.