Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Sage beginners guide - Finch Kraigh copy.pdf
Скачиваний:
13
Добавлен:
10.02.2015
Размер:
5.86 Mб
Скачать

Chapter 3

String Form: <built-in method cos of sage.rings.real_mpfr.RealLiteral object at 0x100478c08>

Namespace: Interactive Definition: real_number.cos(self) Docstring:

Returns the cosine of this number

...

As with classes and functions, typing two question marks after the method name will display its source code.

Learning object-oriented programming for the first time can be confusing, but it will pay off. For now, we've only talked about how to use pre-defined types of objects. In Chapter 9, you will learn how to define your own classes for creating custom objects.

Summary

In this chapter, we learned the basics of interacting with Sage. Specifically, we covered:

How to use the interactive shell

How to use the notebook interface to perform calculations and add documentation to worksheets

Operators and variables Calling functions Making simple plots

Defining our own functions Working with objects

We have only started to unlock the power and flexibility of Sage. In the next chapter, we will learn more about the programming features of Python that are available in Sage.

[ 77 ]

4

Introducing Python and Sage

By now, you have learned the basic principles of interacting with Sage. We will now unlock the power of the Python programming language. The programmatic features of Python complement the mathematical features of Sage.

In this chapter, we shall learn how to:

Use lists and tuples to store sequential data

Iterate with loops

Construct logical tests with "if" statements

Read and write data files

Store heterogeneous data in dictionaries

So, let's start programming…

Python 2 and Python 3

It is important to understand that two stable versions of Python are available. Python 2 was first released in October 2000, and version 2.7 is the latest in a long line of evolutionary upgrades. The Python developers realized that certain aspects of the Python language could not be improved without breaking compatibility with existing code. The result was Python 3, which is the first release of Python that is intentionally backwards-incompatible. In other words, some code written for Python 2 will have to be modified to run on a Python 3 interpreter. For this reason, the migration from Python 2 to Python 3 has been rather slow, even though Python 3 is mature and stable. Version 2.7 is the final release of Python 2, and new features are being added only to Python 3. Sage uses Python 2.7 (as of Sage version 4.6), so the code in this book is written for Python 2.7. Whenever possible, the examples have been written so that they will continue to run when Sage eventually switches to Python 3. When looking at Python documentation and examples online, make sure that you are reading about Python 2, rather than Python 3.

Introducing Python and Sage

WritingcodeforSage

In this chapter, we'll be writing longer blocks of code. While all of the examples can be entered and run using the notebook interface, it is often easier to edit large sections of code with a text editor that is specifically designed for programming. These text editors have special features, such as syntax highlighting and automatic indentation that can help you write code more easily and avoid some common bugs. Unlike word processors or rich-text editors, programmer's text editors save pure text files that do not contain hidden formatting information that can confuse the Python interpreter. Many good text editors are available; the following editors are some popular free and open-source options. GNU Emacs and vim are two popular editors for Linux systems, although Windows, OS X, and Solaris versions are also available. If you are used to the Windows or Mac user interface, it will take some time to get comfortable with the user interface for Emacs or vim. Kate (KDE) and gedit (GNOME) are available for Linux users who prefer a more conventional user interface. Notepad++ is a free, open-source programmer's text editor for Windows, with a familiar installation process and a friendly user interface. TextWrangler is a free text editor for OS X (although it is not open source). jEdit is a Java-based cross-platform editor that will run on any platform that supports Java. To find out more and download the software, use the following links:

http://www.gnu.org/software/emacs/http://www.vim.org/

http://projects.gnome.org/gedit/http://kate-editor.org/

http://notepad-plus-plus.org/

http://www.barebones.com/products/textwrangler/http://www.jedit.org/

Since Sage uses the Python programming language, Sage code follows the same conventions as Python code. These conventions are described in the Style Guide for Python (http:// www.python.org/dev/peps/pep-0008/). It is a good idea to familiarize yourself with these conventions and follow them, so that your code can be easily read by other members of the Sage and Python communities. Since Python uses indentation to denote blocks of code, one of the most important rules is to never mix tabs and spaces. If you use an external text editor, configure the editor so that it inserts four spaces (rather than an invisible tab character) every time you press the Tab key.

[ 80 ]

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]