Chapter 1. Introduction to Programming

Before we dive into the details of Python programming, it helps to learn a bit about what Python is and what kinds of programs it is used for. We will also outline exactly what it is that programmers do. Finally, we’ll learn how to install Python and run the IDLE editor that comes with it.

If you are new to programming, this short introduction should help you get your footing in preparation for learning the Python programming language.

If you already have a grasp of the basic concepts, feel free to jump ahead to the sections on how to install Python and run the editor.

The Python Language

So what is Python? Briefly, it is a computer programming language and a corresponding set of software tools and libraries. It was originally developed in the early 1990s by Guido van Rossum, and it is now actively maintained by dozens of programmers around the world (including van Rossum).

Python was designed to be easy to read and learn. Compared with programs written in most other programming languages, Python programs look neat and clean: Python has few unnecessary symbols, and it uses straightforward English names instead of the cryptic syntax common in other languages (Ruby and Perl, I’m looking at you!).

Python is a very productive language: Once you’re proficient with Python, you can get more done with it in less time than you can in most other programming languages. Python supports—but doesn’t force you to use—object-oriented programming (OOP).

Python comes with a wide range of ready-made libraries that can be (freely!) used in your own programs; as some Python programmers like to say, Python comes with “batteries included.”

One of the most important features of Python is its maintainability: Since Python programs are relatively easy to read and modify, they are easy for programmers to keep up to date. Program maintenance can easily account for 50 percent or more of the work a programmer does, and so Python’s support for maintenance is a big win in the eyes of many professionals.

Finally, a word about the name. According to Python’s originator, Guido van Rossum, Python was named after the Monty Python comedy troupe. Despite this mirthful origin, Python now uses a pair of iconic blue and yellow snakes—presumably pythons—as its standard symbol.

What Is Python Useful For?

While Python is a general-purpose language that can be used to write any kind of program, it is especially popular for the following applications:

  • Scripts. These short programs automate common administrative tasks, such as adding new users to a system, uploading files to a Web site, downloading Web pages without using a browser, and so on. Due to its simplicity, Python is also a popular choice as a scripting language for many products, such as the video game Civilization 4.

  • Web-site development. A number of Python projects—such as Django (www.djangoproject.com), Turbo Gears (www.turbogears.org), and Zope (www.zope.org)—are popular among many Web-site developers as tools for quickly creating dynamic Web sites. For instance, the popular news site www.reddit.com was written using Python.

  • Text processing. Python has excellent support for handling strings and text files, including regular expressions and Unicode.

  • Scientific computing. Many superb scientific Python libraries are available on the Web, providing functions for statistics, mathematics, and graphing.

  • Education. Thanks to its relative simplicity and utility, Python is becoming more and more popular as a first programming language in schools.

Of course, Python isn’t the best choice for all kinds of programming projects. Python is often less efficient than languages such as Java, C, and C++. So, for example, you wouldn’t use Python to create a new operating system.

But when you need to minimize the amount of time a programmer spends on a project, Python is often an excellent choice.

How Programmers Work

While there is no recipe for writing programs, it is helpful to understand the basic steps that all programmers follow.

The programming process

  1. Determine what your program is supposed to do—that is, figure out its requirements.

  2. Write the source code (in our case, the Python code) in IDLE (short for Python’s Integrated Development Environment) or any other text editor. Python source code files end with .py: web.py, urlexpand.py, clean.py, and so on. This is often the most interesting and challenging step, and it often involves creative problem solving.

  3. Convert the source code to object code using the Python interpreter. Python puts object code in .pyc files. For example, if your source code is in urlexpand.py, its object code will be put in urlexpand.pyc.

  4. Run, or execute, the program. With Python, this step is usually done immediately and automatically after step 2 is finished. Thus, in practice, Python programmers rarely work directly with object code or .pyc files.

  5. Finally, check the program’s output. If errors are discovered, go back to step 1 to try to fix them. The process of fixing errors is called debugging. For large or complex programs, debugging can sometimes take up most of the program development time, so experienced programmers try to design their programs in ways that will minimize debugging time.

As Figure 1.1 shows, this is an iterative process: You write your program, test it, fix errors, test it again, and so on until the program behaves correctly.

The basic steps of writing any computer program. Typically, after you check your program output, you find errors and so must go back to the code-writing step to fix them.

Figure 1.1. The basic steps of writing any computer program. Typically, after you check your program output, you find errors and so must go back to the code-writing step to fix them.

Installing Python

Python is a hands-on language, so now we will see how to install it on your computer.

Procedure 1.1. To install Python on Windows:

  1. Go to the Python download page at www.python.org/download using your favorite Web browser.

  2. Choose the most recent version of Python 3 (it should have a name like Python 3.x, where x is some small number). This will take you to the appropriate download page with instructions for downloading Python on different computer systems.

  3. Click the appropriate installer link for your computer. For instance, if you are running Windows, click Windows x86 MSI Installer (3.x).

  4. Once the installer has finished downloading, run it by double-clicking it.

  5. After the installation has finished (which could take a few minutes), test to see that Python is installed properly. Open the Windows Start menu and choose All Programs. You should see an entry for Python 3.0 (often highlighted in yellow). Select IDLE (Python GUI), and wait a moment for the IDLE program to launch (Figure 1.2).

    The starting screen of the IDLE editor. The first line tells you which version of Python you are using—in this case, it’s version 3.0b1.

    Figure 1.2. The starting screen of the IDLE editor. The first line tells you which version of Python you are using—in this case, it’s version 3.0b1.

  6. Try typing in 24 * 7 and pressing Return. The number 168 should appear.

Installing Python on the Mac

Mac OS X already comes with a version of Python installed, although it is typically not the most up-to-date version and lacks the IDLE editor. To install a more recent version of Python, follow the instructions given at www.python.org/download/mac/. Or, just download and run an installer from www.pythonmac.org/packages/. Be careful to ensure that you have the right version of Python (3.0 or better) and that the Mac OS X version number matches yours.

Installing Python on Linux

If you are using Linux, chances are you already have Python installed. To find out, open a command-line window and type python. If you get something similar to the text shown in Figure 1.2, then you have Python.

Be sure to check the version number: This book covers Python 3. If you have Python 2.x or earlier, then you should install Python 3.

The exact details for doing so will depend upon your Linux system. For example, on Ubuntu Linux, you would search for Python in the Synaptic Package Manager. You can also get Linux installation help from www.python.org/download.

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

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