AVINEET Python Practical File
AVINEET Python Practical File
AVINEET KAUR
BCA 3rd Sem
Roll No.02215102021
INDEX
QUESTIONS PAGE.NO
6. To print a pyramid
*
**
***
**** 9
odd numbers.
3,4,5,6,7,1,2.
Output-
Q.2- Find the square root of a number (Newton‘s method).
Code- def squareRoot(n, l) :
x=n
count = 0
while (1) :
count += 1
if (abs(root - x) < l) :
break
x = root
return root
if __name__ == "__main__" :
n = 33
l = 0.01
print(squareRoot(n, l))
Output-
Q.3- Exponentiation (power of a number).
base = 6
exponent = 4
result = 2
while exponent != 0:
result *= base
exponent-=1
Output
Using for loop-
base = 3
exponent = 4
result = 1
Output-
Q.4- Find the maximum of a list of numbers.
largest_number = heights[0]
print(largest_number)
Output-
O.5- Print the Fibonacci series.
n1, n2 = 0, 1
count = 0
if nterms <= 0:
print("Please enter a positive integer")
elif nterms == 1:
print("Fibonacci sequence upto",nterms,":")
print(n1)
# generate fibonacci sequence
else:
print("Fibonacci sequence:")
while count < nterms:
print(n1)
nth = n1 + n2
# update values
n1 = n2
n2 = nth
count += 1
Output-
Q.6- To print a pyramid
*
**
***
**** .
for i in range(rows):
for j in range(i+1):
print("* ", end="")
print("\n")
Output-
Q.7- Find the sum of all even numbers in a list.
Code- even_sum=0
i=0
while(i<20):
even_sum=even_sum+i
i=i+2
print("sum =",even_sum)
Output-
Q.8- Rearrange list such as it has all even numbers followed by odd
numbers.
A = list()
n = int(input("Enter the size of the First List ::"))
print("Enter the Element of First List ::")
for i in range(int(n)):
k = int(input(""))
A.append(k)
splitevenodd(A)
Output-
Q.9- Remove the duplicate elements in an array.
sam_list = [12, 55, 12, 16, 13, 15, 16, 11, 11, 47, 50,50]
print ("The list is: " + str(sam_list))
sam_list = list(set(sam_list))
Output-
Q.10- Array rotation i.e. rotate by 2 input: 1,2,3,4,5,6,7 output:
3,4,5,6,7,1,2.
arr = [1, 2, 3, 4, 5, 6, 7]
print("Array after left rotation is: ", end=' ')
print(rotateArray(arr, 2))
Output-
Q.11- Reversal algorithm for array rotation.
def printArray(arr):
for i in range(0, len(arr)):
print(arr[i], end=" ")