Image via this website |
First things first, you need to download the Python interpreter. For the purposes of me not having to read up on version 3 go with version 2.7. Though I doubt it'll make a difference. I don't believe version 3 broke a lot of code.
For this post I'll just stick with basic output right now. Since I'm not doing anything fancy everything is just text.
After it extracts and you run it you'll be greeted with the shell. Which will be where we will play with today.
snake skin |
Print "Hello World"
After that just hit enter and it'll execute. You should notice Print and "Hello World" were different colors. Print is a command in the language while "Hello World" is a string, for now we'll just denote strings by the pairing of " but it can be done otherwise. Anything between the two "s will be counted as being part of the string. Print will try it's best to print out what's following after it. For instance you can print out the result of a math function. Try the following.
Print 5+4
You get the obvious result of 9. Now for a tricky part, in most languages there is a concept of Concatination. Where you are able to combine strings together. To show this off try the following.
Print "Hello " + "World"
The result is the more complex version of Hello World above. What happens is effectively string math. It takes 'World' and 'adds' it to 'Hello ' to produce 'Hello World'. There are other math commands that python likes using with strings but I'll save those for later. Though speaking of math, what happens if we wanted to use numbers with strings? Like print out #1) for some innane reason? Well lets try it.
print "#"+1+")"
You got a big wall of red text there. That's an error, the interpreter has met with something it did not like. In this case I'll skip letting you figure out the horrors of that message and tell you what went wrong. It's an annoying part of python to me but you can not straight out concatinate strings and numeral objects. You have to convert the int, I'll explain what that means later, into a string. So to do that.
print "#"+str(1)+")"
No ugly red text. str is an inbuilt function in Python which I will get into later, all you need to know is by placing something between the brackets it'll try it's best to make it a string. Though this is an academic case, if you knew you were going to need one there you could have had it contained as "#1)". I don't know the best way to explain this but when it's in a string, it's in a string meaning when it's surrounded by "s it's seen as a character and not as a potential number by the interpretation. So you should have something like this.
I think this is week 1 of a computer science course in a nutshell |
And I'll leave that as it for today. While not immediately useful it's a glimpse into programing. I may take this to completion faster if anyone is interested though it'll serve as something I like doing, showing people how to program.
Edit: Forgot to briefly touch str()
Wow, there's a lot of work that goes into these kinds of things!
ReplyDeleteYeah for the snake.
ReplyDeleteI wish this post was actually about snakes :\
ReplyDeleteI'm actually too tired to do this right now, but I do love stuff like this, so I'm more than likely going to give it a go when I have more energy, you know, during my funeral.
ReplyDelete