In [ ]:
# you dont need typecasting of the varibles.
# dont use keywords as variables.
In [1]:
my_string="python"
In [2]:
print type(my_string)
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]
In [5]:
# string are immutable . you cannot modify them.
my_string[2] = 'T'
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]
In [10]:
# concatination
print my_string[:3] + my_string[3:]
In [ ]:
# task1
# my_string="python"
# output: Tython
In [12]:
print 'T' + my_string[1:]
print my_string[2].upper() + my_string[1:]
In [ ]:
# numbers
In [21]:
print 1 + 1
In [22]:
print 2 ** 3
In [23]:
print 3 - 2
In [24]:
print 5 / 2 # quotient
In [25]:
print 5 % 2 # reminder
In [26]:
print 5/2.0 #
In [30]:
# float,int,str
print float(5)
print int(5.0)
print str(2)
In [27]:
print float(5)/2
In [31]:
print '1' + '1'
In [32]:
print '1' + 1
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
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
In [7]:
# typecasting.
print "my school name is %s.We used to have another school %s." %(my_school,another_school)
In [8]:
print "my school is %s. I love my %s" %(my_school,my_school)
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)
In [ ]:
# %r(__repr__) and %s(__str__)
In [16]:
weeks = "\nsun\nmon\ntue\nwed\nthu\nfri\nsat"
print "Days of a week are: %s" %(weeks)
In [17]:
print "Days of a week are: %r" %(weeks)
In [19]:
# input and raw_input
name = raw_input("please enter your name:")
print "your name is {}".format(name)
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)
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)
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)
In [ ]: