In [ ]:
# python
# type casting is not there in python

In [7]:
my_string="python"  # my_string='python'
print my_string
print type(my_string)
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 [2]:
print my_string.capitalize?

In [3]:
print help(my_string.capitalize)


Help on built-in function capitalize:

capitalize(...)
    S.capitalize() -> string
    
    Return a copy of the string S with only its first character
    capitalized.

None

In [8]:
# integer variable
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 [9]:
# float variable
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 [ ]:
# playing with strings.
# string is a 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 [12]:
# indexing
print my_string
print my_string[2]
print my_string[-4]


python
t
t

In [13]:
my_string[0] = 'T'


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-13-4d54b92f6a1a> in <module>()
----> 1 my_string[0] = 'T'

TypeError: 'str' object does not support item assignment

In [15]:
# slicing
print my_string[0:3] # 0 till 3 , 3 not included.
print my_string[:3]
print my_string[3:6]
print my_string[3:]


pyt
pyt
hon
hon

In [21]:
# slicing with step
print my_string[0:6]
print my_string[:]
#ex: print my_string[start:end:step]
print my_string[0:6:1]
print my_string[0:6:2]
print my_string[1:6:2]
# negative indexing
print my_string[::-1]  # reverse of a string.


python
python
python
pto
yhn
nohtyp

In [23]:
# concatination in string
print '1' + '1'

# muliplication of string
print '* ' * 10


11
* * * * * * * * * * 

In [24]:
# task1
print my_string
# output - 'Tython'

# task2
# homework
# my_string="homework"
# my_string.<tab> or dir(my_string)
# my_string.upper? or help(my_string.upper)


python

In [26]:
print 'T' + my_string[1:]
print my_string[2].upper() + my_string[1:]


Tython
Tython

In [27]:
print help(my_string.replace)


Help on built-in function replace:

replace(...)
    S.replace(old, new[, count]) -> string
    
    Return a copy of string S with all occurrences of substring
    old replaced by new.  If the optional argument count is
    given, only the first count occurrences are replaced.

None

In [29]:
print my_string.replace('p','T')
print my_string


Tython
python

In [39]:
# playing with numbers
# BODMAS
print 1 + 1
print 2 * 2
print 3 ** 2
print 5 / 2  # quotient => 2.5 => 2
print 5 / 2.0
print float(5) / 2
print 5 % 2 # reminder => 1
print 5 + 5 / 2
print (5 + 5) / 2
print 2 ** 38


2
4
9
2
2.5
2.5
1
7
5
274877906944

In [ ]:
# http://www.pythonchallenge.com/
# https://projecteuler.net/archives

In [41]:
# module math
import math

In [43]:
print dir(math)
print help(math.pow)


['__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']
Help on built-in function pow in module math:

pow(...)
    pow(x, y)
    
    Return x**y (x to the power of y).

None

In [ ]:
# printing

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

In [45]:
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 [47]:
print "my school name is " , my_school , 'we had another school ',another_school


my school name is  De Paul we had another school  st. xaviers

In [49]:
# typecasting

# %s -> string
# %f -> float
# %d -> integer
# %r -> raw

print "my school name is %s.We had another school %s" %(another_school,my_school)


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

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


my school is De Paul.I love my De Paul

In [51]:
# format
# simulating the previous example
print "my school name is {}.I love my {}".format(my_school,my_school)


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

In [56]:
# indexed based printing
print "my school name is {0}.I love my {0}".format(my_school,another_school)
# key based printing
print "my school name is {ms}.I love my {ms}".format(ms=my_school,ans=another_school)
# typecasting based prigning
print "my school name is {!s}.I love my {!s}".format(my_school,another_school)


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

In [ ]:
# input and raw_input

In [1]:
name = raw_input("please enter your name:")


please enter your name:kumar

In [3]:
print name,type(name)


kumar <type 'str'>

In [4]:
num1 = raw_input("please enter the num1:")
num2 = raw_input("please enter the num2:")
result = num1 + num2
print "result of operation is {}".format(result)


please enter the num1:11
please enter the num2:11
result of operation is 1111

In [5]:
num1 = int(raw_input("please enter the num1:"))
num2 = int(raw_input("please enter the num2:"))
result = num1 + num2
print "result of operation is {}".format(result)


please enter the num1:11
please enter the num2:11
result of operation is 22

In [6]:
num1 = int(raw_input("please enter the num1:"))
num2 = int(raw_input("please enter the num2:"))
result = num1 + num2
print "result of operation is {}".format(result)


please enter the num1:a
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-6-2a08e521199b> in <module>()
----> 1 num1 = int(raw_input("please enter the num1:"))
      2 num2 = int(raw_input("please enter the num2:"))
      3 result = num1 + num2
      4 print "result of operation is {}".format(result)

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

In [7]:
# input - 2.x and 3.x
num1 = input("please enter the num1:")
num2 = input("please enter the num2:")
result = num1 + num2
print "result of operation is {}".format(result)


please enter the num1:11
please enter the num2:11
result of operation is 22

In [ ]: