Play with OPERATORS

Operators are special symbols that represent computations like addition and multiplication.

10+20   # addition
10-20   # substraction
10*20   # multiplication
20/10   # division
10%8    # mod
20**10  # exponent

Order of operations :  Girls first priority :-)

1. Parentheses have the highest precedence, expressions in parentheses are evaluated first.
2. Exponentiation has the next highest precedence.
3. Multiplication and Division have the same precedence, which is higher than Addition and Subtraction
4. Operators with the same precedence are evaluated from left to right.

Table of precedence

**                      Exponentiation (raise to the power)
~ + -                   Ccomplement, unary plus and minus (method names for the last two are +@ and -@)
* / % //                Multiply, divide, modulo and floor division
+ -                     Addition and subtraction
>> <<                   Right and left bitwise shift
&                       Bitwise 'AND'
^ |                     Bitwise exclusive `OR' and regular `OR'
<= < > >=               Comparison operators
<> == !=                Equality operators
= %= /= //= -= += *= **=Assignment operators
is is not               Identity operators
in not in               Membership operators
not or and              Logical operators


Comparison operators :  Mostly used in Conditional statements and also while evaluating some expressions

==    equality check, if yes then condition becomes true.
!=    opposite to ==
<>    simillar to !=
>     checks if left operand is greater than the value of right operand, if yes then condition becomes true.
<     opposite to >
>=    checks if left operand is greater or equal than the value of right operand, if yes then condition becomes true.

Assignment operators : used to assign values to variables.

=       assignment operator
+=      Add AND assignment operator  # a = a +10 can be written as  --> a += 10
-=      simillarly  a = a - 10 can be written as a -= 10
*=
/=
%=

Bit wise operator : Used to do bit wise operation

Assume if a = 25; and b = 8;


Binary representaion will be like


a = 0001 1001


b = 0000 1000


a&b = 0000 1000    ---> equals to 8  AND operation


a|b = 0001 1001    ---> equals to 25  OR


a^b = 0001 0001    ---> equals to 17  XOR


~a  = 1001 0001    ---> equals to -26


Want to see integer in terms of bits ????

bin()   -->   Is function helps you to see numbers in binary form

>>> bin(10)
'0b1010'



Input and outputs :

print "hello"          # discussed hell lot of time :)   This is the way we will output strings on console

So how to provide input ? think ??????

OK.. let me tell you there are two ways 
1) Command line arguments.
2) Interactive inputs.

Command line Arguments are used primarily to pass arguments(input) to your code when you start your program, for example

#python addition.py  2 6    # for this program 2 and 6 are inputs

import sys                       # sys is the package which lets you to access command line variables
print sys.argv[0]              # Points to addition.py (file name)
print sys.argv[1]              # Points to  2
print sys.argv[2]              # Points to  6

By default sys.argv returns string values.

print sys.argv   
>>> ['addtion.py', '2', '6']

So sys.argv is a list of strings...    (if you want to add "2" and "6"  you need to convert then to integers using int() )

If you try to access value that is not present in sys.argv you will get error "IndexError: list index out of range"
BE CAREFULL  (I ll tell you how to handle error in future posts)

What will you do if you want to give inputs at run time ???
You cant use sys.argv

Use function input( )

1)   >>> x = input( )         # waits untill user gives input and hit Enter key
       1213
       >>> x
       1213
       >>> type( x )
       <type 'int'>            # Type of data enterd will be int if integer entered/ string if string is enterd


2)   >>> s = input( )
       "hai"
       >>> s
       'hai'
       >>> type( s )
       <type 'str'>

raw_input( )                     # return string always
        >>> x = raw_input( )
        2342
        >>> x
        2342
        >>> type( x )

The list is a most use full datatype which we use in Python, basically its comma-separated values (items) in between square brackets.
Advantage of using python List is it can hold any datatype

z = [[1,2,3] , [6,7,8] , [5,6,7]]                                  #  List of List
y = [1,3,4,6]                                                            #  list of integers
x = [ 12, "string", 9.314, {"key" : value}]                   # list of mixed data type
t =  [{"key1":123 , "key2":34 , "key3": "string"}]        # list of dictionaries

Items in list are accessed using index.

print z[0]
>>> [1,2,3]

print z[0][1]
>>>1

print t[0]["key1"]
>>> 123