Programming Tutorial

This page is intended for anyone who does or doesn't want to learn programming but needs to use it and doesn't know how it works. I understand that programming is very difficult to learn at first, due to its technical nature, and the fact that it's a new concept to the average computer user, so this page is designed for the people that need a kickstart to programming but can't handle the formal method of learning it.

We have other suggestions for python tutorials on the Programming page but if the other tutorials don't work for you THIS PAGE may the the tutorial you need as it uses a different approach.

Introduction to Python Programs

Hello World

print "hello world"

Who Goes There?

print "Halt!"

user_reply = raw_input("Who goes there? ")

print "You may pass,", user_reply

Count to 10

a = 0

while a < 10:

a = a + 1

print a

Password (a # sign in Python is a comment, it tells extra information (in regular English) without affecting how the program runs)

password = "no password"

# note that != means not equal

while password != "typepasswordhere":

password = raw_input("Password: ")

print "Welcome in"

Sum Program

a = 1

s = 0

print 'Enter Numbers to add to the sum (positive or negative)'

print 'Enter 0 to quit.'

while a != 0:

print 'Current Sum:', s

a = input('Number? ')

s = s + a

print 'Total Sum =', s

High / Low Guessing Program

number = 10

guess = 0

while guess != number:

guess = input("Guess a number: ")

if guess > number:

print "Too high"

elif guess < number:

print "Too low"

print "Just right"

Even / Odd Program

number = input("Tell me a number: ")

if number % 2 == 0:

print number, "is even."

elif number % 2 == 1:

print number, "is odd."

else:

print number, "is a decimal."

Average Numbers Program

sum = 0.0

print "This program will take several numbers then average them"

count = input("How many numbers would you like to average: ")

current_count = 0

while current_count < count:

current_count = current_count + 1

print "Number", current_count

number = input("Enter a number: ")

sum = sum + number

print "The average was:", sum / count

Temperature Converting Program

def print_options():

print "Options:"

print " 'c' convert from celsius"

print " 'f' convert from fahrenheit"

print " 'q' quit the program"

def celsius_to_fahrenheit(c_temp):

return 9.0 / 5.0 * c_temp + 32

def fahrenheit_to_celsius(f_temp):

return (f_temp - 32.0) * 5.0 / 9.0

choice = "p"

if choice == "p":

print_options()

while choice != "q":

if choice == "c":

temp = input("Celsius temperature: ")

print "Fahrenheit:", celsius_to_fahrenheit(temp)

elif choice == "f":

temp = input("Fahrenheit temperature: ")

print "Celsius:", fahrenheit_to_celsius(temp)

choice = raw_input("option: ")

An Example of Programming

Many people don't exactly know what programming can do for them. So here's a summed up answer: it manipulates data through a series of easy logic. Here's the example:

If there are exactly 20 people in a room, for each member in the room, announce to each member to "Take a seat.".

This may not seem like programming at first, since it's plain English! And that's partially correct: this isn't formal programming. If I threw this statement on a computer, I'm sure I'd get tons of errors. However, if I told a human to do this, would that human understand it? Yes, of course. This is what's known as pseudocode, and it's probably the most effective way of learning how to program in any language.

So then how is this pseudocode related to regular code? Well, let's find out. Let's take each part of my statement apart.

    • "If there are exactly 20 people in a room..."
      • This part of the code is a conditional, since it has the word 'if'. 'If' statements act like doors, since the condition has to be met for the rest of the code to be evaluated.
      • The room is something that holds people, specifically 0 or more people. Note that the room is a thing itself (which is important).
      • Since a room is something that holds people, the units that a room can be measured in is people. For instance, this room can hold at least 20 people.
      • The condition triggers the rest of the statement when 20 people are in the room. If there aren't exactly 20 people (say there are 19 or 21 people), the condition is not met, and the announcement will not be announced.
      • So obviously, something has to count how many people are in the room at all times. The amount of people in the room varies, so we'll call the counter of people a variable.
    • "...for each member in the room..."
      • This is called a loop, because we repeat the following statement every time for every member in the room.
      • Remember how I said a room is a thing? It's what we refer to as a list or an array. This is data that has more data in it. The data in the room is people, specifically 20 of them.
      • So each time we meet a member in the room, we do the following command (announcing)
    • "...announce to each member in the room, "Take a seat."."
      • 'Announce' is a verb, and we're using it as a command. So we are then commanding the announcer to announce.
      • 'in the room' is a sort of limit. We don't want to tell anyone outside of the room to take a seat, that's just silly. So we make sure it stays in the room.
      • 'to all the members' is the same; it's a limit. We don't want to tell all but to one person. It also directs the attention to all the members, not a wall.
      • "Take a seat." is data. What are we doing with/to this data? We're announcing it. In that way of thinking , we find that this is data manipulation. The above is also data manipulation, since we have a variable (of how many people there are in the room) that changes (from some number to 20), and we're using that data to control what the announcer does.

That's as low-level thinking as I can get. Basically, I've just explained what we process so quickly in our brains so that we can turn it into computer compliant code. So if I wanted to write this in computer compliant code, it might look like:

if len(the_room) == 20:

for each_member in the_room:

each_member += " has been told to take a seat"

The_room is also a variable since the room has members inside it, so its length varies and members vary. Each_member seems to refer to a name of the person in the room. So each_member is a new variable that varies every time the ensuing command finishes.

( The above example was written in Python. )

Let's get a little more how everything works.

Data Types

The Integer and the Float

The integer, as we should all know from math class, is any positve/neutral/negative whole real number. 1 is an integer, 55 is an integer, 0 is an integer, -10 is an integer.

The float is any number that's real. 1.9999 is a float, 0.000 is a float (as long as there's a dot), -2.75 is a float.

If I said x = 10, what is x in terms of integers or floats? It's an integer.

Further Information

Other Tutorials

    • pythontutor.com: An interactive python programing tutorial
    • CodeAcademy.com: A website to interactively learn programing in Javascript, Python and other languages.