Shorcuts:


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


(SHIFT + ENTER) will run a cell.

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

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


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

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


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

In [41]:
# help(mList)

In [42]:
# This is a comment

In [ ]:

NUMBERS :


In [43]:
2+1


Out[43]:
3

In [44]:
2+2


Out[44]:
4

In [45]:
3-1


Out[45]:
2

In [46]:
2*5


Out[46]:
10

In [47]:
1/5


Out[47]:
0.2

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


Out[48]:
1.5

In [49]:
3.0/2   # true division


Out[49]:
1.5

In [50]:
3/2.0


Out[50]:
1.5

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


Out[51]:
3.0

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

from __future__ import division

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


Out[53]:
1.5

In [54]:
# power

12**2


Out[54]:
144

In [55]:
# square root

144**0.5


Out[55]:
12.0

In [56]:
a = 5

In [57]:
a


Out[57]:
5

In [58]:
a+a


Out[58]:
10

In [59]:
a = 10

In [60]:
a


Out[60]:
10

In [61]:
a = a+a

In [62]:
a


Out[62]:
20

In [63]:
my_income = 1000000

In [64]:
tax_rate = 0.2

In [65]:
my_taxes = my_income * tax_rate

In [66]:
my_taxes


Out[66]:
200000.0

In [ ]:

STRINGS


In [67]:
'Hello'


Out[67]:
'Hello'

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


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

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


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

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


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

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


Out[71]:
'Hello World 2'

In [72]:
# print statement 

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


Hello World 1
Hwllo World 2

In [73]:
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 [74]:
print 'before TAB \t after TAB'


before TAB 	 after TAB

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


hello World

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

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


how are you

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


Out[78]:
11

In [79]:
s = 'Hello World'

In [80]:
s


Out[80]:
'Hello World'

In [81]:
# indexing
s[0]


Out[81]:
'H'

In [82]:
s[5]


Out[82]:
' '

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


Out[83]:
'ello World'

In [84]:
s[1:7]


Out[84]:
'ello W'

In [85]:
s[:8]


Out[85]:
'Hello Wo'

In [86]:
s[:]


Out[86]:
'Hello World'

In [87]:
s[1:1]


Out[87]:
''

In [88]:
s[-1]


Out[88]:
'd'

In [89]:
s[-2]


Out[89]:
'l'

In [90]:
s[:-1]


Out[90]:
'Hello Worl'

In [91]:
s[:-2]


Out[91]:
'Hello Wor'

In [92]:
s[::1]


Out[92]:
'Hello World'

In [93]:
s[1:5:1]


Out[93]:
'ello'

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


Out[94]:
'el'

In [95]:
s[::2]


Out[95]:
'HloWrd'

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


Out[96]:
'dlroW olleH'

In [97]:
# 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 [98]:
# concatenation

s + "Concatenate me!"


Out[98]:
'Hello WorldConcatenate me!'

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

In [100]:
letter


Out[100]:
'p'

In [101]:
letter * 10


Out[101]:
'pppppppppp'

In [116]:
print letter * 18


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

In [112]:
print(letter * 5)


ppppp

In [113]:
s = 'rustom'

In [114]:
# uppercase

s.upper()


Out[114]:
'RUSTOM'

In [115]:
# lowercase

s.lower()


Out[115]:
'rustom'

In [117]:
# splitting a string

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


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

In [ ]:

PRINT Formatting:


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


This is a string

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


This is a string

In [3]:
m_str = 'String'

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


Place my variable here: String

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


Place my variables here: 13.35

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


Floating point number: 82.17

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


Floating point number: 82.175000000000

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


Floating point number:              82.17

In [8]:
#conversion methods:

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


Conversion to string 15.96

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


first: hi, 
second: rus, 
third: potter

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


First: Hello Potter 
Second: Hello Potter

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


First: Hariom, 
Second: Guruji, 
Third: OmGuru

In [12]:
from __future__ import print_function

In [13]:
print('hello')


hello

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


First: INSERT!!!

In [ ]: