In [1]:
# strings
my_name = "python"

In [33]:
print my_name,type(my_name)
print dir(my_string)


python <type 'str'>
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

In [34]:
my_num = 1
print my_num,type(my_num)
print dir(my_num)


1 <type 'int'>
['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__', '__delattr__', '__div__', '__divmod__', '__doc__', '__float__', '__floordiv__', '__format__', '__getattribute__', '__getnewargs__', '__hash__', '__hex__', '__index__', '__init__', '__int__', '__invert__', '__long__', '__lshift__', '__mod__', '__mul__', '__neg__', '__new__', '__nonzero__', '__oct__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'bit_length', 'conjugate', 'denominator', 'imag', 'numerator', 'real']

In [35]:
my_num1 = 1.0
print my_num1,type(my_num1)
print dir(my_num1)


1.0 <type 'float'>
['__abs__', '__add__', '__class__', '__coerce__', '__delattr__', '__div__', '__divmod__', '__doc__', '__eq__', '__float__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getformat__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__int__', '__le__', '__long__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__nonzero__', '__pos__', '__pow__', '__radd__', '__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rmod__', '__rmul__', '__rpow__', '__rsub__', '__rtruediv__', '__setattr__', '__setformat__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', 'as_integer_ratio', 'conjugate', 'fromhex', 'hex', 'imag', 'is_integer', 'real']

In [5]:
# playing with strings

In [6]:
my_string='python'
print my_string,type(my_string)


python <type 'str'>

In [ ]:
# string of sequence of characters
# p  y  t  h  o  n
# 0  1  2  3  4  5  # +ve indexing or left to right.
# -6 -5 -4 -3 -2 -1 # -ve indexing or right to left.

In [9]:
# indexing
print my_string[3]
print my_string[-3]
print my_string[5],my_string[-1]


h
h
n n

In [23]:
# slicing
# print my_string[start:end:step]
print my_string[0:3]  # 0 till 3
print my_string[:3]  # start till 3
print my_string[3:6]  # 3 till 6
print my_string[3:]   # 3 till end
print my_string[:]    # start to end
print my_string[::1]
print my_string[::2]
print my_string[1::2]
print my_string[::-1]
# hw: to play with the start,end and step.

# concatination
print my_string[:3]  + my_string[3:]


pyt
pyt
hon
hon
python
python
pto
yhn
nohtyp
python

In [16]:
# you cannot modify the element of the sequence
print my_string[3]
my_string[3]="H"


h
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-16-2663c6b46277> in <module>()
      1 # you cannot modify the element of the sequence
      2 print my_string[3]
----> 3 my_string[3]="H"

TypeError: 'str' object does not support item assignment

In [18]:
# task1
my_string="python"
# output : Tython
print "T" + my_string[1:]
print my_string[2].upper() + my_string[1:]


Tython
Tython

In [31]:
# playing with numbers.

print 1 + 1
print '1' + '1'
print 25/2   # quotient
print 25 % 2 # reminder
print 25 + 25/2
print (25 + 25)/2
print 25/2.0

# float,int,str
print float(25)
print float(25)/2
# BODMAS


2
11
12
1
37
25
12.5
25.0
12.5

In [32]:
import math
print dir(math)


['__doc__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']

In [ ]:
# printing

In [36]:
my_school = "De Paul"
another_school = "st. xaviers"
town = "township"
beach = "blue"
commute = "bus"

In [37]:
# primitive
print "my school name is my_school"
print 'my school name is my_school'


my school name is my_school
my school name is my_school

In [39]:
print "my school name is ", my_school , 'We have another school' , another_school


my school name is  De Paul We have another school st. xaviers

In [40]:
# typecasting - %d,%s,%f,%r
print "my school name is %s.We have another school %s" %(my_school,another_school)


my school name is De Paul.We have another school st. xaviers

In [48]:
print "my school name is %s.I love my school %s" %(my_school,my_school)


my school name is De Paul.I love my school De Paul

In [46]:
# format
# positinal based
print "my school name is {}.I love my school {}".format(my_school,another_school)
# indexed based
print "my school name is {0}.I love my school {0}".format(my_school,another_school)
# key based
print "my school name is {ms}.I love my school {ms}".format(ms=my_school,ans=another_school)
# type casting
print "my school name is {!s}.I love my school {!s}".format(my_school,another_school)


my school name is De Paul.I love my school st. xaviers
my school name is De Paul.I love my school De Paul
my school name is De Paul.I love my school De Paul
my school name is De Paul.I love my school st. xaviers

In [49]:
# docstring
#"""  """
#'''  '''
print """
Hey today is tuesday.
Hey i am going to office today.
Lets hope today is relaxed day.

"""


Hey today is tuesday.
Hey i am going to office today.
Lets hope today is relaxed day.



In [1]:
# raw_input and input
# 2.x -> raw_input,input
# 3.x -> input

# raw_input - takes string as default input operator.
name = raw_input("please enter your name:?")
print name,type(name)


please enter your name:?kumar
kumar <type 'str'>

In [2]:
num = raw_input("please enter the number:?")
print num,type(num)


please enter the number:?10
10 <type 'str'>

In [3]:
# function - typecasted - int,float,str
num = int(raw_input("please enter the number:?"))
print num,type(num)


please enter the number:?10
10 <type 'int'>

In [4]:
num = int(raw_input("please enter the number:?"))
print num,type(num)


please enter the number:?a
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-4-7fadd6fc1150> in <module>()
----> 1 num = int(raw_input("please enter the number:?"))
      2 print num,type(num)

ValueError: invalid literal for int() with base 10: 'a'

In [1]:
# input
name = input("please enter your name:")
print name,type(name)


please enter your name:"kumar"
kumar <type 'str'>

In [2]:
num = input("please enter your num:")
print num,type(num)


please enter your num:10
10 <type 'int'>

In [ ]: