Discovering Your Part In Programming


Part 1 of a multi-part series course. Visit the Connect Learning Academy to take the entire course.


You find yourself in a living, breathing world evolving to accept computers to manage our everyday. But what rests behind your screen, behind what you see — what are you truly interacting with? Some may say ones and zeroes, some will give you too much of a description to be interesting. Some, will tell you the truth.

With the history and knowledge of the world sitting just at our fingertips, you can always find yourself with something new to look at and learn. Some schools provide programs teaching students the basics of programming, generally in the Visual .NET series of languages. Namely, Visual Basic. It’s a language that sets itself out as one of the best for learning how to solve problems thinking for the machine, but still apply some small steps and basic terminology along the way. Another popular language, and my current favourite, is Python.

Python is a language first developed in 1991, the most popular implementation being written in C. Nicknamed ‘CPython’ for some. It was influenced by a long list of languages but seems to follow a fairly consistent, unique syntax. Each major release of Python has come with many additions to the language, moving it forwards to be used with each evolving year.

But that’s enough history.

Learning how to program is a struggle. Constantly moving between Google and your text editor, finding the solution to your problems can be hard. That’s the struggle that you need to push through, because the result always feels satisfying. Programming isn’t for everyone, but it might be for you.

Baby steps, now. We can’t all write software for businesses quite yet.


1. Finding a text editor.

Getting a text editor you can feel comfortable with might be a little bit of a challenge — especially with so many of them out there. So how can you find the one that’s right for you?

a) Sublime Text

Courtesy of sublimetext.com

The editor I use almost daily for all of my projects, Sublime Text never fails to provide with its beautiful interface and beautiful syntax highlighting. My favourite feature? Snippets.

But what are snippets, and why are they important?’ you might ask. Well, imagine you’re typing away at the screen. Writing code and you just despise having to write the same thing over and over.

if myFunction == myValue:
elif myFunction >= myValue:
elif myFunction != myValue:

It gets a bit tiresome. Snippets finish it for you. If you start writing something, a little window appears with a short list of everything you can use to finish that little piece without having to type it out yourself.

b) VIM

Courtesy of arstechnica.net

A popular choice among the *nix community, VIM and its counterpart, Vi, are major text editors used by many programmers around the world. Professionals, students, beginners, government contractors—you name it.

VIM takes its name from the original that it is based off of, Vi, titling itself as “VIM — Vi IMproved

VIM requires a lot of attention if you want things to look nice, on the other hand. Customization, nicknamed ‘ricing’, is popular to a slightly smaller community. This is because it takes a lot of time and finding the specifics of what you want. I’ve used VIM before, and it’s the million dollar editor, but I don’t recommend trying to use it right away.

c) Emacs

Courtesy of gnu.org

Another popular text editor among the *nix community, Emacs is a free software text editor. This means that the source code is available to the public and editing it is completely permissible. Emacs was initially written in 1976 by Richard Stallman and Guy Steele Jr.

Like Vim, it’s a text editor with incredible capabilities — but like Vim, it takes on the negative side. Too much time to customize.


Okay, that’s enough. When do we get to code?


We’re getting there. It takes a few steps to set yourself up for really jumping into your code. Once you have your text editor set up, it’s really time to get started. We’ll be working in Python. But wait! We haven’t even downloaded Python yet.

Yes, that’s right. You have to download Python. It doesn’t come with every platform. But it should only take a few minutes. Here, I’ve done all the looking around for you. Click the link below.

Click ‘Download 3.x’. Not literally 3.x, x represents the numbers in front of the 3.


Okay. I did what you asked. Can we get started?


Absolutely.

2. Your First Program

We’re here—finally here. You get to make your first Python program.

#!/usr/bin/env python3
print("Hello, world!")

And there you go. Your first Python program. But don’t leave it there — save it! Let’s run that program and see what it does. On your text editor, save the file with whatever fancy name you want. Need a suggestion? How about “hello.py”

When you open the file, it’ll come up with a little window that says

Hello, world!

Lucky for you, that’s not a problem. That’s all it’s supposed to say. Let’s break that program down and see what everything does.

#!/usr/bin/env python3

This is mostly important if you have multiple versions of Python installed and are running it as an executable application. It’ll identify the proper version and use that to run the code.

print(“Hello, world!”)

print() is a function that simply puts whatever is inside of the brackets onto the screen. You needed to wrap the Hello, world! with quotes “” so that it could be identified as a string.

A string is anything that is parsed as plain text. If you’re writing something in Notepad and save it as a text file, what is inside could be considered as a string.

Now, you’ve made your first step. But is that really all? No. And your journey will be far more exciting than that.

Before I let you go off on your own, let’s write one more program. This time, we’re going to get the user to say how many times they want to flip a coin—and then the command line will print what the result of each flip is.

#!/usr/bin/env python3
import random
from time import sleep
coinSides = [‘HEADS’, ‘TAILS’]
def coinFlip(userNumber):
for i in range(userNumber):
print (random.choice(coinSides))
while True:
userNumber = raw_input(“How many times do you want to flip the coin?: “)
try:
int(userNumber)
except ValueError:
print (“\nIncorrect input. Try again. Remember to put a whole number (ex: 1, 3, 5)”)
continue
else:
userNumber = int(userNumber)
break
coinFlip(userNumber)
sleep(10)

Woah, what’s all this new stuff?


