Photo by Mohammad Rahmani on Unsplash

How to Start Programming: What I Wish I Knew

Part 2 — Programming Basics

Joshua Lim
9 min readMar 19, 2023

--

In my previous article, I went over what programming is. In this article, I am going to go over programming basics, jargon, and more. My hope is by the end of this series you will have enough knowledge on where to start that you are able to go off on your own and make a simple application. For simplicity, I am only going to be using Python and VS Code on macOS for the rest of this series. In other series I write in the near future I plan to branch out to use JavaScript, Swift/SwiftUI, Xcode, and more.

But, before we can get into writing programs, we must first understand syntax and programming language rules first.

What is Syntax and Why Does it Matter?

Syntax is essentially rules for a language. Think of the English language, and the saying “I before E except after C” (maybe a bad example since there are times this is ignored but, you get the point). Programming languages have syntax too, and these rules essentially help create a universal agreement to how we design our code and list our commands for the computer to understand. For example, if we want to print a line to the console, in Python we would run:

# Prints 'Hello, World!' to the console
print('Hello, World!')

Which outputs the following:

Hello, World!

If we wrote any of the following:

# Bad Example #1
print(Hello, World!)

# Bad Example #2
('Hello, World!')print

# Bad Example #3
printToConsole('Hello, World!')

It would result in a syntax error, and wouldn’t even compile the code. It is also important to understand the process of converting code to executable programs, which is known as compiling. Understanding this process can furthermore help us understand any errors we may encounter and fix them. This process is called debugging. In order for code to compile, all syntax must be correct. Text editors such as VS Code will help and alert you before compiling if the code is incorrect. Then, once compiled, we must run that program to get the output we desire. If the program fails, this is known as crashing, or in other words, a runtime error. This is seen more so when developing between systems, such as a server and client (we will discuss this in a later part), or dealing with user input.

Now you understand what is syntax, let’s jump into some basic Python syntax.

VS Code & Basic Python Syntax

First you should familiarize yourself with some basic Python syntax. As we saw above, to print to the console, you run print(), however, you may be wondering, “What is the console?”

Introducing, VS Code. I am going to assume that you are capable of downloading VS Code on your own system, however, if that is something you may struggle with here is a link to another Medium article by Aniket Jadhav describing in detail on how to download and understand the VS Code environment.

After you have VS Code set up, create a folder for your projects. I named mine “How to Start Programming”, after this series, but I recommend something much shorter and simpler such as “myProjects” or “projects”. Once you create that folder, you can either drag it to the VS Code application or ‘cmd + O’ on macOS in the VS Code application to select the folder. After you do that, your text editor should look something like this:

I decided to add another subfolder called “Part 2” since I plan on hosting this on my GitHub for anyone to follow along or reference accordingly. If you look to the left, you will notice the “EXPLORER” panel, which is all of your files that you are editing. At the bottom, you will see the console, also known as the terminal. This is the blank interface of your computer without all the fancy app icons and animations provided by your operating system. Finally, the big blank space in between is your editor, which is where the magic happens.

VS Code Explorer Panel & Editor
VS Code Terminal

Next, we need to create a new Python file. You can either use ‘cmd + N’ on macOS or tap the add file button in the explorer panel.

Create New File VS Code

I named my file “syntax.py”. Once you save the file and open it you should now see it on your left panel, and your editor should showcase a blank file like such:

Blank syntax.py File

You may be prompted to download Python3 for your device if you haven’t already, as well as the VS Code intellisense for Python. Here are linked articles on how to do each, as well as the official Python website:

While this may seem like a daunting and confusing task, I can assure you that it only gets easier from here. Downloading languages and package dependencies are crucial for programming, as your computer likely doesn’t come pre-equipped with every programming language there is. Once done right, it is never needed to be installed again, which is the bright side.

Hoping installing python3 was a smooth experience, we can now begin basic coding!

Let’s try our command from earlier, print(), and see what happens. On lines 1 and 2, write the following code:

# Prints 'Hello, World!' to the console
print('Hello, World!')

(NOTE: Lines are important in programming, especially Python. As you see in the code above, there are 2 lines of code. If we press ‘return’, it creates 3 lines. Also, comments are anything preceding the ‘#’ character. Typically highlighted green, comments are ignored and not ran by the computer. This is helpful in long files of code and collaborating with other developers, as it describes what code does to others when it gets very confusing.)

After you write those 2 lines, save the file by pressing ‘cmd + S’ on macOS, and then write the following in terminal:

$ python3 syntax.py
syntax.py Hello, World!

It should look like the image above. Note how the dollar sign wasn’t included. When giving examples for commands to execute in terminal, you will see a dollar sign. That is an indicator to the beginning of a line, however should not be included in the command itself. After ensuring that you are in the same directory as your file (change ‘syntax.py’ to ‘fileName.py’ if you changed the file name to something different) by using ‘ls’ and ‘cd’ commands, press enter and you should get the following output:

Congrats! You Ran your First Program!

Yep! It is as easy as that (if you found it easy). Now onto more complex ideas. Let’s create a variable, define it, change it, and watch its output as we manipulate it. While this may seem like a lot, it really isn’t. But first, as always, let’s take a look at some of the things we are getting into.

Variables are a type of object that holds a value. Variables are given a name, and in Python, initialized with a type of value. Here is a list of types of variables commonly seen in Python, as well as what a function is:

  • Int or Integer — This is a whole number.
# Example of an Int in Python
age = 19
  • Float — A number with up to 7 decimal places of precision. 32 bit.
# Example of a Float in Python
height = 6.2562890
  • Double — A number with up to 15 decimal places of precision. 64 bit.
# Example of a Double in Python
height = 6.256289043167382
  • String — This is a list of characters. Simply put, it can be letters, words, sentences, etc.
# Example of a String in Python
name = 'Joshua Lim’
  • Bool or Boolean —This is a True or False statement.
# Example of a Boolean in Python
is_male = true
  • List — This is a collection of data. Known as ‘array’ in some other languages.
# Example of an Integer List in Python
integer_array = [1, 2, 3, 4, 5]

# Example of a Float List in Python
float_array = [1.1, 2.2, 3.3, 4.4, 5.5]

# Example of a Double List in Python
double_array = [1.11, 2.22, 3.33, 4.44, 5.55]

# Example of a String List in Python
string_array = ["Hello", "World", "Python", "Programming", "Language"]

# Example of a Boolean List in Python
boolean_array = [True, False, True, False, True]
  • Function — This is a group of code to be executed more than once.
# Example of a function that takes two integers and returns the sum of them
def add_two_ints(a, b):
c = a + b
return c

# Example of calling the function
x = 5
y = 6
z = add_two_ints(x, y)
print(z) # Prints 11

Now you are familiar with some of the primitive types in Python, let’s write our first program that takes user input!

Writing a Program that takes User Input

I started off by creating a new file called “dog_years.py”. This program will take a users age, multiply it by 7, and return back their age in dog years.

(NOTE: You may notice I have been using underscores instead of spaces in names, which is a technique known as snake casing. Another widely known technique is camel casing, which is capitalizing the first letter of every word except the first word. Python uses snake casing, so that is what we will go with moving forward.)

To start, we need to gather the users age, convert it to an integer, and store it in a variable. We can do this by using the input function provided by Python, like so:

# Take the users age
age = input('Enter your age: ')

# Convert the users age to an integer
age = int(age)

As you see on line 2, we prompt the user to input their age. For the simplicity of this tutorial we will assume the user enters an integer such as ‘19’. If a user would enter something like ‘nineteen’ or anything else, the program would crash. We will discuss why this happens in a later article. For this reasoning, we also need to make sure we convert the input to an integer, hence line 5.

# Calculate the age in dog years
dog_age = age * 7

Next, we create a new variable ‘dog_age’ and set it equal to 7 times the amount of ‘age’. In programming, the asterisk is used for multiplying two values, and the slash (‘/’) is used to divide two values. Finally, with our new value, we can print it back to the user and showcase their age in dog years!

# Print the age in dog years
print('Your age in dog years is ' + str(dog_age))

As you may have noticed, we converted dog_age (an integer) back into a string (using the str() function in Python). This is so we can print it to the screen alongside our message. Again, this will be covered in a later article, but if you are interested, feel free to read up on Python type casting more here. Run the following command:

$ python3 dog_years.py

And voila! Enter your age and watch the magic happen! (Should get an output like the one below…)

Wow! I am 133 years old in Dog Years!

What’s Next?

Wow, in only two parts you’ve went from knowing nothing about programming to calculating a users age in dog years! Here is a few things you can challenge yourself to do if you aren’t ready to move onto the next article just yet:

  • Create a function that calculates the dogs age and call that
  • Ask for the users name and instead of saying ‘your’ say the users name in print statements
  • Look for similar projects online!

I will begin to upload all code to my GitHub and link it at the end of each article if you find yourself stuck at all during this process. If you have any questions or issues with this walkthrough, feel free to email me at joshualim@creatingreal.com or join my Discord server which will be linked below. I hope you enjoyed this second part in the series, and hope the next one helps just as much!

--

--