CHAPTER 1

image

Hello Python

Let’s review a simple Python program that will explain the use of the Python Interactive Shell along with a few tips.

Python’s Interactive Shell

Once you have Python installed on your system, you can use the Interactive Shell to try simple lines of code, debug some problematic code, and even use it as a simple calculator.

In a terminal (Linux) or Command Prompt (Windows), simply type:

python

If you are running on Linux, you will see something like this:

Python 2.7.3 (default, Apr 10 2013, 05:09:49)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

Or if you are running on Windows, you would see something like this:

Python 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

As you can see, there is not a lot of difference in the two.

Commands

Once you see the three “right arrow” symbols (>>>), you are at the user prompt. From here you can enter commands:

>>> 3+23
26
>>> print "This is a test of the interactive mode"
This is a test of the interactive mode
>>> a = 3
>>> b = 4
>>> print a * b
12
>>> print a + b
7
>>> exit()

Multiline Statements

You can also enter multiline statements that are indented as in a for loop or an if statement (which will be discussed in a later chapter). For example:

>>> for i in range(0,3):
...     print i
...
0
1
2

When you are in multiline statements, the interpreter will automatically recognize this (by the “:” colon) and prompt you with three dots instead of three right arrows. Be sure to use spaces to indent your next line(s). To end the multiline mode, simply press “enter” on a blank line. Your multiline statement will then be evaluated (if possible) and the result will be displayed.

To exit the interactive shell, type “exit()” as shown earlier.

The Code

I know that you are “chomping at the bit” to write your first program, so we'll get started with a very simple example. Assuming that you have Python installed on your machine, fire up your favorite text editor to enter the following lines:

print 'Hello. I am a Python program.'
name = raw_input("What is your name? ")
print 'Hello there ' + name + '!'

Save this as ‘hello.py’ whereever you like. I'd suggest you create a subfolder of your home directory to store all your test code, like this:

C:
   |-Development
                |-Tests

Now in a command or terminal window, change to the folder you saved the above file in and type:

python hello.py

You should see something like this:

greg@earth:∼$ python hello.py
Hello. I am a Python program.
What is your name? Greg (The program pauses here for you to enter your name)
Hello there Greg!

or under Windows:

C:> python hello.py
Hello. I am a Python program.
What is your name? Greg (The program pauses here for you to enter your name)
Hello there Greg!

Decoding the Code

Let’s look at each line of the code.

print 'Hello. I am a Python program.'

The print command will output into the terminal or command prompt box whatever follows.

In this case, we are telling Python to display “Hello. I am a Python program.” Notice that what we want displayed is in single quotes in our code. You can use either single or double quotes to enclose the string. However, when you want to show single quotes within a display string, you should use double quotes to enclose the entire string. If you want to display double quotes within a display string, you should use single quotes to enclose the string. We'll discuss this later in Chapter 4.

name = raw_input("What is your name? ")

Here we are doing two things. First, we are using the raw_input command to tell Python to display a prompt to the user (the “What is your name? “ part) and then wait for the user to input something, even if it is a simple “enter” key to enter a blank input. It is important for you to include the space after the question mark and before the ending quote. This gives a cleaner look for the program, in that the ultimate user has an idea that there is something they need to do. The second thing we are doing is assigning the user’s response to a variable called ‘name’. We will talk more about variables in Chapter 2. For now, think of a variable as a simple box that holds something. In this case, it would hold the name of the user:

print 'Hello there ' + name + '!'

Again, we are telling Python to display a line in the terminal window, but this time we are creating the line out of three strings of text. This is called concatenation. The plus symbol ('+') is used to join the parts of the line together. So we have “Hello there” and the user name and a “!”.

Comments

Comments allow us to put notes within our code. When the interpreter encounters a comment, it ignores the rest of the line. It is considered good practice to comment your code well to explain the thought process behind it. If you ever have to come back to your code after a week or so, the comments will help you remember what you were doing. It also allows other people to understand your code.

To create a comment line, simply start the comment with the hash symbol (#). The following lines are example of commented lines:

# This is a full line comment.
aVariable = 23 # this is an in-line comment.

It is important to remember that anything following the hash symbol is considered a comment. You can also use triple double quotes (""") to enclose multiline comments.

"""
This is an example of a multiline comment.
Everything between the sets of triple double quotes is considered a comment.
"""

Comments also allow us to tell the interpreter to ignore lines of code that might be giving us problems or a block of code that is incomplete.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset