Sunteți pe pagina 1din 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,
applications are platform-independent.

so

the

python

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)
(eg:PriceAtOpening)is VALID

and

Camel-casing

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
octothorpe/pound/hash/mesh

python;

called

as

>>> 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.
script.

line-continuation operator. Used when a statement spans


more than one line.

Used

as

the

first

character

in

python

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, Ifelif-else ladder can be used. Also, dictionary of
functions can be used.
Eg:
>>>
...
...
...
>>>
17

result={
'a':lambda x :x*5,
'b':lambda x:x+7,
'c':lambda x:x-2}
result['b'](10)

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 "<interactive input>", 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
right shift operator
power of operator
division operator
floor division operator (only in python3)
modulo operator. Returns remainder of a division.

PEP8 suggests (not mandatory) for a white space around any


operator.
It is suggested to define default parameters in the last, for
functions.

S-ar putea să vă placă și