Python Tutorial

Week - 3:

1. i) Write a program to convert a list and tuple into arrays.

Answer:

Method:-1

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

print(My_lt)

t=tuple(My_lt)

print(t)

Output: [1,2,3,4,5]

(1,2,3,4,5)

Method:- 2

import numpy as np

my_list = [1, 2, 3, 4, 5, 6, 7, 8]

print("List to array: ")

print(np.asarray(my_list))

my_tuple = ([8, 4, 6], [1, 2, 3])

print("Tuple to array: ")

print(np.asarray(my_tuple))

Output: [1,2,3,4,5]

(1,2,3,4,5)

ii) Write a program to find common values between two arrays.

Answer:

Method:-1

def intersection_list(list1, list2):

list3 = [value for value in list1 if value in list2]

return list3

# Driver Code

list1 = [40, 90, 11, 58, 31, 66, 8, 4, 79]

list2 = [58, 90, 54, 31, 45, 11, 6, 28, 26]

print(intersection_list(list1, list2))

output:

[90, 11, 58, 31]

Method:-2

import numpy as np

a1 = np.array([0, 10, 20, 40, 60])

print("Array1: ",a1)

a2 = [10, 30, 40]

print("Array2: ",a2)

print("Common values between two arrays:")

print(np.intersect1d(a1, a2))

Output:

Array1: [ 0 10 20 40 60]

Array2: [10, 30, 40]

Common values between two arrays:

[10 40]

Method:-3

import numpy as np

ar1 = np.array([10, 20, 50, 30, 40])

ar2 = [10, 30, 50]

# Common values between two arrays

print(np.intersect1d(ar1, ar2))

Output:

[10 30 50]

2. Write a function called gcd that takes parameters a & b and returns their greatest common divisor.

Answers:

Method:-1

def gcd(a,b):

gcd = 1

if a % b == 0:

return b

for k in range(int(b / 2), 0, -1):

if a % k == 0 and b % k == 0:

gcd = k

break

return gcd

print("GCD of 12 & 17 =",gcd(12, 17))

print("GCD of 4 & 6 =",gcd(4, 6))

print("GCD of 336 & 360 =",gcd(336, 360))

Output:

GCD of 12 & 17 = 1

GCD of 4 & 6 = 2

GCD of 336 & 360 = 24

Method:-2

#Import math Library

>>>import math

#find the the greatest common divisor of the two integers

print (math.gcd(3, 6))-----------3

print (math.gcd(6, 12))-------------------6

print (math.gcd(12, 36))----------------12

print (math.gcd(-12, -36))------------------12

print (math.gcd(5, 12))--------------------------1

print (math.gcd(10, 0))-----------------------------10

print (math.gcd(0, 34))--------------------------------34

print (math.gcd(0, 0))---------------------------------------0

3. Write a function called palindrome that takes a string argument and returns True if it is a palindrome and False otherwise. Remember that you can use the built-in function len to check the length of a string.

Answer :

# function which return reverse of a string

def isPalindrome(s):

return s == s[::-1]

# Driver code

s = "madam" #string=input(("Enter a letter:"))

ans = isPalindrome(s)

if ans:

print("TRUE")

else:

print("FALSE")

print(len(s))

Method:2

def isPalindrome(str):

# Run loop from 0 to len/2

for i in range(0, int(len(str)/2)):

if str[i] != str[len(str)-i-1]:

return False

return True

# main function

s = "madam"

ans = isPalindrome(s)

if ans:

print("True")

else:

print("False")

print(len(s))

Method:3

x = "malayalam"

w = ""

for i in x:

w = i + w

if (x == w):

print("True")

else:

print("False")

print(len(x))

Method:4

n=int(input("Enter number:"))

temp=n

rev=0

while(n>0):

dig=n%10

rev=rev*10+dig

n=n//10

if(temp==rev):

print("True")

else:

print("False")

Program Explanation

1. Enter the input value which is integer type and stored in the variable “n”.
2. The “n” integer value is assigned to another temporary variable called “temp”.
3. While loop is used and till the last digit of the number is attained by using the modulus operator.
4. The last digit is stored at the one’s place, second last digit is stored at the ten’s place and continues.
5. The last digit is then removed by dividing the number with 10.
6. This loop ends when the value of the number become zero.
7. The reverse of the number is then compared with the integer value stored in the temporary variable.
8. If both are same, then it’s true.
9. If both aren’t same, then it’s false.