Python is an interpreted, cross-platform, object-oriented language that can be used to write large-scale Internet search engines, small administration scripts, GUI applications, CGI scripts and more. The creator of the language, Guido van Rossum, combined a clean syntax with popular elements from several existing languages to produce Python.
Python is a freely distributed technology whose open-source nature has encouraged a wide base of developers to submit modules that extend the language. Using Python’s core modules and those freely available on the Web, programmers can develop applications that accomplish a great variety of tasks. Python’s interpreted nature facilitates rapid application development (RAD) of powerful programs. GUI applications, in particular, can be tested quickly and developed using Python’s interface to Tcl/Tk (among other GUI toolkits).
1. First Python Program
In this section, we examine a simple Python program and explain how to work with the Py-thon programming environment. For this chapter, we assume the reader has installed Py-thon 2.0 or later. [Note: The resources for this book posted at our Web site, www.deitel.com, include step-by-step instructions on installing Python on Windows and Unix/Linux platforms.] Python can be executed on a program stored in a file, or Python can run in interactive mode, where users enter lines of code one at a time. Among other things, interactive mode enables program writers to test small blocks of code quickly and helps contribute to a relatively rapid development time for most Python projects.
Figure 28.1 is a simple Python program that prints the text Welcome to Python! to the screen. Lines 1–2 contain single-line comments that describe the program. Com-ments in Python begin with the # character; Python ignores all text in the current line after this character. Line 4 uses the print statement to write the text Welcome to Python! to the screen.
# Fig. 28.1: fig28_01.py
# A first program in Python
print "Welcome to Python!"
Welcome to Python!
Fig. 28.1 Simple Python program.
Python statements can be executed in two ways. The first is by typing statements into a file (as in Fig. 28.1). Python files typically end with .py, although other extensions (e.g.,
.pyw on Windows) can be used. Python is then invoked on the file by typing
at the command line, where file.py is the name of the Python file. [Note: To invoke Py-thon, the system path variable must be set properly to include the python executable. The resources for this book posted at our Web site, www.deitel.com, include step-by-step instructions on how to set the appropriate variable.] The output box of Fig. 28.1 contains the results of invoking Python on fig28_01.py.
Python statements can also be interpreted interactively. Typing
at the command prompt runs Python in interactive mode.
Figure 28.2 shows Python running in interactive mode on Windows. The first two lines display information about the version of Python being used. The third line begins with the Python prompt (>>>). A Python statement is interpreted by typing the statement at the Python prompt and pressing the Enter or Return key.
The print statement on the third line prints the text Welcome to Python! to the screen. After printing the text to the screen, the Python prompt is displayed again (line 5), and Python waits for the user to enter the next statement. We exit Python by typing Crtl-Z (on Microsoft Windows systems) and pressing the Return key. [Note: On UNIX and Linux systems, Ctrl-D exits Python.]
Python 2.1 (#15, Apr 16 2001, 18:25:49) [MSC 32 bit (Intel)] on win32 Type "copyright", "credits" or "license" for more information.
print "Welcome to Python!" Welcome to Python!
Fig. 28.2 Python in interactive mode.
2. Python Keywords
Before we discuss Python programming in more detail, we present a list of Python’s key-words (Figure 28.3). These words have special meanings in Python and cannot be used as variable names, function names or other objects.
A list of Python keywords can also be obtained from the keyword module. Figure 28.4 illustrates how to obtain the list of Python keywords in interactive mode. [Note: We discuss modules further in Section 28.4.]
Python is a case-sensitive language. This means that Python treats variable x (lower-case) and variable X (upper case) as two different variables. Similarly, the statement
Def = 3
is a valid Python statement, but the statement
def = 3
causes a syntax error, because def is a keyword and, therefore, not a valid variable name.
Line 5 contains the function definition header for function greatestCommonDi-visor. This function computes the greatest common divisor of two numbers—the largest integer that divides evenly into both numbers. The keyword def marks the beginning of the function definition. The function takes two parameters: x and y. The list of parameters is placed inside parentheses (()), and the parameter list is followed by a colon (:).
Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.