Indexes are zero based and negative indexes are relative to the end Slices take

Indexes are zero based and negative indexes are

This preview shows page 9 - 12 out of 34 pages.

Indexes are zero-based, and negative indexes are relative to the end. Slices take elements from the startindex up to, but not including, the stopindex. The third slice parameter, called stepor stride, allows elements to be skipped and reversed. Slice indexes may be omitted, for example a[:]returns a copy of the entire list. Each element of a slice is a shallow copy.
Background image
In Python, a distinction between expressions and statements is rigidly enforced, in contrast to languages such as Common Lisp, Scheme, or Ruby. This leads to duplicating some functionality. For example:List comprehensionsvs. for-loopsConditionalexpressions vs. ifblocksThe eval()vs. exec()built-in functions (in Python 2, execis a statement); the former is for expressions, the latter is for statements.Statements cannot be a part of an expression, so list and other comprehensions or lambda expressions, all being expressions, cannot contain statements. A particular case of this is that an assignment statement such as a = 1cannot form part of the conditional expression of a conditionalstatement. This has the advantage of avoiding a classic C error of mistaking an assignment operator =for an equality operator ==in conditions: if (c = 1) { ... }is syntactically valid (but probably unintended) C code but if c = 1: ...causes a syntax error in Python.Methods[edit]Methodson objects are functionsattached to the object's class; the syntax instance.method(argument)is, for normal methods and functions, syntactic sugarfor Class.method(instance, argument). Python methods have an explicit selfparameter to access instance data, in contrast to the implicit self(or this) in some other object-oriented programming languages (e.g., C++, Java, Objective-C, or Ruby).[75]Typing[edit]Python uses duck typingand has typed objects but untyped variable names. Type constraints are not checked at compile time; rather, operations on an object may fail, signifying that the given object is not of a suitable type. Despite being dynamically typed, Python is strongly typed, forbidding
Background image
operations that are not well-defined (for example, adding a number to a string) rather than silently attempting to make sense of them.Python allows programmers to define their own types using classes, which are most often used for object-oriented programming. New instancesof classes are constructed by calling the class (for example, SpamClass()or EggsClass()), and the classes are instances of the metaclasstype(itself an instance of itself), allowing metaprogrammingand reflection.Before version 3.0, Python had two kinds of classes: old-styleand new-style.[76]The syntax of both styles is the same, the difference being whether the class objectis inherited from, directly or indirectly (all new-style classes inherit from objectand are instances of type). In versions of Python 2 from Python 2.2 onwards, both kinds of classes can be used. Old-style classes were eliminated in Python 3.0.
Background image
Image of page 12

You've reached the end of your free preview.

Want to read all 34 pages?

  • Fall '12
  • DuiWei

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture