I’m Learning Python part 3

I’m Learning Python (part 3)

Python Logo

What Is Interactive Mode?

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.

What Are Modules?

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:

  • Open your editor and write the following in it:
    text = “I Love Python”
    print text
  • Save the file somewhere with the name “module1.py”
  • Now start a new file and write the following in it:
    import module1
    module1.text = “You Know I Love Python”
    print module1.text
  • Save the file in the same folder which has “module1.py” with the name “module2.py”
  • Now let the Python interpreter run your module2 file:
    python [SOME FOLDER]/module2.py
  • You must see the following:
    I Love Python
    You Know I Love Python

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.

What Is The Difference Between “import [MODULE]” And “from [MODULE] import”?

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.

How can I write hints and comments in 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

Bottom Line:

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.

One thought on “I’m Learning Python part 3

Leave a Reply