Shorcuts:


In [1]:
print "Hello"
print "(SHIFT + ENTER) will run a cell."


Hello
(SHIFT + ENTER) will run a cell.

In [0]:
mList = [1,2,3]

In [0]:
print "TAB will show a popup of the methods on an object!!"


TAB will show a popup of the methods on an object!!

In [0]:
print "(SHIFT + TAB) will show help popup for docstring."


(SHIFT + TAB) will show help popup for docstring.

In [0]:
# help(mList)

In [0]:
# This is a comment

In [0]:

NUMBERS :


In [0]:
2+1


Out[0]:
3

In [0]:
2+2


Out[0]:
4

In [0]:
3-1


Out[0]:
2

In [0]:
2*5


Out[0]:
10

In [0]:
1/5


Out[0]:
0.2

In [0]:
3/2     # classic integer division


Out[0]:
1.5

In [0]:
3.0/2   # true division


Out[0]:
1.5

In [0]:
3/2.0


Out[0]:
1.5

In [0]:
float(3)        #casting:


Out[0]:
3.0

In [0]:
#importing capabilities from python3 into python2

from __future__ import division

In [0]:
3/2         #(due to __future__)


Out[0]:
1.5

In [0]:
# power

12**2


Out[0]:
144

In [0]:
# square root

144**0.5


Out[0]:
12.0

In [0]:
a = 5

In [0]:
a


Out[0]:
5

In [0]:
a+a


Out[0]:
10

In [0]:
a = 10

In [0]:
a


Out[0]:
10

In [0]:
a = a+a

In [0]:
a


Out[0]:
20

In [0]:
my_income = 1000000

In [0]:
tax_rate = 0.2

In [0]:
my_taxes = my_income * tax_rate

In [0]:
my_taxes


Out[0]:
200000.0

In [0]:

STRINGS


In [0]:
'Hello'


Out[0]:
'Hello'

In [0]:
'This is also a string'


Out[0]:
'This is also a string'

In [0]:
'I'm a string    # ERROR


  File "<ipython-input-69-2d82d9e81731>", line 1
    'I'm a string    # ERROR
       ^
SyntaxError: invalid syntax

In [0]:
"I'm a string"    #OK


Out[0]:
"I'm a string"

In [0]:
'Hello World 1'
'Hello World 2'


Out[0]:
'Hello World 2'

In [0]:
# print statement 

print 'Hello World 1'
print 'Hwllo World 2'


Hello World 1
Hwllo World 2

In [0]:
print 'Here is a new line \n and here is a 2nd line'


Here is a new line 
 and here is a 2nd line

In [0]:
print 'before TAB \t after TAB'


before TAB 	 after TAB

In [0]:
print("hello World")


hello World

In [0]:
# To use print function from Python3 in Python2
from __future__ import print_function

In [0]:
print ('how are you')


how are you

In [0]:
len('Hello World')


Out[0]:
11

In [0]:
s = 'Hello World'

In [0]:
s


Out[0]:
'Hello World'

In [0]:
# indexing
s[0]


Out[0]:
'H'

In [0]:
s[5]


Out[0]:
' '

In [0]:
# slicing
s[1:]


Out[0]:
'ello World'

In [0]:
s[1:7]


Out[0]:
'ello W'

In [0]:
s[:8]


Out[0]:
'Hello Wo'

In [0]:
s[:]


Out[0]:
'Hello World'

In [0]:
s[1:1]


Out[0]:
''

In [0]:
s[-1]


Out[0]:
'd'

In [0]:
s[-2]


Out[0]:
'l'

In [0]:
s[:-1]


Out[0]:
'Hello Worl'

In [0]:
s[:-2]


Out[0]:
'Hello Wor'

In [0]:
s[::1]


Out[0]:
'Hello World'

In [0]:
s[1:5:1]


Out[0]:
'ello'

In [0]:
s[1:5:2]


Out[0]:
'el'

In [0]:
s[::2]


Out[0]:
'HloWrd'

In [0]:
s[::-1]          # reversing the string backwards


Out[0]:
'dlroW olleH'

In [0]:
# immutability

s[2] = 'P'        # cannot assign to a particlar element in a string sequence


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-97-eae392253637> in <module>()
      1 # immutability
      2 
----> 3 s[2] = 'P'        # cannot assign to a particlar element in a string sequence

TypeError: 'str' object does not support item assignment

In [0]:
# concatenation

s + "Concatenate me!"


Out[0]:
'Hello WorldConcatenate me!'

In [0]:
# multiplication
letter = 'p'

In [0]:
letter


Out[0]:
'p'

In [0]:
letter * 10


Out[0]:
'pppppppppp'

In [0]:
print letter * 18


  File "<ipython-input-116-5d56031a985b>", line 1
    print letter * 18
               ^
SyntaxError: invalid syntax

In [0]:
print(letter * 5)


ppppp

In [0]:
s = 'rustom'

In [0]:
# uppercase

s.upper()


Out[0]:
'RUSTOM'

In [0]:
# lowercase

s.lower()


Out[0]:
'rustom'

In [0]:
# splitting a string

s.split('o')         # splitting around 'o'


Out[0]:
['rust', 'm']

In [0]:

PRINT Formatting:


In [0]:
print 'This is a string'


This is a string

In [0]:
print('This is a string')


This is a string

In [0]:
m_str = 'String'

print 'Place my variable here: %s' %(m_str)


Place my variable here: String

In [0]:
x = 13.35
print 'Place my variables here: %s' %(x)


Place my variables here: 13.35

In [0]:
print 'Floating point number: %1.2f' %(82.175)


Floating point number: 82.17

In [0]:
print 'Floating point number: %1.12f' %(82.175)


Floating point number: 82.175000000000

In [0]:
print 'Floating point number: %18.2f' %(82.175)


Floating point number:              82.17

In [0]:
#conversion methods:

print 'Conversion to string %r' %(15.96)


Conversion to string 15.96

In [0]:
print 'first: %s, \nsecond: %s, \nthird: %s' %('hi', 'rus', 'potter')


first: hi, 
second: rus, 
third: potter

In [0]:
print 'First: {x} \nSecond: {x}'.format(x = "Hello Potter")


First: Hello Potter 
Second: Hello Potter

In [0]:
print 'First: {x}, \nSecond: {y}, \nThird: {z}'.format(x = "Hariom", y = "Guruji", z = 'OmGuru')


First: Hariom, 
Second: Guruji, 
Third: OmGuru

In [0]:
from __future__ import print_function

In [0]:
print('hello')


hello

In [0]:
print('First: {x}'.format(x = 'INSERT!!!'))


First: INSERT!!!

In [0]: