Hello, World! (Python series)

Hello, World! (Python series)

June 23rd, 2013 // 2:08 pm @

Welcome back.

Without further ado, I will start this section. Hopefully you have Python ready and installed on your OS. Since I am not a big fan of programming in under Win I chose to use Ubuntu, Python usually comes with most Unix distros. Same story with Ubunto. To make sure you’ve got Python ready just type

arad@ubunto-01:Python --version
Python 2.7.3

Aright, I am not going to cover the ‘how to install Python’, since there are several great tutorial online. So if you need to install, just do so and then we can continue from there.

As I described before, Python is a very simple but extremely powerful program. for our first program we are going to re-produce the good old ‘Hello, World’ program.

1- Within the desire folder create a blank Python page

nano hello.py

* Note the .py extension, this is Python’s extension

We start the code by initializing Pythons directory to our code. THIS IS VERY IMPORTANT, or Python won’t be recognized

#!/usr/bin/python

that path points to where I have the Python installed. This is the default Dir for most Linux distros. change that link ONLY IF you have Python installed somewhere else.

next is print directtive. This is so simple you won’t even believe it

print 'Hello, world!'

That’s it…! I know, it’d mind blowing, NOTE that I didn’t use semicolon (;) at the end of my directive unlike most other programs. So this is our code at the end

#!/usr/bin/python
print 'Hello, world!'

Save the code, and Exit.

Now, before you can run your program you have to grant access to the *.py . We can do that by using the chmod (CHange MOD ) command

arad@ubunto-01:chmod +x hello.py

Unlike C you don’t need to compile it, so just run with Python Extension. You should get a result like this

arad@ubunto-01:./hello.py
Hello, world!

Congratulations! You just wrote your very first Python. Stay tune for more fun with Python


Category : Linux &Python

Comments are closed.

On LinkedIn

LinkedIn

.

"I have no special talent.
I am only passionately
curious.
"
Albert Einstein

Subscribe Now