4
 Python Facts Python2 is backward compatible; whereas Python3 is backward INCOMPATIBLE. Python supports OOPs concepts. But, in python, both private and protected modes are having same authority. This is the only limitation in python becoming complete OOP language. Python interpreter generates byte-code, so the python applications are platform-independent. Python runs faster than java, but slower than C language. Python was written in C language. Pre/Post increment/decrement like x++,x--,--x,++x, are INVALID in python Compound operations such as +=,-=,*=,/= are VALID in Python Python is case-sensitive i.e., variable “ work” is different from variable “Work”, or “WORKUsing underscores (eg:price_at_opening ) and Camel-casing (eg:PriceAtOpening)is VALID Multiple Assignments are VALID in python. Eg: x=y=z=1 Parallel assignment (or unpacking) is POSSIBLE in python Eg: a,b=1,2 (x,y,z)=1,2,3 In python 2, Print is a statement. In python 3, print is a function. To use print() function of python 3 in python2, place this statement in the first lines of script: from __future__ import print_functi on = assignment operator == Operator to check value equivalence is Operator to check identity of objects; looks at object level

Python Facts

Embed Size (px)

DESCRIPTION

Few facts about python programming language

Citation preview

  • Python Facts

    Python2 is backward compatible; whereas Python3 is backward

    INCOMPATIBLE.

    Python supports OOPs concepts. But, in python, both private

    and protected modes are having same authority. This is the

    only limitation in python becoming complete OOP language.

    Python interpreter generates byte-code, so the python

    applications are platform-independent.

    Python runs faster than java, but slower than C language.

    Python was written in C language.

    Pre/Post increment/decrement like x++,x--,--x,++x, are INVALID

    in python

    Compound operations such as +=,-=,*=,/= are VALID in Python

    Python is case-sensitive i.e., variable work is different

    from variable Work, or WORK

    Using underscores (eg:price_at_opening) and Camel-casing

    (eg:PriceAtOpening)is VALID

    Multiple Assignments are VALID in python.

    Eg: x=y=z=1

    Parallel assignment (or unpacking) is POSSIBLE in python

    Eg: a,b=1,2

    (x,y,z)=1,2,3

    In python 2, Print is a statement. In python 3, print is a

    function.

    To use print() function of python 3 in python2, place this

    statement in the first lines of script:

    from __future__ import print_function

    = assignment operator

    == Operator to check value equivalence

    is Operator to check identity of objects; looks at object

    level

  • in Operator to check identity of objects; looks at index

    level

    != not-equal-to operator

    not-equal-to operator. Available only in python2.

    # comment operator in python; called as

    octothorpe/pound/hash/mesh

    >>> Input operator, shows readiness of interpreter. Called as

    chevron

    ^ bit-wise operator. Not power operator. Called as caret

    ; used to separate TWO or more statements in the same line.

    or docstrings. Used for multiple-line

    comment in python scripts. Differs from using #. Appears

    in the output. Useful, especially, in modules.

    #! Called shebang. Used as the first character in python

    script.

    \ line-continuation operator. Used when a statement spans

    more than one line.

    Eg: print Udhay\

    Prakash

    () used for tuples

    [] used for lists

    {} used for dictionaries

    ([]) used for sets

    Switch-case condition is not defined in python. Instead, If-

    elif-else ladder can be used. Also, dictionary of

    functions can be used.

    Eg:

    >>> result={

    ... 'a':lambda x :x*5,

    ... 'b':lambda x:x+7,

    ... 'c':lambda x:x-2}

    >>> result['b'](10)

    17

  • Indentation is both boon and bane (for beginners) for python.

    Use spaces instead of Tabs in scripts.

    Eg:

    >>> a=2

    >>> a

    2

    >>> a=2

    Traceback ( File "", line 1

    a=2

    ^

    IndentationError: unexpected indent

    Running python with t option prints warning messages when

    tabs and spaces are mixed inconsistently within the same

    program block.

    Running python with tt option turns these warning messages in

    to TabError exception.

    raw_input() takes any input as string type.

    input() takes any input type dynamically.

    &&,|| are defined operators in python

    and boolean operator for AND operation

    or boolean operator for OR operation

    not Boolean operator for NOT operation

    set operators

    | union operator for sets

    & intersection operator for sets

    - Set difference operator

    Eg: c=t-s# set c contains items in set t, but not in set s

    ^ Symmetric difference operator for sets

    Eg: d=t^s # set d contains items in set t or set s, but

    not common items in both.

  • bitwise Operators

    & bit-wise AND operator

    | bit-wise OR operator

    ^ bit-wise XOR operator

    ~ bit-wise invert operator. Eg: ~5 results in -6

    Bit-wise invert of x is (x+1)

    >> left shift operator

    Eg: 15>>1

    7

    Explanation: 15-1111 (in 8421 convention)

    15>>1 means shifting one position left.

    It becomes 0111 i.e., 7