Category Archives: Cmst103 – Program Logic & Design

Beginning Programming with Brian: #Python Part 1 #tutorials

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.

Our example IPO chart:

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

 

Introduction: Begining Programming with Brian #python #tutorial

I will start writing tidbits of info I am learning in my intro programming class. So, heres a little introduction of my background and how I got into programming. The next post will cover how to open python to create our first program.

For my digital media tech degree I am required to take a programming class. Honestly, 5 years ago I was secretly scared of programming. I mean… Yes and No. 7 years ago or so. I was learning HTML, then onto other things. HTML was unique for me. I learned it the home brew way, I mean — I taught myself how certain tags display certain things. It was only basic <tags> that I knew, then I wanted a full website, at the time — I didn’t understand the whole thing about hosting or a web server, but I knew that I can download HTML templates and upload them to Geocities (now, a dead service from Yahoo) Then, edit the text between tags, and wahla! You had a website! and did I mention —- Free! Later on, I learned many thing about HTML the proper way by books and an HTML class in college. and after the boom on CMS systems, I wanted to learn that too! so I taught myself the basics in installing Joomla or WordPress on my web hosting. So, over the course of time, I was intrigued by many technologies, and wanted to know how things work. I can hear the voice of one of my good friends Sue, “I need to figure out how this thing ticks.” So all in all, I have a knack of learning things really quick.

When I left college (first time) to pursue other things, I learned other things like Database design , Networking, and FileMaker Pro scripting, I picked it quickly, and the introduction of MySQL.

So, how does learning Python relate to what I learned in the past? So far, I can say Python is a interpreted language, and what I can see that what you put into it (as in command line GUI) it will give you a response. In programming, you have rules (syntax), and if you break the rules you’ll get a syntax error. Same idea for logic errors, assuming your syntax is correct, the process of creating a calculation, and if you calculate something than you desired, you will get an  undesired response. For an example: we can set a variable to calculate 12 * 12, and of course, the answer is 144, but assume that you set the variable  to 12 + 12 instead so you get two differences 144 and 24. The difference is in the operational of the programmer — * and +. The programmer didn’t mean to set that calculation to + but he meant *. Though, the computer or the interpreter does not know the difference of what you thought. A computer will only do what you tell it to do.

Key terms: Syntax and Logic errors

In the next post, I will discuss the process of using a IPO chart and talk about pseudo-code to help create our first program