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

AVINEET Python Practical File

This document provides 11 code examples demonstrating basic Python programming concepts including: 1) Computing the greatest common divisor (GCD) of two numbers. 2) Finding the square root of a number using Newton's method. 3) Exponentiation using while and for loops. 4) Finding the maximum value in a list. 5) Printing the Fibonacci series. 6) Printing a pyramid pattern of asterisks. 7) Finding the sum of even numbers in a list. 8) Rearranging a list to group even and odd numbers. 9) Removing duplicate elements from a list. 10) Rotating an array by a specified number of positions. 11) Reversing parts

Uploaded by

GAURAV
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

AVINEET Python Practical File

This document provides 11 code examples demonstrating basic Python programming concepts including: 1) Computing the greatest common divisor (GCD) of two numbers. 2) Finding the square root of a number using Newton's method. 3) Exponentiation using while and for loops. 4) Finding the maximum value in a list. 5) Printing the Fibonacci series. 6) Printing a pyramid pattern of asterisks. 7) Finding the sum of even numbers in a list. 8) Rearranging a list to group even and odd numbers. 9) Removing duplicate elements from a list. 10) Rotating an array by a specified number of positions. 11) Reversing parts

Uploaded by

GAURAV
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Basic of Python Programming

AVINEET KAUR
BCA 3rd Sem

Roll No.02215102021
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


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-
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-
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
Using for loop-

base = 3
exponent = 4

result = 1

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


result *= base

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

Output-
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-
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-
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-
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.

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy