In [1]:
# variables

my_string="python"
print my_string
print type(my_string)


python
<type 'str'>

In [2]:
my_number = 1
print my_number
print type(my_number)


1
<type 'int'>

In [3]:
my_number = 1.0
print my_number
print type(my_number)


1.0
<type 'float'>

In [4]:
# playing with string
# string is sequence of characters.
my_string="python"
# 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 [7]:
# indexing
print my_string[3]
print my_string[-3]
print my_string[0]


h
h
p

In [10]:
# slicing
# pyt => 0 till 3 where 3 is not included.
print my_string[0:3]
print my_string[:3]
print my_string[3:6]
print my_string[3:]


pyt
pyt
hon
hon

In [25]:
print my_string
print my_string[0:6]
print my_string[:]
print my_string[0:6:1]
print my_string[0:6:2]
print my_string[1:6:2]
print my_string[::-1]  # (i=10;i<=1;i--)


python
python
python
python
pto
yhn
nohtyp

In [12]:
# you cannot modify the characters within the string
print my_string[0]
my_string[0]="T"


p
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-12-a62911739f70> in <module>()
      1 # you cannot modify the characters within the string
      2 print my_string[0]
----> 3 my_string[0]="T"

TypeError: 'str' object does not support item assignment

In [13]:
# string support concatination
print my_string[:3] + my_string[3:]


python

In [16]:
print '1' + '1'
print 1 + 1
print '1' + 1


11
2
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-16-53f70c83666a> in <module>()
      1 print '1' + '1'
      2 print 1 + 1
----> 3 print '1' + 1

TypeError: cannot concatenate 'str' and 'int' objects

In [18]:
# task
my_string="python"
# output => "Tython"
print 'T' + my_string[1:]
print my_string[2].capitalize() + my_string[1:]


Tython
Tython

In [26]:
# playing with numbers
# BODMAS

In [27]:
print 25 * 25


625

In [28]:
print 4 / 2  # quotient


2

In [29]:
print 4 % 2 # reminder


0

In [30]:
print 25 + 25 / 2 # 37,37.5


37

In [31]:
print (25 + 25) / 2


25

In [32]:
print 25/2.0


12.5

In [33]:
print float(25)/2


12.5

In [34]:
# printing

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

In [37]:
print "my school name is my_school"
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
my school name is De Paul

In [38]:
print 'my school name is' , my_school , "We had another school adjancent to us", another_school


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

In [39]:
# type casting
# %d - digit,%s - string,%f - float
print "my school name is %s.We had another school adjacent to us %s" %(my_school,another_school)


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

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


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

In [ ]:
# format

In [3]:
print "my school name is {}.I love my {}".format(my_school,another_school)


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

In [5]:
# format - indexing
print "my school name is {1}.I love my {1}".format(my_school,another_school)


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

In [6]:
# format - keys
print "my school name is {ms}.I love my {ms}".format(ms=my_school,ans=another_school)


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

In [8]:
# foramt - typecasting
print "my school name is {!s}.\n I love my {!s}".format(my_school,another_school)


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

In [8]:
# doc strings
print """
Today is monday.
We call it a blud day.
Hopefully nothing is wired out.
"""


Today is monday.
We call it a blud day.
Hopefully nothing is wired out.


In [42]:
# Home work
my_string="python"
print my_string.capitalize()


Python

In [43]:
a = 5L
print type(a)


<type 'long'>

In [ ]:
# input and raw_input

In [10]:
name = raw_input("what your name ?")
print name


what your name ?kumar
kumar

In [ ]:
print type(name)

In [1]:
number = raw_input("please enter your roll number?")
print number
print type(number)


please enter your roll number?10
10
<type 'str'>

In [3]:
number = int(raw_input("please enter your roll number?"))
print number
print type(number)


please enter your roll number?ten
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-3-d2fcf270a0b2> in <module>()
----> 1 number = int(raw_input("please enter your roll number?"))
      2 print number
      3 print type(number)

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

In [4]:
# input

name = input("please enter your name:")


please enter your name:"kumar"

In [6]:
print name
print type(name)


kumar
<type 'str'>

In [7]:
number = input("please enter a number")
print type(number)
print number


please enter a number10
<type 'int'>
10

In [ ]: