As I needed a certain script that did a certain thing that executed a certain whatever where I wanted certain activities, I decided not to use PHP, but try another, more suitable language for the task.

I chose Python in the end and decided to share some of the basics I had to learn with y’all.

In this post I will try to explain how why and what to create a simple “Hello World” Python script with some fancy tweaks in the end. Because if we just want to

print "Hello World"

, well that would be it.

First of all, you need to install python on your workstation. I will try to cover this all, but can only test on Mac. In your terminal:

brew install python

For Linux this will be the same, just remove brew and replace it with apt-get

Now, let us create our very first Python story. Create a new file in a directory of your choice and name it as you wish but don’t forget to add the .py extension. I’m calling it printer.py, you can however use every random string of characters in your file name that you wish.

Open up the file you created an type in:

print "Hello World of Python"

Save it, open up your terminal, navigate to the place you saved the file and execute it by using the python command followed by the python script.
In this case:

python printer.py

That was quite easy wasn’t it? Now let us try to make it a little fancier.

In Python you can use classes as objects with properties. Lets us set a class with some (ANSI) colors in the top of our file. Be careful, Python does work with indentation. Align everything correct and I will keep working.
Add at the top:

class bcolors:
    HEADER = '\033[95m'
    BLUE = '\033[94m'
    GREEN = '\033[92m'
    ORANGE = '\033[93m'
    RED = '\033[91m'
    NORMAL = '\033[0m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'

Lets print Hello world again, but then in red.

print bcolors.RED + "Hello World" + bcolors.NORMAL

Execute it in your terminal and your text will be red.
As you can see, you can concatenate strings and variables with the + sign. Also you can reach properties of classes by just calling the class, followed by a dot and then the property you want.

Well, thats getting a bit more fancy ‘aight?

Next, use a loop to print all the colors to the screen, we love rainbows!
For this to work we will have to adjust our class and make it a plain simple array and then, well see the code…

class bcolors:
    HEADER = '\033[95m'
    BLUE = '\033[94m'
    GREEN = '\033[92m'
    ORANGE = '\033[93m'
    RED = '\033[91m'
    NORMAL = '\033[0m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'

colorArray = {
    '\033[95m',
    '\033[94m',
    '\033[92m',
    '\033[93m',
    '\033[91m',
    '\033[90m',
    '\033[89m',
    '\033[88m',
}


for color in colorArray:
	print color + "Hello World" + bcolors.NORMAL

Try tabbing the print to the first tab position and try to execute the script again. As you will see, this doesn’t work. This is caused by the fact that Python uses indentation to care for blocks of code.

Thats it for now, I will post a blog in the near future using imports and if’s!