0% found this document useful (0 votes)
62 views7 pages

For Me 3yr Python-2

The document contains multiple questions and code snippets for Python programs to perform various tasks: 1) Find the greatest common divisor (GCD) of two numbers using recursion, find the square root of a number using Newton's method, exponentiation of a number, and finding the maximum of a list. 2) Perform linear search on a list, binary search on a sorted list, selection sort and insertion sort on lists. 3) Find the first n prime numbers between two given numbers and use command line arguments in a Python program.

Uploaded by

Ayush Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views7 pages

For Me 3yr Python-2

The document contains multiple questions and code snippets for Python programs to perform various tasks: 1) Find the greatest common divisor (GCD) of two numbers using recursion, find the square root of a number using Newton's method, exponentiation of a number, and finding the maximum of a list. 2) Perform linear search on a list, binary search on a sorted list, selection sort and insertion sort on lists. 3) Find the first n prime numbers between two given numbers and use command line arguments in a Python program.

Uploaded by

Ayush Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Ques1. To write a python program to find GCD of two numbers?

def gcd(a,b):
if(b==0):
return a
else:
return gcd(b,a%b)
a=int(input("Enter first number:"))
b=int(input("Enter second number:"))
GCD=gcd(a,b)
print("GCD is: ")
print(GCD)

Output:

Ques.2. To write a Python program to find the square root of a number by Newton’s Method?
def newton_method(number, number_iters = 500):
a = float(number) # number to get square root of
for i in range(number_iters): # iteration number
number = 0.5 * (number + a / number) # update
# x_(n+1) = 0.5 * (x_n +a / x_n)
return number

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


ans = newton_method(num)
print(ans)

Output:
Ques. 3To write a Python program to find the exponentiation of a number.

a = int(input("Enter base:"))
b = int(input("Enter exponent:"))
result = a**b

print (a, " to the power of ", b, " is = ", result)

Output:

Ques. To write a Python Program to find the maximum from a list of numbers.
list1 = []
num = int(input("Enter number of elements in list: "))
for i in range(1, num + 1):
ele = int(input("Enter elements: "))
list1.append(ele)
print("Largest element is:", max(list1))

Output:

Ques. To write a Python Program to perform Linear Search.


lst = []
num = int(input(“Enter size of list: \t”))
for n in range(num):
numbers = int(input(“Enter any number: \t”))
lst.append(numbers)
x = int(input(“\nEnter number to search: \t”))
found = False
for i in range(len(lst)):
if lst[i] == x:
found = True
print(“\n%d found at position %d” % (x, i))
break
if not found:
print(“\n%d is not in list” % x)

Output:

Ques. To write a Python Program to perform binary Search.


def binarySearch(alist, item):
first = 0
last = len(alist)-1
found = False
while first<=last and not found:
midpoint = (first + last)//2
if alist[midpoint] == item:
found = True
else:
if item < alist[midpoint]:
last = midpoint-1
else:
first = midpoint+1
return found
testlist = [0, 1, 2, 8, 13, 17, 19, 32, 42,]
lst = []
num = int(input("Enter size of list: \t"))
for n in range(num):
numbers = int(input("Enter any number: \t"))
lst.append(numbers)
x = int(input("\nEnter number to search: \t"))
print(binarySearch(lst,x))

Output:

Ques. To write a Python program to perform selection sort.


lst = []
num = int(input("Enter size of list: \t"))
for n in range(num):
numbers = int(input("Enter any number: \t"))
lst.append(numbers)
size = len(lst)
for i in range(0,size):
for j in range(i+1,size):
if lst[j] < lst[i]:
min = lst[j]
lst[j] = lst[i]
lst[i] = min
print(lst)

Output:

Ques. To write a Python Program to perform insertion sort.


Arr = []
num = int(input("Enter size of list: \t"))
for n in range(num):
numbers = int(input("Enter any number: \t"))
Arr.append(numbers)
n= len(Arr)
i = 1
while (i<=n-1):
value = Arr[i]
position = i
while (position>0 and Arr[position-1]>value):
Arr[position] = Arr[position-1]
position = position -1
i = i+1
Arr[position] = value
for i in Arr:
print (i)

Output:

Ques. To write a Python Program to to find first n prime numbers.


start = 11
end = 25
for val in range(start, end + 1):
if val > 1:
for n in range(2, val):
if (val % n) == 0:
break
else:
print(val)

Output:

Ques. To write a Python Program for command line arguments.


import sys
argumentList = sys.argv
print (argumentList)
print (sys.argv[0])
print (sys.argv[1])

Output:

You might also like