0% found this document useful (0 votes)
56 views

Kritika's Python Practical File

This document is a practical file on basic Python programming concepts by Kritika Singh Chauhan. It contains 11 questions on topics like computing GCD, square roots, exponentiation, finding maximum/minimum in lists, Fibonacci series, printing patterns like pyramids, manipulating lists by sorting/removing elements, and array rotation. For each question it provides the code implementation and output.

Uploaded by

GAURAV
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)
56 views

Kritika's Python Practical File

This document is a practical file on basic Python programming concepts by Kritika Singh Chauhan. It contains 11 questions on topics like computing GCD, square roots, exponentiation, finding maximum/minimum in lists, Fibonacci series, printing patterns like pyramids, manipulating lists by sorting/removing elements, and array rotation. For each question it provides the code implementation and output.

Uploaded by

GAURAV
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/ 15

Basic of Python Programming

Kritika Singh Chauhan


BCA 3rd Sem

Roll No. 35215102021

1 | K r i ti k a ’ s p r a c ti c a l fi l e
INDEX
QUESTIONS PAGE.NO

1. Compute the GCD of two numbers. 3

2. Find the square root of a number (Newton‘s method). 4

3. Exponentiation (power of a number). 5

4. Find the maximum of a list of numbers. 7

5. Print the Fibonacci series. 8

6. To print a pyramid
*
**
***
**** 9

7. Find the sum of all even numbers in a list. 10

8. Rearrange list such as it has all even numbers followed by 11

odd numbers.

9. Remove the duplicate elements in an array. 12

10.Array rotation i.e rotate by 2 input: 1,2,3,4,5,6,7 output: 13

3,4,5,6,7,1,2.

11.Reversal algorithm for array rotation. 14

2 | K r i ti k a ’ s p r a c ti c a l fi l e
Q.1- Compute the GCD of two numbers.
Code- num1 = 36
num2 = 60
gcd = 1

for i in range(1, min(num1, num2)):


if num1 % i == 0 and num2 % i == 0:
gcd = i
print("GCD of", num1, "and", num2, "is", gcd)

Output-

3 | K r i ti k a ’ s p r a c ti c a l fi l e
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

root = 0.5 * (x + (n / x))

if (abs(root - x) < l) :
break

x = root

return root

if __name__ == "__main__" :

n = 33
l = 0.01

print(squareRoot(n, l))

Output-

4 | K r i ti k a ’ s p r a c ti c a l fi l e
Q.3- Exponentiation (power of a number).

Output- Using while loop.

base = 6
exponent = 4

result = 2

while exponent != 0:
result *= base
exponent-=1

print("Answer = " + str(result))

Output

5 | K r i ti k a ’ s p r a c ti c a l fi l e
Using for loop-

base = 3
exponent = 4

result = 1

for exponent in range(exponent, 0, -1):


result *= base

print("Answer = " + str(result))

Output-

6 | K r i ti k a ’ s p r a c ti c a l fi l e
Q.4- Find the maximum of a list of numbers.

Code- heights = [100, 2, 67, 10, 167, 600]

largest_number = heights[0]

for number in heights:


if number > largest_number:
largest_number = number

print(largest_number)

Output-

7 | K r i ti k a ’ s p r a c ti c a l fi l e
O.5- Print the Fibonacci series.

Code- nterms = int(input("How many terms? "))

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-

8 | K r i ti k a ’ s p r a c ti c a l fi l e
Q.6- To print a pyramid
*
**
***
**** .

Code- rows = int(input("Enter number of rows: "))

for i in range(rows):
for j in range(i+1):
print("* ", end="")
print("\n")

Output-

9 | K r i ti k a ’ s p r a c ti c a l fi l e
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-

10 | K r i ti k a ’ s p r a c ti c a l fi l e
Q.8- Rearrange list such as it has all even numbers followed by odd
numbers.

Code- def splitevenodd(A):


evenlist = []
oddlist = []
for i in A:
if (i % 2 == 0):
evenlist.append(i)
else:
oddlist.append(i)
print("Even lists:", evenlist)
print("Odd lists:", oddlist)

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-

11 | K r i ti k a ’ s p r a c ti c a l fi l e
Q.9- Remove the duplicate elements in an array.

Code- # removing duplicated from the list using set()

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))

print ("The list after removing duplicates: " + str(sam_list))

Output-

12 | K r i ti k a ’ s p r a c ti c a l fi l e
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.

Code- def rotateArray(a, d):


temp = []
n = len(a)
for i in range(d, n):
temp.append(a[i])
i=0
for i in range(0, d):
temp.append(a[i])
a = temp.copy()
return a

arr = [1, 2, 3, 4, 5, 6, 7]
print("Array after left rotation is: ", end=' ')
print(rotateArray(arr, 2))

Output-

13 | K r i ti k a ’ s p r a c ti c a l fi l e
Q.11- Reversal algorithm for array rotation.

Code- def reverseArray(arr, start, end):


while (start < end):
temp = arr[start]
arr[start] = arr[end]
arr[end] = temp
start = start + 1
end = end - 1

def Rotate(a, d):


if d == 0:
return
n = len(a)
d=d%n
reverseArray(a, 0, d - 1)
reverseArray(a, d, n - 1)
reverseArray(a, 0, n - 1)

def printArray(arr):
for i in range(0, len(arr)):
print(arr[i], end=" ")

a = [11, 12, 13, 14, 14, 15, 16, 17]


n = len(a)
d=5
printArray(a)
Rotate(a, d)
print("\nShifted array: ")
printArray(a)

14 | K r i ti k a ’ s p r a c ti c a l fi l e
Output-

15 | K r i ti k a ’ s p r a c ti c a l fi l e

You might also like