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

Python Lab File Activities

Python

Uploaded by

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

Python Lab File Activities

Python

Uploaded by

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

Title: Python program to print all Prime numbers in an Interval

Code:

def print_primes_in_interval(start, end):

primes = []

for num in range(start, end + 1):

if num > 1: # Prime numbers are greater than 1

for i in range(2, int(num ** 0.5) + 1):

if num % i == 0:

break

else:

primes.append(num)

return primes

print("Prime numbers between 10 and 50:", print_primes_in_interval(10, 50))

Output:

Prime numbers between 10 and 50: [11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
Title: Python program to check whether a number is Prime or not
Code:

def is_prime(num):

if num <= 1:

return False

for i in range(2, int(num ** 0.5) + 1):

if num % i == 0:

return False

return True

print("Is 29 prime?", is_prime(29))

Output:

Is 29 prime? True
Title: Python Program for nth Fibonacci number
Code:

def fibonacci(n):

if n <= 0:

return 0

elif n == 1:

return 1

else:

return fibonacci(n - 1) + fibonacci(n - 2)

print("10th Fibonacci number:", fibonacci(10))

Output:

10th Fibonacci number: 55


Title: Python Program to check if a number is a Fibonacci number
Code:

def is_fibonacci(num):

x1 = 5 * num ** 2 + 4

x2 = 5 * num ** 2 - 4

return int(x1 ** 0.5) ** 2 == x1 or int(x2 ** 0.5) ** 2 == x2

print("Is 21 a Fibonacci number?", is_fibonacci(21))

Output:

Is 21 a Fibonacci number? True


Title: Python Program for array rotation
Code:

def rotate_array(arr, k):

n = len(arr)

k = k % n # Handle cases where k > n

return arr[k:] + arr[:k]

print("Array after rotation:", rotate_array([1, 2, 3, 4, 5], 2))

Output:

Array after rotation: [3, 4, 5, 1, 2]


Title: Python Program for Reversal algorithm for array rotation
Code:

def reversal_rotate(arr, k):

n = len(arr)

k=k%n

arr[:k] = reversed(arr[:k])

arr[k:] = reversed(arr[k:])

arr.reverse()

return arr

print("Reversed rotated array:", reversal_rotate([1, 2, 3, 4, 5], 2))

Output:

Reversed rotated array: [3, 4, 5, 1, 2]


Title: Python Program to Split the array and add the first part to the end
Code:

def split_and_add(arr, k):

return arr[k:] + arr[:k]

print("Split and add result:", split_and_add([1, 2, 3, 4, 5], 2))

Output:

Split and add result: [3, 4, 5, 1, 2]


Title: Python Program for Find remainder of array multiplication divided by n
Code:

def remainder_of_product(arr, n):

product = 1

for num in arr:

product = (product * num) % n

return product

print("Remainder of product divided by 11:", remainder_of_product([1, 2, 3, 4, 5], 11))

Output:

Remainder of product divided by 11: 1


Title: Python | Multiply all numbers in the list
Code:

def multiply_list(lst):

result = 1

for num in lst:

result *= num

return result

print("Product of list:", multiply_list([1, 2, 3, 4]))

Output:

Product of list: 24
Title: Python program to find smallest number in a list
Code:

def smallest_in_list(lst):

return min(lst)

print("Smallest number in list:", smallest_in_list([4, 1, 7, 2, 9]))

Output:

Smallest number in list: 1

You might also like