As I talked about earlier, I am beginning to learn Python. I thought that this could be a good opportunity to document what I am learning and share my thoughts, experience and code.
To be totally cliche, I figured I would first demonstrate how to create the Hello World program in Python.
The most simplistic method to accomplish this is to simply print out the phrase:
print "Hello World!"
This looks almost exactly like PHP code expect you will notice there is no semicolon at the end of the statement. That is because Python does not use the semicolon to end a line.
Ok, let’s take this to the next level now. Let’s put this string inside a function that takes in a single parameter.
The function code looks like this:
def helloWorld(name): return "Hello " + name
So as you can see a function definition in Python begins with the word “def” followed by the function name (in this case helloWorld), and finally parameters in parenthesis with a colon at the end of the line.
Now this is where things get a little different. Instead of using regular block characters, we simply only indent the code to signify the code is apart of that block. It does not necessarily matter how many spaces or tabs you use in your code, however you must be consistent with how many spaces you use.
Now as you can see this function simply returns the word Hello with the name appended to it (with a space in between).
So now you can use the function in your code:
That’s it. You can how say Hello World in Python.