Chapter 03: Input/Output Functions and Statements

Example 1 , Page number: CP-39

In [1]:
#program to add and find product of two numbers

#variable declaration
a = 5
b = 3

# calculation of sum and product 

summ = a + b
product = a * b

print a,b
print summ,product
5 3
8 15

Example 2 , Page number: CP-45

In [2]:
# Program to convert degree fahrenheit to celcius 
# variable declaration

f = 105.00
print "Degree fahrenheit ? %d" % f
# calculation of degree in celcius 

c = 5.0/9.0 * (f-32)

# result 
print
print "Degree centigrade =%6.2f" % c
Degree fahrenheit ? 105

Degree centigrade = 40.56

Example 3 , Page number: CP-45

In [4]:
# To find area of a triangle
# variable declaration

a = 5
b = 4
c = 6


# calculation of area
s = float((a+b+c))/2
area = (s*(s-a)*(s-b)*(s-c)) ** 0.5

# result
print "Enter three sides : %d" % a,b,c
print 
print "Area of triangle = %0.2f Sq.units" % area
Enter three sides : 5 4 6

Area of triangle = 9.92 Sq.units

Example 4 , Page number: CP-51

In [5]:
# To print ASCII value of a given character 
# Variable declaration

ch = "A"
print "Enter a character : " , ch

# Calculation of ASCII value of a character

print 
print "ASCII value of " + ch + " is" ,ord(ch)
print "Press any key to stop. . ."
Enter a character :  A

ASCII value of A is 65
Press any key to stop. . .

Example 5 , Page number: CP-51

In [6]:
# To print electricity for consumers
# Variable declaration

sno = "TMR65358"
pmr = 4305
cmr = 4410

print "Enter service number :" ,sno
print "Previous meter reading ?",pmr
print "Current meter reading ?",cmr

# Calculation of electricity charges

units = cmr - pmr
amt = units * 1.50

# Result

print
print "         Electricity Bill"
print "         ----------------"
print "Service No :",sno
print "Unit Consumed :",units
print "Electricity Charges : Rs.%0.2f" % amt
Enter service number : TMR65358
Previous meter reading ? 4305
Current meter reading ? 4410

         Electricity Bill
         ----------------
Service No : TMR65358
Unit Consumed : 105
Electricity Charges : Rs.157.50

Example 6 , Page number: CP-52

In [1]:
# Program to swap value of two variables
# Variable declaration

a = 15
b = 250 

print "Enter value to A :",a
print "Enter value to B :",b

# Swapping

temp = a
a = b
b = temp

print 
print "Value of A =",a
print "Value of B =",b
Enter value to A : 15
Enter value to B : 250

Value of A = 250
Value of B = 15