Don’t worry. Let’s review what’s up above, here.

As we remember from before, #!/usr/bin/env python3 is so that it recognizes the file written in CPython major release 3. But next, we have something that looks a little new—don’t we?

import random
from time import sleep

These lines read modules (You’ll hear about those later) and let you use functions from them that otherwise, you wouldn’t have access to. With the random module, we can use its function choice(). This lets us read from the given value and it chooses ‘randomly’ between them. This isn’t true random, though. True random doesn’t exist in code, and what we have here is tagged as ‘pseudorandom’. There’s an algorithm to the decisions made that makes it impossible to be a true random, but that’s beside the point.

def coinFlip(userNumber):
for i in range(userNumber):
print (random.choice(coinSides))

This defines the function coinFlip(). Later in the code, we run this function, but here it serves only the purpose of making something that can be later used. Functions are something that can be applied to multiple different parts of code, instead of having to rewrite something over and over—you can just define a function and use that.

for i in range(userNumber)

This is a for loop. For loops are loops that run depending on a certain pattern or value. It’s used when you want to execute a certain statement for a certain number of times.

coinSides = [ ‘HEADS’, ‘TAILS’ ]

This is a list. Specifically, this list is an array. You can use these if you want to store different values within something without giving them a definition. If you wanted to give them a definition, you’d put them in a dictionary. For what we want to do though, our best use is an array.

Although, here’s what it might look like as a dictionary.

coinSides = { 1: 'HEADS', 2: 'TAILS' }

This assigns the numbers 1 and 2 to the strings HEADS and TAILS within the dictionary coinSides.

Anyway, moving ahead.

print (random.choice(coinSides))

With the 50–50 chance of getting either heads or tails, it prints the result from flipping the coin using the function choice() from module random.

while True:
userNumber = raw_input("How many times do you want to flip the coin?: ")

while True: is a while loop! So, like the for loop from earlier, it executes a statement until it finds an end. Although, as a while loop, it runs infinitely until it’s given a bold reason to ‘break’ out, or cancel the loop. Our term break, later on in the loop, is what does this.

try:
int(userNumber)
except ValueError:
print (“\nIncorrect input. Try again. Remember to put a whole number (ex: 1, 3, 5)”)
continue
else:
userNumber = int(userNumber)
break
coinFlip(userNumber)
sleep(10)

This is the last stretch of that code, the big part that really puts everything up to this point to the test.

try:

This is a statement that attempts to do something, checking what the given result would be. In our case, it wants to check if the input from our user is an integer. If the computer raises an error, known here as ‘ValueError’, it’ll pass over and tell the user that they haven’t given a proper input — asking for a number and continuing to the beginning of the loop once again.

else:
userNumber = int(userNumber)
break

If there is no error and the user has given an integer value, this makes sure to convert the value into an integer and then exits the loop. Once it exits the loop, it moves forwards and executes the next part.

coinFlip(userNumber)
sleep(10)

This executes the function that we made earlier, coinFlip(), using the value we got from the while loop—’userNumber’. After that, it runs sleep(10) to hold the window open for 10 seconds before exiting or killing the program.


That’s nice. So, can I start writing code now?


Not exactly. Here, you’ve seen a few key bits that you may need in the future but you’re not ready to start programming yet. But I’ll help you with the push to move forwards with the knowledge and newfound superpowers you’ve gained today.

3. Coming Out On Top

No one gets out this easily, and that’s why you need to make the real push forwards for success. You might’ve learned that programming isn’t for you, or you might’ve learned that it’s something you find interesting. It’s not always going to be good fun, but it won’t always be boring.

From here, your real lessons begin. You might find yourself going to school, or teaching yourself with the resources available everywhere. If you want to teach yourself, you need somewhere to start.

a) Codecademy

Courtesy of sitebuilderreport.com

Codecademy is a resource that helps you get started with programming and finding the drive to develop your own ideas when you really want to start working on a project. I used it years ago for HTML and CSS, and although it can seem like a bit of a drag, even copying the code by writing it yourself helps you memorize the movements and parts that could help you down the road.

It’s not the best, but nothing can be the best. Just don’t fool yourself into thinking that you can program professionally after any of the courses. They’re a jumpstart to getting the basic ideas of how to build a program. Or a website, depending on what course you take through it.

b) Khan Academy

Courtesy of businessinsider.com

Khan Academy holds a much wider variety of courses for people, including parts in biology, physics, math, and what matters here — computer programming. It gives you more space for practical thinking within what you’re learning, and guides you along a good path for you to decide on what you’ll start doing with your own time.

c) Python Documentation

Courtesy of blog.python.org

This is the true library that’s going to help you through doing anything. It describes everything about Python. Even while you’re working on a course, you’ll be looking here to find out how to do something or what you can do with something. It’s your one-stop-shop for everything Python and you’ll almost never not need it. The code examples can give you an even better showcase of how to write your program than the descriptions of each function themselves.


There you are. It’s your turn to teach yourself, and to really step out into the world of programming. It’s no easy life, but it can feel that way when you try. Never give up.

Code on.
Disclaimer:
The article (collectively; ‘This’, ‘the article’, ‘this part’, ‘part one’) is not meant to represent the results of professional programming or working in a professional environment. It is meant to provide the basis of learning to the reader. We are not responsible for anything you do or produce after reading.
One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.