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’ll find an executable called ‘python’, when you run it you’ll find a terminal and you’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 “help”, “copyright”, “credits” or “license” 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’s try:<br /> <em>>>> print “I Love Python”<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 ”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.
<h1 style="font-family: Georgia;"> What Are Modules? </h1> <p> Let’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’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’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 “import” statement, so let’s try something: </p> <ul> <li> Open your editor and write the following in it:<br /> text = “I Love Python”<br /> print text </li> <li> Save the file somewhere with the name “module1.py” </li> <li> Now start a new file and write the following in it:<br /> import module1<br /> module1.text = “You Know I Love Python”<br /> print module1.text </li> <li> Save the file in the same folder which has “module1.py” with the name “module2.py” </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’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”.<br /> 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?<br /> 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.<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 “import [MODULE]” And “from [MODULE] import”? </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’s name followed by a period before the variable’s name.<br /> 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:<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’s name before it, you can simply use the “text” 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 = “I Love Python”<br /> in module2.py write:<br /> text = “You Know I Love Python”<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 – # – character will hint the following text until the end of the line:<br /> text = “I Love Python” <em>#a declaration of a variable named text</em> </p> <h1 style="font-family: Georgia;"> Bottom Line: </h1> <p> 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. </p> <p> Cheers. </p></div>