Yesterday, I wrote a small introduction about my background and different computer experiences I have had. I promised a basic tutorial on python. Honestly, I am learning python still. I am taking an entry level programming class at school, but I feel the need to share my knowledge with other people.
So hopefully, I can continue writing tutorials.
So, here it goes — deep breath
With any programming — YOU will make mistakes and hopefully you will be able to overcome them. Have you ever tried learning a new language? If so, it is like learning how to program.
Programming is not hard, does it require thinking? Yes! Many people would say programming is hard. But really, once you know how a program language works you can understand what you can do and what you can’t do with it.
I am laughing now because few years ago, I wanted to jump into programming, and just skip the boring stuff, such what I am writing. But, now I understand why we must understand a few pointers before we get our hands dirty.
Why the boring stuff?
Programming is like learning how to drive a car. People who have not learned to drive, don’t just get into the driver side and start driving without the basic rules of the road. The same goes for programming, Programming has basic rules to follow, does each programmer do exactly the same thing while coding? No, each person will find a different way of doing things but the result will be the same. I think you get the idea – Hopefully.
What are the rules?
That’s the fun part — each programming language will have their own set of rules. We call these rules, syntax. How will I know if I have broken a rule? For this case, python is an interpreted language, and it means that the code you write is on-command – no need to compile. And once you run your code through the interpreter, it will tell you whether or not the syntax is ok.
Let’s get started
Your expecting me to start a program and tell you what type into the shell (command line) to create your first program. But, we are! When you think of a program in mind you want to create you have already started the process of building a program. But, how will the program work. How complex will it be? Pretty soon, all that thinking can get a little fuzzy. So, traditionally, programmers will use a IPO chart and pseudo-code to help build a program that will work.
What is that IPO thingy?
A IPO chart is short for three words: Input, Process, and Output.
A IPO chart is not hard to create. You can create one with Word or hand write one. A basic format is three column with the labels: I P O.
The purpose for a IPO chart is to identify what going on? What are we putting in our program? What will the computer do with our input? What will the results be after the process?
What do we do after our IPO?
That’s were the magic begins — pseudo-code! This is where we start “seeing” our program come together. So, what is pseudo-code? pseudo = fake, means that the code we will write is not a real programming language. We can use basic instructions to write fake-code. and before we create our fake-code and before creating an IPO chart, we need actually define what our program will do.
Lets see, we can create a program that asks you name and age, then adds ten year to your age to display how old you will be in ten years.
Fake-code
We can say the same thing in many ways. This program will use variables and user input. The most common thing is to declare a variable (let it be known to the computer what variable you will be using). Set the variables to a value or calculation. Then Display the results.
like this:
1 Declare name = 0
2 Declare num1 = 0
3 Declare addTwoNum = 0
4
5 Set name = userinput("What is your name")
6 Set num1 = userinput("What is your age")
7 Set addTwoNum = num1 + 10
8
9 Display "Hello" + name + "your age in ten years will be" + addTwo
So now we can create our python program
Remember I was talking about rules and stuff? Well, there formality also
Did you notice in the fake-code we shown our variables with camel case? It helps to see what is a variable and what not. Another thing we can talk about is comments, Comments will help tell other coder what something does. or like me, I’m getting forgetful after a few weeks, I need a refresher if what I did. In Python – you can use the # sign to create a line comment.
So all we need to do now is to convert our fake-code to python.
Before I display the steps, I need to say that I am running Python 2.7, the new one is 3.0 but our class is using 2.7, so I have not tested this code on the new update
You can download Python here or use this code as your own risk.
Step 1: Open Python
By default, Python opens on a command line, but we want an editor to create our program
Step 2: Go to file > New Window
When working with anything you should save it before you begin (periodically, saving). Exention should end with .py
Step 3: Go to file > Save as > [AnyName].py > Save
In the new window, looking at your fake-code, figure out how something is done by using the proper syntax. I have created the code below:
#This is a comment the program will ignore what you type here starting a (#) line
#
# Comments will help create notes about the program for programmers(non-users)
#
# initialize varibles
name = 0
num1 = 0
addTwoNum = 0
#
#
# Set Varibles to values
name = raw_input("What is your name?")
num1 = input ("What is your age?")
#
#
# Set Calulation for two number add
addTwoNum = num1 + 10
#
#
# Get ready to display calculations
print "Hello", name, "your age in ten years will be", addTwoNum
Once you have create your program, you need to save it [CTRL + S] works, then
Step 4: press F5 or goto Run > Run Module
Hopefully, You will get a prompt asking your name
[ENTER]
Another prompt, will ask your age.
[ENTER]
then the program with display your name and tell you how old you will be in ten years
If you have made it this far, Congrats! This ends the tutorial. Thanks for trying it out, if you get errors, let me know!
Thanks,
Brian
