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)
In [2]:
print my_string.capitalize?
In [3]:
print help(my_string.capitalize)
In [8]:
# integer variable
my_num = 1
print my_num,type(my_num)
print dir(my_num)
In [9]:
# float variable
my_num1 = 1.0
print my_num1,type(my_num1)
print dir(my_num1)
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]
In [13]:
my_string[0] = 'T'
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:]
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.
In [23]:
# concatination in string
print '1' + '1'
# muliplication of string
print '* ' * 10
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)
In [26]:
print 'T' + my_string[1:]
print my_string[2].upper() + my_string[1:]
In [27]:
print help(my_string.replace)
In [29]:
print my_string.replace('p','T')
print my_string
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
In [ ]:
# http://www.pythonchallenge.com/
# https://projecteuler.net/archives
In [41]:
# module math
import math
In [43]:
print dir(math)
print help(math.pow)
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'
In [47]:
print "my school name is " , my_school , 'we had another school ',another_school
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)
In [50]:
print "my school is %s.I love my %s" %(my_school,my_school)
In [51]:
# format
# simulating the previous example
print "my school name is {}.I love my {}".format(my_school,my_school)
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)
In [ ]:
# input and raw_input
In [1]:
name = raw_input("please enter your name:")
In [3]:
print name,type(name)
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)
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)
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)
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)
In [ ]: