In [1]:
print "Hello"
print "(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!!"
In [0]:
print "(SHIFT + TAB) will show help popup for docstring."
In [0]:
# help(mList)
In [0]:
# This is a comment
In [0]:
In [0]:
2+1
Out[0]:
In [0]:
2+2
Out[0]:
In [0]:
3-1
Out[0]:
In [0]:
2*5
Out[0]:
In [0]:
1/5
Out[0]:
In [0]:
3/2 # classic integer division
Out[0]:
In [0]:
3.0/2 # true division
Out[0]:
In [0]:
3/2.0
Out[0]:
In [0]:
float(3) #casting:
Out[0]:
In [0]:
#importing capabilities from python3 into python2
from __future__ import division
In [0]:
3/2 #(due to __future__)
Out[0]:
In [0]:
# power
12**2
Out[0]:
In [0]:
# square root
144**0.5
Out[0]:
In [0]:
a = 5
In [0]:
a
Out[0]:
In [0]:
a+a
Out[0]:
In [0]:
a = 10
In [0]:
a
Out[0]:
In [0]:
a = a+a
In [0]:
a
Out[0]:
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]:
In [0]:
In [0]:
'Hello'
Out[0]:
In [0]:
'This is also a string'
Out[0]:
In [0]:
'I'm a string # ERROR
In [0]:
"I'm a string" #OK
Out[0]:
In [0]:
'Hello World 1'
'Hello World 2'
Out[0]:
In [0]:
# print statement
print 'Hello World 1'
print 'Hwllo World 2'
In [0]:
print 'Here is a new line \n and here is a 2nd line'
In [0]:
print 'before TAB \t after TAB'
In [0]:
print("hello World")
In [0]:
# To use print function from Python3 in Python2
from __future__ import print_function
In [0]:
print ('how are you')
In [0]:
len('Hello World')
Out[0]:
In [0]:
s = 'Hello World'
In [0]:
s
Out[0]:
In [0]:
# indexing
s[0]
Out[0]:
In [0]:
s[5]
Out[0]:
In [0]:
# slicing
s[1:]
Out[0]:
In [0]:
s[1:7]
Out[0]:
In [0]:
s[:8]
Out[0]:
In [0]:
s[:]
Out[0]:
In [0]:
s[1:1]
Out[0]:
In [0]:
s[-1]
Out[0]:
In [0]:
s[-2]
Out[0]:
In [0]:
s[:-1]
Out[0]:
In [0]:
s[:-2]
Out[0]:
In [0]:
s[::1]
Out[0]:
In [0]:
s[1:5:1]
Out[0]:
In [0]:
s[1:5:2]
Out[0]:
In [0]:
s[::2]
Out[0]:
In [0]:
s[::-1] # reversing the string backwards
Out[0]:
In [0]:
# immutability
s[2] = 'P' # cannot assign to a particlar element in a string sequence
In [0]:
# concatenation
s + "Concatenate me!"
Out[0]:
In [0]:
# multiplication
letter = 'p'
In [0]:
letter
Out[0]:
In [0]:
letter * 10
Out[0]:
In [0]:
print letter * 18
In [0]:
print(letter * 5)
In [0]:
s = 'rustom'
In [0]:
# uppercase
s.upper()
Out[0]:
In [0]:
# lowercase
s.lower()
Out[0]:
In [0]:
# splitting a string
s.split('o') # splitting around 'o'
Out[0]:
In [0]:
In [0]:
print 'This is a string'
In [0]:
print('This is a string')
In [0]:
m_str = 'String'
print 'Place my variable here: %s' %(m_str)
In [0]:
x = 13.35
print 'Place my variables here: %s' %(x)
In [0]:
print 'Floating point number: %1.2f' %(82.175)
In [0]:
print 'Floating point number: %1.12f' %(82.175)
In [0]:
print 'Floating point number: %18.2f' %(82.175)
In [0]:
#conversion methods:
print 'Conversion to string %r' %(15.96)
In [0]:
print 'first: %s, \nsecond: %s, \nthird: %s' %('hi', 'rus', 'potter')
In [0]:
print 'First: {x} \nSecond: {x}'.format(x = "Hello Potter")
In [0]:
print 'First: {x}, \nSecond: {y}, \nThird: {z}'.format(x = "Hariom", y = "Guruji", z = 'OmGuru')
In [0]:
from __future__ import print_function
In [0]:
print('hello')
In [0]:
print('First: {x}'.format(x = 'INSERT!!!'))
In [0]: