background preloader

Python

Facebook Twitter

The Python Tutorial. Python is an easy to learn, powerful programming language.

The Python Tutorial

It has efficient high-level data structures and a simple but effective approach to object-oriented programming. Python’s elegant syntax and dynamic typing, together with its interpreted nature, make it an ideal language for scripting and rapid application development in many areas on most platforms. The Python interpreter and the extensive standard library are freely available in source or binary form for all major platforms from the Python Web site, and may be freely distributed.

The same site also contains distributions of and pointers to many free third party Python modules, programs and tools, and additional documentation. The Python interpreter is easily extended with new functions and data types implemented in C or C++ (or other languages callable from C). This tutorial introduces the reader informally to the basic concepts and features of the Python language and system. Overview. Sage. Google Noticias. Chapter 16: Inheritance. Warning: the HTML version of this document is generated from Latex and may contain translation errors.

Chapter 16: Inheritance

In particular, some mathematical expressions are not translated correctly. 16.1 Inheritance The language feature most often associated with object-oriented programming is inheritance. Inheritance is the ability to define a new class that is a modified version of an existing class. The primary advantage of this feature is that you can add new methods to a class without modifying the existing class. Inheritance is a powerful feature. On the other hand, inheritance can make programs difficult to read. In this chapter we will demonstrate the use of inheritance as part of a program that plays the card game Old Maid. 16.2 A hand of cards For almost any card game, we need to represent a hand of cards. Chapter 14: Classes and methods. Warning: the HTML version of this document is generated from Latex and may contain translation errors.

Chapter 14: Classes and methods

In particular, some mathematical expressions are not translated correctly. 14.1 Object-oriented features Python is an object-oriented programming language, which means that it provides features that support object-oriented programming. It is not easy to define object-oriented programming, but we have already seen some of its characteristics: Programs are made up of object definitions and function definitions, and most of the computation is expressed in terms of operations on objects.Each object definition corresponds to some object or concept in the real world, and the functions that operate on that object correspond to the ways real-world objects interact.

For example, the Time class defined in Chapter 13 corresponds to the way people record the time of day, and the functions we defined correspond to the kinds of things people do with times. 14.2 printTime class Time: pass Or with Points: Chapter 12: Classes and objects. Warning: the HTML version of this document is generated from Latex and may contain translation errors.

Chapter 12: Classes and objects

In particular, some mathematical expressions are not translated correctly. 12.1 User-defined compound types Having used some of Python's built-in types, we are ready to create a user-defined type: the Point. Consider the concept of a mathematical point. In two dimensions, a point is two numbers (coordinates) that are treated collectively as a single object. A natural way to represent a point in Python is with two floating-point values. An alternative is to define a new user-defined compound type, also called a class.

A class definition looks like this: class Point: pass Class definitions can appear anywhere in a program, but they are usually near the beginning (after the import statements). This definition creates a new class called Point. By creating the Point class, we created a new type, also called Point. Blank = Point() Classes. Compared with other programming languages, Python’s class mechanism adds classes with a minimum of new syntax and semantics.

Classes

It is a mixture of the class mechanisms found in C++ and Modula-3. Python classes provide all the standard features of Object Oriented Programming: the class inheritance mechanism allows multiple base classes, a derived class can override any methods of its base class or classes, and a method can call the method of a base class with the same name. Objects can contain arbitrary amounts and kinds of data. As is true for modules, classes partake of the dynamic nature of Python: they are created at runtime, and can be modified further after creation.

In C++ terminology, normally class members (including the data members) are public (except see below Private Variables and Class-local References), and all member functions are virtual. (Lacking universally accepted terminology to talk about classes, I will make occasional use of Smalltalk and C++ terms.