In [ ]:
# you dont need typecasting of the varibles.
# dont use keywords as variables.

In [1]:
my_string="python"

In [2]:
print type(my_string)


<type 'str'>

In [ ]:
# string is a sequence of characters.

In [ ]:
# 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 [4]:
# indexing

print my_string[2]
print my_string[-4]


t
t

In [5]:
# string are immutable . you cannot modify them.
my_string[2] = 'T'


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-5-44d5caadc1e9> in <module>()
      1 # string are immutable . you cannot modify them.
----> 2 my_string[2] = 'T'

TypeError: 'str' object does not support item assignment

In [20]:
# slicing
# pyt =>i want from 0 till 3 (3 not included.)

# 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..

print my_string[0:3] # pyt
print my_string[:3]
print my_string[3:6] # hon
print my_string[3:]
print my_string[-3:]
print my_string[-5:2]
# slicing
print my_string[:] # beginning to end
print my_string[0:6]
print my_string[0:6:1]  # [start:end:step]
print my_string[0:6:2]  # P y T h O n
print my_string[1:6:2]  # p Y t H o N
print my_string[::-1]


pyt
pyt
hon
hon
hon
y
python
python
python
pto
yhn
nohtyp

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


python

In [ ]:
# task1
# my_string="python"
# output: Tython

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


Tython
Tython

In [ ]:
# numbers

In [21]:
print 1 + 1


2

In [22]:
print 2 ** 3


8

In [23]:
print 3 - 2


1

In [24]:
print 5 / 2 # quotient


2

In [25]:
print 5 % 2  # reminder


1

In [26]:
print 5/2.0  #


2.5

In [30]:
# float,int,str
print float(5)
print int(5.0)
print str(2)


5.0
5
2

In [27]:
print float(5)/2


2.5

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


11

In [32]:
print '1' + 1


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-32-2929f3af6d0a> in <module>()
----> 1 print '1' + 1

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

In [ ]:
# fun with python challenges
# http://www.pythonchallenge.com/

In [ ]:
# Homework
# create a string called my_string="python"
# go via all object ex: my_string.<tab>
# find out one more solution for task1.

In [33]:
# work with module called as math
# please browse over these function.
import math

In [ ]:
# printing

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

In [4]:
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 [5]:
print 'my school name is', my_school , "We used to have another school " , another_school , 'We used to live in a small', town , ' we have a beach which is', beach , 'in color', "we used to commute to our home in ", commute


my school name is De Paul We used to have another school  st. xaviers We used to live in a small township  we have a beach which is blue in color we used to commute to our home in  bus

In [7]:
# typecasting.
print "my school name is %s.We used to have another school %s." %(my_school,another_school)


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

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


my school is De Paul. I love my De Paul

In [14]:
# format
print "my school is {}.I love my {}".format(my_school,my_school)
# indexing
print "my school is {1}.I love my {1}".format(my_school,another_school)
# key-value pairing
print "my school is {ms}.I love my {ms}".format(ms=my_school,ans=another_school)
# typecasting
print "my school is {!s}.I love my {!s}".format(my_school,my_school)


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

In [ ]:
# %r(__repr__) and %s(__str__)

In [16]:
weeks = "\nsun\nmon\ntue\nwed\nthu\nfri\nsat"
print "Days of a week are: %s" %(weeks)


Days of a week are: 
sun
mon
tue
wed
thu
fri
sat

In [17]:
print "Days of a week are: %r" %(weeks)


Days of a week are: '\nsun\nmon\ntue\nwed\nthu\nfri\nsat'

In [19]:
# input and raw_input
name = raw_input("please enter your name:")
print "your name is {}".format(name)


please enter your name:kumar
your name is kumar

In [21]:
num1 = raw_input("please enter the num1:")
num2 = raw_input("please enter the num2:")
result = num1 +  num2
print "sum on {} and {} is {}".format(num1,num2,result)


please enter the num1:10
please enter the num2:10
sum on 10 and 10 is 1010

In [23]:
num1 = int(raw_input("please enter the num1:"))
num2 = int(raw_input("please enter the num2:"))
result = num1 +  num2
print "sum on {} and {} is {}".format(num1,num2,result)


please enter the num1:a
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-23-3ca8f458a514> 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 "sum on {} and {} is {}".format(num1,num2,result)

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

In [24]:
# input - 2.x and 3.x
# if your provide int it takes integer.
# if you provide str it takes string.
# 3.x -> we dont have raw_input

num1 = input("please enter the num1:")
num2 = input("please enter the num2:")
result = num1 +  num2
print "sum on {} and {} is {}".format(num1,num2,result)


please enter the num1:10
please enter the num2:10
sum on 10 and 10 is 20

In [ ]: