Friday, 11 October 2013

The story so far and starting with Python

Oh hey, fancy meeting you here! Do you come here often? I do. All the time!!

What is the story so far, well the first week was over and done with before I knew it.. because each class we had went over the course module and what is expected of us, the recurring message I got from many of the tutors is.. if you attend, it's highly unlikely that you will fail badly! So that was a nice positive message I took from that!

So, I'll start right off one of my favourites and probably head into something else with the next post. Today's post will be about

Programming

So, let's start off with what I have to do in the year.. there will be two assignments and then a written exam at a later date. Enough with that! Let's have a look at the first program I ever wrote in Python! We started using an awesome site called repl.it, which allows you try out your code in a multitude of different programming languages! After selecting python we were told to write the following in the left hand side of the page...

print "Hello World"

Then hit the play button in the middle of the screen and BOOM! Ho-lee-heck-balls, I have started my hopefully long love affair with programming languages! I'm pretty sure this is the first thing that anyone gets shown how to do, but I still felt like a superstar when I saw my little message show up on the right hand side of the screen. The tutor told us that our Hello World's are what is known as a string and the print is an instruction, or statement. Each new line is a new command or statement.

We were told about the three building blocks of each and every single program in the world


One thing about computers and programs that the tutor has told us on numerous occasions is; "You'd think computers are smart, but they aren't. They're stupid. When you need to tell them to do something, you need to tell explain it like you were talking to a child". We then moved on to the wonderful little things called Variables which are what he calls "little blocks of memory that stores whatever you put in it".  Obviously the next step was to show us a variable working!

total = 7
print "Total: ", total

Total is the variable, you're telling the computer that total is equal to 7, so it's got it's little block of memory ready to take whatever you give it.. and you gave it 7, so it's like "Don't you worry, I'll keep that safe for you buddy!" and that's just the first line! Again, you're making use of the print statement and the string, but this time you're telling it to print what it's got in it's total shaped box too! Because you have told the program with the comma that the "Total: " and the total are two separate commands but on the same line! 

We ended the first real programming lesson with the following code, can you tell me what happens and what it returns? Leave me a comment, if you dare!

num1 = (int(raw_input("Number1: ")))
num2 = (int(raw_input("Number2: ")))

add_total = num1+num2
sub_total = num1-num2
mult_total = num1*num2
div_total = num1/num2

print "Addition: ", add_total
print "Subtraction: ", sub_total
print "Multiplication: ", mult_total
print "Division: ", div_total

Good luck there lads and lasses!

1 comment:

  1. Set number 1 as 8 and number 2 as 7 and I got as follows;

    Addition: 15
    Subtraction: 1
    Multiplication: 56
    Division: 1

    Woohoo! Go me :)

    ReplyDelete

Leave me a comment!