Search This Blog

Showing posts with label Python Programming. Show all posts
Showing posts with label Python Programming. Show all posts
Wednesday, 28 October 2020

Python Programs for Simple Computation

0 comments

Python Programs using Variables, Expression and Statements

Program 1: Greeting with Hello to the Programmer


Program 2: Performing simple arithmetic such as Addition, Subtraction etc.


Program 3: Finding whether a given number is Odd or Even

Program 4: Finding the Greatest of given three Numbers.

Program 5: Calculating the sum of first N numbers.

Program 6: Generating a Multiplication Table of your choice.


Program 7: Converting a Decimal Number (Base 10) into Binary Number (Base 2)


Continue reading →
Sunday, 10 March 2013

Developing Applications with Python

0 comments
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

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
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

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])

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.

Continue reading →