Writing Programs using Python
1. Write a simple
program in python for finding the greatest of three numbers.
Solution:
#! /usr/bin/python
# program to find greatest among three numbers
no1 = raw_input("Enter the First Number ")
no2 = raw_input("Enter the Second Number ")
no3 = raw_input("Enter the Third Number ")
great = no3
if no1 > no2 > no3:
great = no1
elif no2 > no3:
great = no2
print "The Greatest Number is : "+great
# program to find greatest among three numbers
no1 = raw_input("Enter the First Number ")
no2 = raw_input("Enter the Second Number ")
no3 = raw_input("Enter the Third Number ")
great = no3
if no1 > no2 > no3:
great = no1
elif no2 > no3:
great = no2
print "The Greatest Number is : "+great
2. Write a python
program for generating Fibonacci series and finding the factorial of a
number using looping statements.
Solution:
#! /usr/bin/python
# Program to
demonstrate for loop and range
# Program to find
the factorial of a number and generate Fibonacci series
a,b = 0, 1
print " The
first 10 fibonacci numbers are "
for i in range(10):
c = a + b
print c
a = b
b = c
fact = 1
for i in range(1,5):
fact = fact * i
fact = fact * i
print "Factorial
of 5 is ", fact
3. Define a function
max_of_three()
that takes three numbers as arguments and returns the largest of them. Use it for finding the largest of three numbers.
Solution:
#! /usr/bin/python
# program to find greatest among three numbers
no1 = raw_input("Enter the First Number ")
no2 = raw_input("Enter the Second Number ")
no3 = raw_input("Enter the Third Number ")
great = no3
if no1 > no2 > no3:
great = no1
elif no2 > no3:
great = no2
print "The Greatest Number is : "+great
# program to find greatest among three numbers
no1 = raw_input("Enter the First Number ")
no2 = raw_input("Enter the Second Number ")
no3 = raw_input("Enter the Third Number ")
great = no3
if no1 > no2 > no3:
great = no1
elif no2 > no3:
great = no2
print "The Greatest Number is : "+great
4. Write a function that takes a character (i.e. a string of length 1) and returns
True
if it is a vowel, False
otherwise.
Solution:
def vowel(c):
vow = ['a','e','i','o','u']
if c in vow:
print c+"is a Vowel"
else:
print c+"is a Consonant"
s = raw_input("Enter a Character:")
vowel(s[0])
def vowel(c):
vow = ['a','e','i','o','u']
if c in vow:
print c+"is a Vowel"
else:
print c+"is a Consonant"
s = raw_input("Enter a Character:")
vowel(s[0])
5. Define a procedure
histogram()
that takes a list of integers and prints a histogram to the screen. For example, the input 4, 9, 7
should print the following:*********
*******
Solution:
def histogram(iList):
for item in iList:
print "*"*item
t = []
n = int(raw_input("Enter the number lines in the Histogram:"))
for i in range(0,n):
print "Enter the value for Row ",i+1
m = int(raw_input())
t.append(m)
histogram(t)
You can refer the material available in this link for learning more about Python.