Chapter 02 : Managing Input and Output Operators

Example 1, Page No 4.56

In [1]:
#Testing Characters

#Variable Declaration

#User Input
ch=raw_input("Give any Character as a input: ")

if (ch.isalpha()>0):          #Checking whether the character is alphabet
    print ch,"is a alphabet"
elif(ch.isdigit()>0):         #Checking whether the character is digit
    print ch,"is a digit"
else:                          #else print alphanumeric
    print ch,"is a alphanumeric"
Give any Character as a input: a
a is a alphabet

Example 2, Page No 4.57

In [2]:
#Convert Uppercase to Lowercase and Viceversa

#Variable Declaration


#User Input
ch= raw_input("Give the input in lower case or upper case: ")

if(ch.islower()):      #Converting lowercase if input is uppercase
    print ch.upper(), "is a upper case character"
else:                   #else print lowercase
    print ch.lower(), "is a lower case character"
Give the input in lower case or upper case: s
S is a upper case character

Example 3, Page No 4.58

In [1]:
#Program using input function


scientist = raw_input("Enter Name: ")
print "Print the name: ", scientist
Enter Name: Abdul kalam
Print the name:  Abdul kalam

Example 4, Page No 4.62

In [3]:
#Count numbers of entered numbers

#Variable declaration

#getting input from user     
j=int(raw_input("Give input for j "))
k=int(raw_input("Give input for k "))
i=int(raw_input("Give input for i "))

i= i, j, k       #assigning the user input in to a variable

print "No. of given input values:",  len(i)      #counting length of the given input
print "The Value of j and k is ", j, k              #printing the values of input
Give input for j 56
Give input for k 32
Give input for i 10
No. of given input values: 3
The Value of j and k is  56 32

Example 5, Page No 4.65

In [4]:
#Print a statement using print

print "VRB Publishers"     #Printing statement
VRB Publishers

Example 6, Page No 4.65

In [5]:
#Adding two numbers

#variable declaration

c= a+b      #Adding a, b and stores in c 

print "Addition of two numbers is : ", c
Addition of two numbers is :  14

Example 7, Page No 4.66

In [6]:
#Reading and Displaying Details of a student

#Variable Declaration


#User Inputs
sno=int(raw_input("Give Student serial no. "))
name=raw_input("Student Name : ")
m1=int(raw_input("Student Mark 1: "))
m2=int(raw_input("Student Mark 2: "))
m3=int(raw_input("Student Mark 3: "))

total=float(m1+m2+m3)        #Calculation of marks

avg="%.2f"%float(total/3.0)           #Average

print '\n'"Student serial No.:", sno
print "Student Name:", name
print "Student's Marks:", m1, m2, m3
print "Total:", total
print "Average:", avg 
Give Student serial no. 1
Student Name : Raja
Student Mark 1: 90
Student Mark 2: 95
Student Mark 3: 98

Studnt serial No.: 1
Student Name: Raja
Student's Marks: 90 95 98
Total: 283.0
Average: 94.33

Example 8, Page No:4.71

In [7]:
#Display quotient and reminder of given input

#variable declaration


#User input
a=int(raw_input("Give input for a: "))
b=int(raw_input("Give input for b: "))

q=float(a/b)              #Calculation for Quotient

r=float(a-q*b)         #Calculation for Reminder

print "Quotient of a and b is :",q
print "Reminder of a and b is :",r
Give input for a: 39
Give input for b: 7
Quotient of a and b is : 5.0
Reminder of a and b is : 4.0

Example 9, Page No:4.73

In [8]:
#Slope and Midpoint of a line

#Variable declaration


#User input1

x1=int(raw_input("Give point X1: "))
y1=int(raw_input("Give point Y1: "))

#User input2

x2=int(raw_input('\n'"Give point X2: "))
y2=int(raw_input("Give point Y2: "))

slope =(y2-y1)/(x2-x1)            #Calculation for Slope

#Calculation for Midpoints

print '\n' "Slope of a line is :", slope
print "Midpoint of a line is :", x,y
Give point X1: 10
Give point Y1: 20

Give point X2: 30
Give point Y2: 40

Slope of a line is : 1
Midpoint of a line is : 20.0 30.0

Example 10, Page No:4.73

In [9]:
#Average of Given N Numbers

#Variable Declaration


n=int(raw_input("Give total number of input: "))

print "Give those",n ,"Numbers"

for i in range(n):          #Looping values of n
    x=input()               #Values for n
    total+=x                 # Adding given numbers
avg=float(total/n)           #Calculating an average of given numbers

print "Sum of given numbers is ", total
print "Average is ", avg
Give total number of input: 5
Give those 5 Numbers
Sum of given numbers is  112
Average is  22.0

Example 11, Page No:4.75

In [10]:
#Temperature Conversion (Fahrenheit to Celsius)

#Variable Declaration

F_MIN =0
fh= F_MIN

print  "Fahrenheit" '\t' "Celsius"
    cel ="%.2f"%float((fh-(32.0))/1.8)     #Calculating Celsius value
    print  fh,'\t''\t', cel
    fh=fh+INC              #Calculating Fahrenheit value
Fahrenheit	Celsius
0 		-17.78
50 		10.00
100 		37.78
150 		65.56
200 		93.33
250 		121.11
300 		148.89
350 		176.67
400 		204.44
450 		232.22
500 		260.00

Example 12, Page No:4.76

In [11]:
#Area of Triangle

#Variable Declaration


#User input

a=int(raw_input("First side of Triangle is "))
b=int(raw_input("Second side of Triangle is "))
c=int(raw_input("Third side of Triangle is "))

s=(a+b+c)/2.0      #Calculating Size of triangle

area="%.3f"%float((s*(s-a)*(s-b)*(s-c)) ** 0.5)    #Calculating Area of triangle

print '\n',"Area of Triangle is",area
First side of Triangle is 10
Second side of Triangle is 10
Third side of Triangle is 10

Area of Triangle is 43.301