Abdullah Diab’s Blog

I’m Learning Python part 3

I’m Learning Python (part 3)

<p>
  <img title="Python Logo" src="http://www.python.org/images/python-logo.gif" alt="Python Logo" /></div> 
  
  <h1 style="font-family: Georgia;">
    What Is Interactive Mode?
  </h1>
  
  <p>
    When you install Python on your platform you&#8217;ll find an executable called &#8216;python&#8217;, when you run it you&#8217;ll find a terminal and you&#8217;ll see something like the following:<br /> <em>Python 2.6 (r26:66721, Oct 2 2008, 11:35:03) [MSC v.1500 32 bit (Intel)] on win32<br /> Type &#8220;help&#8221;, &#8220;copyright&#8221;, &#8220;credits&#8221; or &#8220;license&#8221; for more information.<br /> >>></em><br /> This is called interactive coding (or interactive mode), here you can write Python statements and the interpreter will execute them directly, let&#8217;s try:<br /> <em>>>> print &#8220;I Love Python&#8221;<br /> I Love Python<br /> >>> print 2 ** 10<br /> 1024<br /> >>></em><br /> 


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 &#8221;I Love Python&#8221;”
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 &#8221; 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.

  <h1 style="font-family: Georgia;">
    What Are Modules?
  </h1>
  
  <p>
    Let&#8217;s here explain the Python program hierarchy:<br /> 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&#8217;ll use 🙂<br /> 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.<br /> We&#8217;ll use modules (for now) to run our code multiple times without the need of re-typing it again.<br /> You can access another module using &#8220;import&#8221; statement, so let&#8217;s try something:
  </p>
  
  <ul>
    <li>
      Open your editor and write the following in it:<br /> text = &#8220;I Love Python&#8221;<br /> print text
    </li>
    <li>
      Save the file somewhere with the name &#8220;module1.py&#8221;
    </li>
    <li>
      Now start a new file and write the following in it:<br /> import module1<br /> module1.text = &#8220;You Know I Love Python&#8221;<br /> print module1.text
    </li>
    <li>
      Save the file in the same folder which has &#8220;module1.py&#8221; with the name &#8220;module2.py&#8221;
    </li>
    <li>
      Now let the Python interpreter run your module2 file:<br /> python [SOME FOLDER]/module2.py
    </li>
    <li>
      You must see the following:<br /> I Love Python<br /> You Know I Love Python
    </li>
  </ul>
  
  <p>
    What happened here? In module2 I only printed the variable after editing it, why was it printed before it was edited?<br /> When you import a module, you&#8217;re simply running it, so the Python interpreter ran module1, and executed the print statement so you see &#8220;I Love Python&#8221;, then it executed module2, it edited the value of the variable in module1, then executed the print statement in module2, and you see &#8220;You Know I Love Python&#8221;.<br /> So importing means running, but what if I needed to run the code again? Knowing that you can&#8217;t import a module more than one, what should we do?<br /> There is another statement &#8220;reload&#8221; 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.<br /> Try adding this line to your code in module2.py:<br /> reload(module1)<br /> Re-run your module2 and you must see:<br /> I Love Python<br /> You Know I Love Python<br /> I Love Python<br /> The third line is actually the print statement in module1 file.
  </p>
  
  <h1 style="font-family: Georgia;">
    What Is The Difference Between &#8220;import [MODULE]&#8221; And &#8220;from [MODULE] import&#8221;?
  </h1>
  
  <p>
    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&#8217;s name followed by a period before the variable&#8217;s name.<br /> The second – from [MODULE] import – imports the specified variables from the module, so to import the &#8220;text&#8221; variable from &#8220;module1&#8221; in the previous example you write:<br /> from module1 import text<br /> To import more than one variable separate them with columns:<br /> from [MODULE] impor [VAR1], [VAR2], …, [VARN]<br /> Notice that importing a variable lets you use the variable without the need to write the module&#8217;s name before it, you can simply use the &#8220;text&#8221; variable in module2 after importing it without writing module1.text.<br /> Beware that importing a variable that has the same name of a local variable will replace the local one, for instance:<br /> in module1.py write:<br /> text = &#8220;I Love Python&#8221;<br /> in module2.py write:<br /> text = &#8220;You Know I Love Python&#8221;<br /> from module1 import text<br /> print text
  </p>
  
  <p>
    Executing module2 will give you: I Love Python.
  </p>
  
  <h1 style="font-family: Georgia;">
    How can I write hints and comments in Python?
  </h1>
  
  <p>
    Using the sharp &#8211; # &#8211; character will hint the following text until the end of the line:<br /> text = &#8220;I Love Python&#8221; <em>#a declaration of a variable named text</em>
  </p>
  
  <h1 style="font-family: Georgia;">
    Bottom Line:
  </h1>
  
  <p>
    Next time we&#8217;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.
  </p>
  
  <p>
    Cheers.
  </p></div>