Www.eced.com



UNIT I

1. python program to find minimum in a list

def smallest( list ):

min = list[ 0 ]

for a in list:

if a < min:

min = a

return min

numbers=list(input("Enter the list of numbers"))

print(smallest(numbers))

Output:

Enter the list of numbers2,0,-8,1

-8

2. python program to insert a card in a list of sorted cards

def insertionSort(arr):

         for i in range(1, len(arr)):

         key = arr[i]

         j = i-1

         while j >=0 and key < arr[j] :

                arr[j+1] = arr[j]

                j -= 1

         arr[j+1] = key

 

arr = list(input("Enter the list of elements"))

insertionSort(arr)

print ("Sorted array is:")

for i in range(len(arr)):

    print (arr[i])

Output:

Enter the list of elements4,2,0,-7

Sorted array is:

-7

0

2

4

3. python program to guess an integer number in a range

import random

hidden = random.randrange(1, 200)

guess = int(raw_input("Please enter your guess: "))

if guess == hidden:

print "Guessed correctly!"

elif guess < hidden:

print "Your guess is too low"

else:

print "Your guess is too high"

Output:

please enter your guess: 154

Your guess is too high

4. python program for Towers of Hanoi.

disks = 3

def hanoi(n, from_tower, to_tower, temp):

if n > 0:

hanoi(n-1, from_tower, temp, to_tower)

print('move disk from ', from_tower, ' to ', to_tower)

hanoi(n-1, temp, to_tower, from_tower)

hanoi(disks, 'A','C','B')

Output:

move disk from A to C

move disk from A to B

move disk from C to B

move disk from A to C

move disk from B to A

move disk from B to C

move disk from A to C

UNIT II

1. python program to exchange the values of two variables

a=int(input("Enter value of first variable: "))

b=int(input("Enter value of second variable: "))

a=a+b

b=a-b

a=a-b

print("\nafter swapping\na=", a, " b=", b)

(OR)

a=int(input("Enter value of first variable: "))

b=int(input("Enter value of second variable: "))

 

print("before swapping\na=", a, " b=", b)

 

temp = a

a = b

b = temp

 

print("\nafter swapping\na=", a, " b=", b)

Output:

Enter value of first variable: 24

Enter value of second variable: 56

after swapping

a= 56 b= 24

2. python program to circulate the values of n variables

a=list(input("enter the list"))

print(a)

for i in range(1,len(a),1):

print(a[i:]+a[:i])

Output:

enter the list1,2,3

[1, 2, 3]

[2, 3, 1]

[3, 1, 2]

3. python program to find the distance between two points

import math

print("enter the coordinates for point A")

x1=int(input("Enter x1"))

y1=int(input("enter y1"))

print("enter the coordinates for point B")

x2=int(input("enter x2"))

y2=int(input("enter y2"))

x_temp=(x2-x1)**2

y_temp=(y2-y1)**2

distance=math.sqrt(x_temp+y_temp)

print("distance between two point:",distance)

Output:

enter the coordinates for point A

Enter x1 4

enter y1 0

enter the coordinates for point B

enter x2 6

enter y2 6

distance between two point: 6.324555320336759

UNIT III

1. python program to find the gcd.

x=int(input("Enter X Value"))

y=int(input("Enter Y value"))

rem=x%y

while rem!=0:

x=y

y=rem

rem=x%y

print("GCD of given numbers is:",y)

OUTPUT:

Enter X Value54

Enter Y value24

GCD of given numbers is: 6

2. python program for finding the square root of a number.

def newtonSqrt(n, times):

approx = 0.5 * n

for i in range(times):

approx = 0.5 * (approx + n/approx)

return approx

print(newtonSqrt(25, 3))

print(newtonSqrt(25, 5))

print(newtonSqrt(25, 10))

Output:

5.01139410653

5.00000000002

5.0

3.python program to find the exponential value of a number.

def power(base,exp):

if(exp==1):

return(base)

if(exp!=1):

return(base*power(base,exp-1))

base=int(input("Enter base: "))

exp=int(input("Enter exponential value: "))

print("Result:",power(base,exp))

OUTPUT:

Enter base: 2

Enter exponential value: 3

Result: 8

4.python program for linear search.

list=[4,1,2,5,3]

search=int(input("Enter search number"))

for i in range(0,len(list)):

if search==list[i]:

print(search,"found at position",i)

break

else:

print("Not Found")

OUTPUT:

Enter search number3

3found at position4

Enter search number8

Not Found

5. python program for binary search.

def binarySearch (list, left, right, x):

if right >= left:

mid = (left + right) //2

if list[mid] == x:

return mid

elif list[mid] > x:

return binarySearch(list, left, mid-1, x)

else:

return binarySearch(list, mid+1, right, x)

else:

return -1

list = [ 2, 3, 4, 10, 40 ]

x = int(input("Enter the search element"))

result = binarySearch(list, 0, len(list)-1, x)

if result != -1:

print ("Element is present at index ", result)

else:

print ("Element is not present in array")

OUTPUT:

Enter the search element40

Element is present at index 4

Enter the search element50

Element is not present in array

6.python program for sum of an array of numbers.

def sum(arr):

total=0

for i in arr:

total=total+i

return total

arr=list(input("Enter the array of numbers"))

tot=sum(arr)

print("sum of array=",tot)

Output:

Enter the array of numbers 4,5,6

sum of array=15)

UNIT IV

1.python program to sort a set of numbers using selection sort.

def selectionsort( a ):

for i in range( len( a ) ):

min = i

for k in range( i + 1 , len( a ) ):

if a[k] < a[min]:

min = k

tmp = a[min]; a[min] = a[i]; a[i] = tmp

a=list(input("Enter the list of numbers"))

selectionsort(a)

print("sorted list:")

for i in a:

print(i)

Output:

Enter the list of numbers56,78,-23,1,0,89,100,6

sorted list:

-23

0

1

6

56

78

89

100

2.python program to sort a set of numbers using insertion sort.

def insertionSort(arr):

for i in range(1, len(arr)):

key = arr[i]

j = i-1

while j >=0 and key < arr[j] :

arr[j+1] = arr[j]

j =j-1

arr[j+1] = key

arr = list(input("Enter the list of elements"))

insertionSort(arr)

print ("Sorted array is:")

for i in arr:

print (i)

Output:

Enter the list of elements56,0,-8,12,3

Sorted array is:

-8

0

3

12

56

3.python program to sort the set of numbers using merge sort.

def mergeSort(nlist):

if len(nlist)>1:

mid = len(nlist)//2

left = nlist[:mid]

right = nlist[mid:]

mergeSort(left)

mergeSort(right)

i=j=k=0

while i < len(left) and j < len(right):

if left[i] < right[j]:

nlist[k]=left[i]

i=i+1

else:

nlist[k]=right[j]

j=j+1

k=k+1

while i < len(left):

nlist[k]=left[i]

i=i+1

k=k+1

while j < len(right):

nlist[k]=right[j]

j=j+1

k=k+1

nlist =list(input("Enter the list of numbers"))

mergeSort(nlist)

print(nlist)

Output:

Enter the list of numbers45,14,37,23,78,56,0,-10

[-10, 0, 14, 23, 37, 45, 56, 78]

4.python program to create a histogram from a list of integers.

def histogram( items ):

for n in items:

output = ''

times = n

while( times > 0 ):

output += '*'

times = times - 1

print(output)

histogram([2, 3, 6, 5])

output:

**

***

******

*****

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download