Getting more familiar with Python Programming

0
18754
Python Tutorial – Part (2)

Let’s start from the point at which we stopped the last time. In Python Tutorial Part1, we have written a very simple program, that prints the sentence “Hello, I am a Python Programmer”.

Image 1: First Program

Our first program consists of one line of code, which in turn contains one command statement. The statement uses the built-in function print to write the string “Hello, I am a Python Programmer” to the standard output. The print function takes one or more arguments, and then prints its argument(s) to the screen. Actually, in the interactive shell we don’t need to use the print function to echo the output to the screen; just typing the string surrounded by double (or single) quotes at shell has “nearly” the same effect:

Image 2: Printing strings in the Interactive Shell without using the print function

But, as you have already noticed, strings printed using this way are printed with surrounding single quotes. (Annoying, huh!!) So, it is better to use the print function whenever you need to output something to the screen.

Now, consider the need to print the word “PYTHON” to the screen using asterisk character:

Image 3: The word “PYTHON” written in asterisks

How could we achieve this?

Bravo!!! You are right!!
5 print statements are needed; each one will print a horizontal slice of the word. Actually, this is exactly the idea of the Dot Matrix printers (if you have seen one before).
So, let’s try it!!!

Image 4: The output doesn’t display as expected when run from the Interactive Shell

– Oops!! What happened? It didn’t print correctly!!

Yes, because you forgot that the Interactive Shell will execute each line of code as soon as it is entered. So, the individual print statements appear within the output, and that is why you see it distorted!!

– So, How can we make it print correctly as desired?

To make this code work correctly (by printing the desired word as expected), put the statements together in a script file. So, simply open your favorite Text Editor (vi in UNIX, vim in Linux, Notepad++ or IDLE in Windows) and type the above statements in sequence.

Image 5: Typing the program in script file.

Save the file, and exit. Now, execute it as follows:

Image 6: Executing the python script

Ah… It works!!!

What we did here to execute the script file is that we run the python interpreter and asked it to interpret and execute the file print_python.py. The following is another way to execute the program:

Image 7:  Another version of the program

To execute it, use the following command:

Image 8: A second way to execute the script.

In this way, the first statement in the script (Called Shebang) tells the program executer to use the /bin/python as the interpreter for this script file. We have modified the permissions on the file to make it executable, so that we could execute it as a normal UNIX/Linux executable using (./script_name)

* * * * * *

Using Comments in Python

In general, it is a practice to add comments inside your code. Adding comments makes your code more readable. If there is a need to modify the code in the future, the absence of comments will make it difficult to understand the code. You may wonder for example what this if statement does, and this loop is used for what. Even worse, imagine that the one who is about to make the modifications is somebody else (not the code author); in this case the task will be much more difficult. Again, imagine having to read and understand a program consisting of five or ten thousands of lines (No, I am not terrifying you).

To mark a text as a comment in Python, precede it with #. The # character comments what appears to its right up to the end of the line. The comments are completely ignored by the Python interpreter, and are not executed. The following is a third version of the script that prints the word “PYTHON” in asterisks, with some comments added:

* * * * * *

Prompting User for Input

Python uses the built-in function input to accept a line of text from the user. Consider the following script that prompts the user to enter his name, and responds with a greeting:

Image 9: A script that accepts input from the user.

Now, let’s execute it:

Image 10: Executing the user_input script.

* * * * * *

Leading spaces in Python

The following is another version of the user input script:

Image 11: A modified version of the user input script

with extra white space before one of the statements.

– Wait!! Is that another version?! I can’t see any difference!!!

No, actually there is!! And it is a big one.

– You mean the statement in line 2 that is shifted to the right one space? Is that a big deal?!

Wait until we execute it, and you will notice the difference.

Image 12: The Python interpreter generates an error.

– Oops!! The interpreter reported an error!!

Yes!! I told you, but you didn’t believe me. This is simply because Python cares about the leading white spaces that come before the beginning of each statement. I will tell you why soon, but for now remember not to insert unnecessary spaces at the beginning of your scripts’ lines.

* * * * * *

Empty Lines in Python

– What about the empty lines? Will they also cause the Python interpreter to report an error?!

No, don’t worry about them; Python interpreter ignores the blank lines.

* * * * * *

In this Python Tutorial, we have gone through several features of the Python language. We have illustrated these features with some examples in an attempt to get you more familiar with Python.
In the Python Tutorial Part 3, we will talk about variables in Python and the Data types.
So, don’t go anywhere. We won’t be late.

LEAVE A REPLY

Please enter your comment!
Please enter your name here