print "Hello World" #printing a statement
print "Hello World" #printing a statement
msg="C++ cout object"
sex='M'
age=24
number=420.5
print sex,
print " ", age, " ", number
print msg
print '%d%d%d' %(1,2,3)
print number+1
print 99.99
name=[None]*25 #char name[25]
address=[None]*25 #char address[25]
name=raw_input("Enter name: ") #take input from user
age=int(raw_input("Enter Age: "))
address=raw_input("Enter address: ")
print "The data entered are: "
print "Name =", name
print "Age =", age
print "Address =", address
principle=int(raw_input("Enter Principle Amount: "))
time=int(raw_input("Enter time (in years): "))
rate=int(raw_input("Enter Rate of Interest: "))
SimpInt=(principle*time*rate)/100
print "Simple Interest =", SimpInt
total= principle + SimpInt
print "Total Amount =", total
PI=3.1452
radius=float(raw_input("Enter Radius of Circle: "))
area=PI*radius*radius
print "Area of Circle =", area
def display(msg): #pass by reference
print msg
msg="Misuse"
return msg
string=[None]*15
string="Hello World"
string=display(string)
print string
def display(msg): #pass by value
print msg
string=[None]*15
string="Hello World"
display(string)
print string
num=20
def main():
global num
x=num
num=10
print "Local =", num
print "Global =",x
print "Global+Local =", x+num
main()
counter=50
def main():
global counter
x=counter
for counter in range(1, 10):
print x/counter
main()
for i in range(5):
print i
i+=1;
print i
a=10
def main():
global a
global_a=a
print global_a
a=20
def temp():
a=30
print a
print global_a
temp()
print a
print global_a
main()
a=z=1
b=2
c=3
print "a=",a, "b=", b, "c=", c, "z=", z
a=z=b
print "a=",a, "b=", b, "c=", c, "z=", z
a=z=c
print "a=",a, "b=", b, "c=", c, "z=", z
print "&a=", hex(id(a)),"&b=", hex(id(b)) , "&c=", hex(id(c))
from ctypes import c_int, pointer
n=c_int(100)
p=pointer(n)
m=p[0]
print "n =", n.value, "m =", m, "*p =", p[0]
k=c_int(100)
p=pointer(k)
k.value=200
print "n =", n.value, "m =", m, "*p =", p[0]
def Max(a, b):
if(a>b):
return a
else:
return b
x, y=[int(x) for x in raw_input("Enter two integers: ").split()] #takes input in a single line separated by white space
print "Maximum =", Max(x,y)
def swap (x, y): #pass by reference
i=x
x=y
y=i
return x, y
a, b=[int(x) for x in raw_input("Enter two integers <a, b>: ").split()]
(a,b)=swap(a, b)
print "On swapping <a, b>:", a, b
def square(x):
x=x*x
return x
num=float(raw_input('Enter a number <float>: '))
print 'Its square =', square(num)
def show_integer(val):
print "Integer: ", val
def show_double(val):
print "Double: ", val
def show_string(val):
print "String: ", val
show_integer(420)
show_double(3.1415)
show_string("Hello World\n!")
def show(val): #function overloading
if (isinstance(val, int)):
print "Integer: ", val
if (isinstance(val, float)):
print "Double: ", val
if(isinstance(val, str)):
print "String: ", val
show(420)
show(3.1415)
show("Hello World\n!")
def showstring(string="Hello World!"): #default arguments
print string
showstring("Here is an explicit argument")
showstring()
import sys
def PrintLine(ch='-', RepeatCount=70): #default arguments
print "\n"
for i in range(RepeatCount):
sys.stdout.write(ch)
PrintLine()
PrintLine('!')
PrintLine('*', 40)
PrintLine('R', 55)
import sys
def PrintLine(ch='-', RepeatCount=70, nLines=1): #default arguments
for j in range(nLines):
print "\n"
for i in range(RepeatCount):
sys.stdout.write(ch)
PrintLine()
PrintLine('!')
PrintLine('*', 40)
PrintLine('R', 55)
PrintLine('&', 25, 2)
from collections import namedtuple
struct_date = namedtuple('struct_date', 'day month year')
d1 = struct_date(26, 3, 1958)
d2 = struct_date(14, 4, 1971)
d3 = struct_date(1, 9, 1973)
print "Birth Date of the First Author:",
print "%s-%s-%s" %(d1.day, d1.month, d1.year)
print "Birth Date of the Second Author:",
print "%s-%s-%s" %(d2.day, d2.month, d2.year)
print "Birth Date of the Third Author:",
print "%s-%s-%s" %(d3.day, d3.month, d3.year)
from ctypes import Structure, c_int
class date(Structure):
_fields_=[("day", c_int),("month", c_int),("year",c_int)]
def show(self):
print "%s-%s-%s" %(self.day, self.month, self.year)
d1=date(26, 3, 1958)
d2 = date(14, 4, 1971)
d3 = date(1, 9, 1973)
print "Birth Date of the First Author:",
d1.show()
print "Birth Date of the Second Author:",
d2.show()
print "Birth Date of the Third Author:",
d3.show()
a=int
b=420.5
print "int(10.4) =", int(10.4)
print "int(10.99) =", int(10.99)
print "b =", b
a=int(b)
print "a = int(b) =", a
b=float(a)+1.5
print "b = float(a)+1.5 =", b
def swap(x, y):
t=x
x=y
y=t
return x, y
ch1, ch2=raw_input("Enter two Characters <ch1, ch2>: ").split()
ch1, ch2=swap(ch1, ch2)
print "On swapping <ch1, ch2>:", ch1, ch2
a, b=[int(x) for x in raw_input("Enter integers <a, b>: ").split()]
a, b=swap(a, b)
print "On swapping <a, b>:", a, b
c, d=[float(x) for x in raw_input("Enter floats <c, d>: ").split()]
c, d=swap(c, d)
print "On swapping <c, d>:", c, d
def swap(x, y):
t=x
x=y
y=t
return x, y
ch1, ch2=raw_input("Enter two Characters <ch1, ch2>: ").split()
ch1, ch2=swap(ch1, ch2)
print "On swapping <ch1, ch2>:", ch1, ch2
a, b=[int(x) for x in raw_input("Enter integers <a, b>: ").split()]
a, b=swap(a, b)
print "On swapping <a, b>:", a, b
c, d=[float(x) for x in raw_input("Enter floats <c, d>: ").split()]
c, d=swap(c, d)
print "On swapping <c, d>:", c, d
def AddVectors(a, b, c, size):
for i in range(size):
c[i]=a[i]+b[i]
def ReadVector(vector, size):
for i in range(size):
vector[i]=int(raw_input())
def ShowVector(vector, size):
for i in range(size):
print vector[i],
vec_size=int(raw_input("Enter size of vector: "))
x=[int]*vec_size
y=[int]*vec_size
z=[int]*vec_size
print "Enter Elements of vector x: "
ReadVector(x, vec_size)
print "Enter Elements of vector y: "
ReadVector(y, vec_size)
AddVectors(x, y, z, vec_size)
print "Summation Vector z=a+b:",
ShowVector(z, vec_size)
del x
del y
del z
def area(s1, s2=None):#function overloading and default parameters
if (isinstance(s1, int)):
if(isinstance(s2, int)):
return (s1*s2)
else:
return (s1*s1)
elif (isinstance(s1, float)):
return (3.14*s1*s1)
s=int(raw_input("Enter the side length of the square: "))
l, b=[int(x) for x in raw_input("Enter the length and breadth of the rectangle: ").split()]
r=float(raw_input("Enter the radius of the circle: "))
print "Area of square = ", area(s)
print "Area of rectangle = ", area(l, b)
print "Area of circle = ", area(r)