Basics of Coding

Wanted to get a simple guide to coding out with my Fun Coding Projects article. This is a very basic crash course to python, which is a good beginner language in my opinion. We won't cover anything very complex, but you should be able to make simple interactive terminal programs when this is done.

Setting Up your Environment

So before you start coding, you're going to need something to write the code with, and something to run the code with. These will be a text editor and your interpreter.

Hopefully if you're running Windows you have notepad installed on your computer, that will be the assumed editor for this tutorial. Now for the interpreter, you can download it here. Once you have that installed, you can press the window key and type powershell. This will be your terminal.

If you're running MacOS then idk and I'm not figuring it out right now. You can contact me if you want some help, and I might add it to the guide later, but not now. If you're running Linux ask whoever installed it for you to help you.

Ok, so you have a terminal. I want you to copy the following text and paste it into your text editor.

			
			print("Hello World!")
			
			
Then save it to C:\Users\\hello.py, replacing the YourUsername with whatever your username on windows is. Now in that terminal type python3 C:\Users\\hello.py and press enter.

If all went well, python just said hello! If all did not go well, either you or I messed up. Have a try at figuring out what's wrong, and if you can't look for help (I can probably help if you contact me).

Now explaining what just happened, you just wrote a simple python program that tells the computer to print hello to the terminal. You then ran that program through the python interpreter, by pointing the interpreter (invoked with python3) at the file you saved. The interpreter is a program that runs programs. Basically your CPU (the part of the computer that does the thinking) doesn't speak python, it speaks machine code. The interpreter however can translate from python to machine code, and allow your program to run.

The Very Basics

So, let's start by understanding that program we wrote.

			
			print("Hello World!")
			
			
Basically, print is a command to print whatever out to the terminal. You make clear you're calling print by immediately following it with parenthesis (no space in between). Whatever is in the parenthesis are the inputs to the command, in this case we gave it some text (called a string) containing the hello world. Strings must be surrounded by quotation marks for the interpreter to know it's supposed to be a string.

We can also pass print other kinds of things, like a number

			
			print(1)
			
			
In fact print can handle anything as an input! Most commands can't do this, and require specific types of input.

Now lets try something else. Replace the contents of the file with this and save it.

			
			print("first thing")
			print("second thing")
			
			
This prints the first thing, then the second. The commands you give are executed in the order you give them. Pretty straight forward, but it is important to understand.

Now try copying this

			
			print("thing)
			
			
Running that gives us an error. Why? Because we didn't close the parenthesis!

Computers are terribly literal things, and can't understand your program unless you type it exactly right. One minor typo and it'll just stop working once it reaches it.

Another aspect of them being literal is that they do what you told them to do, NOT what you want them to do. This makes it easy to make a program that doesn't do what you want if you get one small detail wrong.

Variables

Now lets introduce a new command. The input command.

			
			input("This is a prompt:")
			
			
When you run this program, you'll see that it prints its input. We already had a command to do that, why another one? Well this one also accepts input from the terminal. You'll notice your program hasn't finished yet. If you type into the terminal and press enter, it'll be captured and returned by the input command.

Returned? Let me explain with an example.

			
			print(input("This is a prompt:"))
			
			
This will take your terminal input, and print it back out to the terminal. How? Well print accepts an input, and the code here says we're going to use the output (or return value) of input as that input. We can actually do this as many times we want.
			
			print(input(input(input("echo"))))
			
			
And anything that returns a value can be fed to print and input like this.

What else can we do with these return values? Save them.

			
			a = input("Value to Save:")
			print(value)
			
			
Here we take the value input gave us, put it into a variable name a, and then print it. We can put anything we want in a variable actually. Even a command!
			
			a = 1 + 1
			b = "stuff"
			c = print
			c(a)
			
			
Here we also see we can do basic math using +-*/.

Conditional Execution

What if we want to run or not run something based on if we received the word kitty as input?

			
			a = input("Type something:")
			if a == "kitty":
				print("meow")
			
			
This is called an if statement. It only does the print("meow") if the input was "kitty". You use it like this
			
			if :
				
			
			
The colon is vital, as is the indent. The indent tells our interpreter that all the commands at that indent level belong to that if statement. We can repeat this structure as well.
			
			if :
				if :
					
			
			
Condition can be anything that can be true or false, such as checking if two things are equal (a == b), comparisons (a >= b, a > b, and likewise), and not equals (a != b). There are many more possible conditions, but this is supposed to be basic.

You can also do an if-else statement.

			
			if :
				
			elif :
				
			else:
				
			
			
The elif has its condition checked when the if has its condition fail, and the else is executed when all conditions fail.

Loops

I'm going to show you a while loop. There's also a for loop, but I'm not explaining it in this guide. Googling things is an important skill for coding, so if you're curious give that a try.

			
			while :
				
			
			
It checks the condition and runs the commands if that was true. So far just like the if statement. But when it's done with the commands, it tests the condition again and if its true runs the commands again. It keeps doing it until the condition is false. This does allow you to make an infinite loop. If you want to stop a program that is currently running, press CTRL+C.

You can run a set of commands 3 times by doing the following.

			
			x = 0
			while x < 3:
				
				x = x + 1
			
			
That last bit might be confusing, as it looks like we're comparing x to x+1. But no, we're taking the value of x, adding one, then overwriting the previous value of x with that.

You can also use the for loop to run a certain number of times, but I don't feel like explaining that.

Functions

What if you want to make your own command? Presumably by putting together lots of the things we've seen so far. Well the secret is that the things I've been calling commands are actually called functions, and you can make your own!

			
			def (, , ...):
				
			
			
You just slap this in your code somewhere before you want to use it, and boom, you've got a custom function. You can also pass multiple arguments (what the inputs are called) to a function, by separating them with commas. So print(1,2) prints both one and two, and print("one","two") does something similar.

Libraries

Lastly, what if you want to do things other than read and write from the terminal? Well, you can import things from a library.

			
			import random

			print(random.randint(1,2))
			
			
This randomly prints 1 or 2. There are lots of these libraries, and I'm not listing them here, but you can look them up, and even install new ones.

Wrapping Up

Well, that's my quick and dirty explanation. Hopefully you can do some fun things with that. I have suggestions here. You could also try the w3 tutorial. Or you could try other tutorials. Idk, try to have fun.