Week-3 Python Lab Programs
Week-3 Python Lab Programs
1.
I) Write a program to convert a list and tuple into arrays
import numpy as np
#Declare List Elements
my_list = []
list_length = int(input("Enter No of List Elements:" ))
for i in range(list_length):
lst = int(input('Enter Element: '))
my_list.append(lst)
#Display List Elements
print("\n List Elements are: ",end=' ')
for i in my_list:
print(i,end=' ')
#Convert and Display Elements from List to Array
array1=np.asarray(my_list)
print("\n Convert After list to array is : ",array1)
Program-1
# Python code to demonstrate naive
# method to compute gcd ( recursion )
def gcd(a, b):
if(b == 0):
return a
else:
return gcd(b, a % b)
# Driver program to test above function
m = int(input("Enter a Number: "))
n = int(input("Enter b Number: "))
print("The gcd of {0} and {1} is : ".format(m,n),end=' ')
print(gcd(m, n))
Program-2
# Recursive function to return gcd of a and b
def gcd(a, b):
# Everything divides 0
if (a == 0):
return b
if (b == 0):
return a
# base case
if (a == b):
return a
# a is greater
if (a > b):
return gcd(a-b, b)
return gcd(a, b-a)
# main function
s = input("Enter a String: ")
#call the function to check the string is palindrome or not
ans =palindrome(s)
print(s," is Palindrome:",end='')
if (ans):
print("TRUE ")
else:
print("FALSE")