Abd Allah Diab's Blog
My Technical Blog
My Technical Blog
Oct 11th
When you install Python on your platform you'll find an executable called 'python', when you run it you'll find a terminal and you'll see something like the following:
Python 2.6 (r26:66721, Oct 2 2008, 11:35:03) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
This is called interactive coding (or interactive mode), here you can write Python statements and the interpreter will execute them directly, let's try:
>>> print "I Love Python"
I Love Python
>>> print 2 ** 10
1024
>>>
You can write any statement here (even those who are more than on line as we'll see later).
If you remember we said that the Python compiler and interpreter are always in present at runtime, so that's why you can execute the statements directly here. Another benefit you get from the presence of the compiler and interpreter at runtime that you can let your program execute Python code written by the user, and that's what some programs do, they let the user manipulate the program's behavior by writing his/her code inside the program.
To do so you can run the "exec" statement like this:
>>> exec "print \"I Love Python\""
I Love Python
>>>
Here we invoked a method called "exec" and it takes a string parameter, this string contains Python code, and the interpreter will run it, so you can simply read code from the user and run it for him/her. This method is available anywhere (not just in the interactive mode).
You must have noticed using the escape character \" in the text, and that is to tell the interpreter that this is not the end of the string and it is a double quotation inside the string, there are more escape characters that we'll find more in the upcoming parts.
Another feature to talk about in the interactive mode that you don't have to write print every time, so the next statement will run without errors:
>>> "I Love Python"
'I Love Python'
>>>
But beware, you must use print in any other mode, interactive mode is the only mode which doesn’t need the print statement, that's 'cuz it is made to print the results.
You might ask, what about statements that don't return values, what would the interactive mode print for them? The answer is: simply nothing
>>> text = "I Love Python"
>>>
The previous statement is an assignment statement, which declares a string called "text" and has the value of "I Love Python".
To exit the interactive code you use the break character (Ctrl+Z in Windows, Ctrl+D in Linux) and hit return.
Let's here explain the Python program hierarchy:
Each program consists of modules -> Each module consists of statements -> Each statement consists of expressions -> Each expression creates and processes objects -> Objects are the raw materials you'll use
When you write a Python program you write it in files, these files usually called modules, the module is a namespace, so each module has its own variables and methods. You can have two variables with the same name, but you must put each one in a module.
We'll use modules (for now) to run our code multiple times without the need of re-typing it again.
You can access another module using "import" statement, so let's try something:
What happened here? In module2 I only printed the variable after editing it, why was it printed before it was edited?
When you import a module, you're simply running it, so the Python interpreter ran module1, and executed the print statement so you see "I Love Python", then it executed module2, it edited the value of the variable in module1, then executed the print statement in module2, and you see "You Know I Love Python".
So importing means running, but what if I needed to run the code again? Knowing that you can't import a module more than one, what should we do?
There is another statement "reload" which is a method and takes a string with the name of the module to re-run it again, but it should be imported before.
Try adding this line to your code in module2.py:
reload(module1)
Re-run your module2 and you must see:
I Love Python
You Know I Love Python
I Love Python
The third line is actually the print statement in module1 file.
The first – import [MODULE] – imports and runs the module, but when you want to access its variables you should write: [MODULE].[VARIABLE], so each time you want to use the variable you should write the module's name followed by a period before the variable's name.
The second – from [MODULE] import – imports the specified variables from the module, so to import the "text" variable from "module1" in the previous example you write:
from module1 import text
To import more than one variable separate them with columns:
from [MODULE] impor [VAR1], [VAR2], …, [VARN]
Notice that importing a variable lets you use the variable without the need to write the module's name before it, you can simply use the "text" variable in module2 after importing it without writing module1.text.
Beware that importing a variable that has the same name of a local variable will replace the local one, for instance:
in module1.py write:
text = "I Love Python"
in module2.py write:
text = "You Know I Love Python"
from module1 import text
print text
Executing module2 will give you: I Love Python.
Using the sharp – # – character will hint the following text until the end of the line:
text = "I Love Python" #a declaration of a variable named text
Next time we'll learn about built-in types in Python, till then try to create and import and print modules and variables, try that in both files and interactive mode, try to make syntax errors, try to divide by zero.
Cheers.
Oct 7th

In the world there are more than 1 million Python user, that’s because it’s
open source, and because it is implemented for every platform, and it comes
included within Linux distributions and Macintosh computers and more.
See Python website for more.
Python is not compiled (almost), it is interpreted. So when you run the program
the interpreter will run it line by line without compilation.
You might say “Why did you say almost?”, the answer is, Python as described
before can be compiled into byte code, so it’ll be faster for the interpreter
to execute it than the source code, just like Java’s byte code and .NET’s MSIL.
Python files have a .py extension normally, so when you write Python code you
should save it with a .py extension.
Let’s try and write our first Hello World Python program:
Where [File] is the location of your .py file.
Hello World, It’s My First Python Program
Very easy, right?
Although it is not that big program but you can run your files this way
.
Now let’s understand what happened:
The Python interpreter was given a file to run.
If it has write privileges it’ll compile it into byte code and you’ll see that
a new file was created next to your file with a .pyc extension, if it doesn’t
it’ll run it as source code.
It reads the first line (which is a print statement) and runs it and you see
the result, the execution takes place inside the PVM – Python Virtual Machine,
which handles the execution of Python byte code and source code to run on more
than one platform without change in code.

If you have a background in C or C++ you might find a change in the way of running
and compiling code, ‘cuz there is no making and linking in Python, and it is
not compiled into machine code (binary file), instead it runs inside a virtual
machine, that’s why some Python code will run faster in C++ and C.
But don’t be afraid, 90% of the time you’ll be running your programs as if they
were written in C or C++, there won’t be any difference in the time of execution,
and that’s because of the Python simplicity which makes the program in Python
shorter and simpler than C or C++, and because it’s optimization which we’ll
describe later.
In Python you’ll notice that the compiler and the execution environment are
one thing, the compiler always present in runtime, and that leads to rapid development
cycle, with no linking and making stage.
CPython runs inside the PVM, while Jython runs inside the JVM and IronPython
.NET runs inside .NET Framework or Mono.
Jython and IronPython .NET allows the Python program to use Java and .NET classes
respectively.
If you don’t wanna use Java or .NET classes you should use CPython, ‘cuz it’s
faster and the most complete, CPython runs like C and C++ so it is the fastest.
In Jython the Python byte code is translated into Java byte code to be run inside
the JVM, while in .NET it is translated to MSIL.
Python has something called Frozen Binaries, which are the executables, if you
chose to create a frozen binary for your program, you’d end up with an executable
that runs on a specified platform (exe for Windows).
This executable has a great feature; it includes your program along with its
dependencies and the Python interpreter. Yes you don’t have to tell the user
to install a Python interpreter to be able to use your program, he/she doesn’t
have to know that your program is a Python program.
You can use Py2Exe to get an exe of your program (Which runs only in Windows),
or PyInstaller which gives you the ability to create a frozen binary for Windows,
Linux or UNIX. And you can use Freeze the original one. Those are available
free of charge, you can check Python website or Vaults of Parnassus (http://www.vex.net/parnassus).
Python comes with a simple IDE called IDLE you can use it, or you can use Emacs, but IMHO
Eclipse with PyDev make a great IDE for you and Python.
Today it was just a description on how Python runs your program, and we wrote
our Hello World program.
For the next part, try to use the ** operator – which is the power operator
in Python – to see how much you can do in Python, e.g.:
print 2 ** 10
1024
Try huge numbers and see
Cheers.
Oct 4th

I’ll start posting this series of blogs on WordPress and CSC, here I’ll write what I’m gonna learn on my way to learning Python.
If you don’t know what Python is, or you’ve heard about it but you’re not sure what it is, my first blog will cover it for you.
Python is a programming language, this programming language was created in 1990, this programming language has proved its power through the last 18 years she’s been with us.
The most points that will catch your interest in this language are:
Here I’ll tell you what fields you can use Python in, but in a glance ‘cuz I don’t know how to use it yet.
That’s enough for today, hope I’ll tell you more on my next blog.
For now I’d suggest you to read more about the language and its features from the language website: www.Python.org.
And also don’t forget to download the appropriate built for your platform.
Cheers.
Jun 12th
Hi all .Net developers and programmers.
If you’re a .Net developer or a programmer you must have heard of the Drawing namespace in System namespace, and you maybe used it to draw in your applications.
For those who used them they must have found that moving a single shape in a scene using simple code is not that easy, you’ll have to redraw the whole scene with the shape in its new location, the same thing happens for rotating and scaling a shape, so I had an idea, “Why can’t we just write Rectangle.Move(X,Y) and it moves? Why should I remember every shape coordinates to redraw a small shape in a new position?” so I answered the question.
I developed a small class that lets you manipulate your drawings as Objects, I mean that you can define a rectangle and just write Rectangle.Move(x,y) and it’ll move to the given position without much hard-coding, I defined (Rectangle, Ellipse, Lines), but you can also define your own shape, you just have to inherit the base class (MagicGraphics.Shape) and you’ll have to write your own Render sub (void) and ToString function, and you can add as much as you want of properties.
All the shapes you draw must be placed in a ShapeContainer that handles the drawing according to the Z-Order between shapes.
The following links are for the class and the testing project in VB .Net 2005:
The Class - The Testing Project.
The testing project shows how to draw, rotate and move shapes, you can start using my class directly after downloading it.
P.S.: The whole project is still Beta but I wanted to share the first build with you.
Have fun, and please tell me what do you think.