Expressions & Methods of python programming language


Expressions[edit]
  • Addition, subtraction, and multiplication are the same, but the behavior of division differs (see Mathematics for details



  • y value, in contrast to Java, which compares numerics by value[61] and objects by reference.[62] (Value comparisons in Java on objects can be performed with the equals() method.) Python's is operator may be used to compare object identities (comparison by reference). In Python, comparisons may be chained, for example a <= b <= c.
  • Python uses the words andornot for its boolean operators rather than the symbolic &&||! used in Java and C.
  • Python has a type of expression termed a list comprehension. Python 2.4 extended list comprehensions into a more general expression termed a generator expression.[42]
  • Anonymous functions are implemented using lambda expressions; however, these are limited in that the body can only be a single expression.
  • Conditional expressions in Python are written as x if c else y[63] (different in order of operands from the ?: operator common to many other languages).
  • Python makes a distinction between lists and tuples. Lists are written as [1, 2, 3], are mutable, and cannot be used as the keys of dictionaries (dictionary keys must beimmutable in Python). Tuples are written as (1, 2, 3), are immutable and thus can be used as the keys of dictionaries, provided all elements of the tuple are immutable. The parentheses around the tuple are optional in some contexts. Tuples can appear on the left side of an equal sign; hence a statement like x, y = y, x can be used to swap two variables.
  • Python has a "string format" operator %. This functions analogous to printf format strings in C, e.g. "spam=%s eggs=%d" % ("blah", 2) evaluates to "spam=blah eggs=2". In Python 3 and 2.6+, this was supplemented by the format() method of the str class, e.g. "spam={0} eggs={1}".format("blah", 2).
  • Python has various kinds of string literals:
  • Strings delimited by single or double quotation marks. Unlike in Unix shellsPerl and Perl-influenced languages, single quotation marks and double quotation marks function identically. Both kinds of string use the backslash (\) as an escape character and there is no implicit string interpolation such as "$spam".
  • Triple-quoted strings, which begin and end with a series of three single or double quotation marks. They may span multiple lines and function like here documents in shells, Perl and Ruby.
  • Raw string varieties, denoted by prefixing the string literal with an r. No escape sequences are interpreted; hence raw strings are useful where literal backslashes are common, such as regular expressions and Windows-style paths. Compare "@-quoting" in C#.
  • Python has index and slice expressions on lists, denoted as a[key]a[start:stop] or a[start:stop:step]. Indexes are zero-based, and negative indexes are relative to the end. Slices take elements from the start index up to, but not including, the stop index. The third slice parameter, called step or 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.
  • List comprehensions vs. for-loops
  • Conditional expressions vs. if blocks
  • The eval() vs. exec() built-in functions (in Python 2, exec is a statement); the former is for expressions, the latter is for statements.
Methods[edit]

  • Some Python expressions are similar to languages such as C and Java, while some are not:
    In Python, a distinction between expressions and statements is rigidly enforced, in contrast to languages such as Common LispScheme, or Ruby. This leads to some duplication of functionality. For example:
    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 = 1 cannot form part of the conditional expression of a conditional statement. 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 valid C code but if c = 1: ... causes a syntax error in Python.
    Methods on objects are functions attached to the object's class; the syntax instance.method(argument) is, for normal methods and functions, syntactic sugar forClass.method(instance, argument). Python methods have an explicit self parameter to access instance data, in contrast to the implicit self (or this) in some other object-oriented programming languages (e.g. C++JavaObjective-C, or Ruby).[64]). Python also added the ** operator for exponentiation.

  • As of Python 3.5, it supports matrix multiplication directly with the @ operator, as opposed to C and Java which implement these as library functions. Earlier versions of Python also used methods instead of an infix operator.[59][60]
  • In Python, == compares b




Share this

Related Posts

Previous
Next Post »