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

sypython pr11 to p20

The document contains practical Python programming exercises, including checking for palindromes, printing prime numbers, searching elements in a list, checking Armstrong numbers, creating arrays, and demonstrating array methods. Each exercise is accompanied by code snippets and example outputs. The exercises cover various fundamental programming concepts and functions in Python.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

sypython pr11 to p20

The document contains practical Python programming exercises, including checking for palindromes, printing prime numbers, searching elements in a list, checking Armstrong numbers, creating arrays, and demonstrating array methods. Each exercise is accompanied by code snippets and example outputs. The exercises cover various fundamental programming concepts and functions in Python.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Practical : 11 Write a Python program to check the given number is palindrome

or not.
num = int(input("Enter a number: "))
original_num = num
reversed_num = 0
while num > 0:
digit = num % 10
reversed_num = reversed_num * 10 + digit
num //= 10

if original_num == reversed_num:
print("Number is a palindrome.")
else:
print("Number is not a palindrome.")

output:

PS C:\sw> python p11.py

Enter a number: 123

Number is not a palindrome.

Practical: 12 Write a Python Program to print first 10 prime number.


count = 0
num = 2
while count < 10:
is_prime = True
for i in range(2, num):
if num % i == 0:
is_prime = False
break
if is_prime:
print(num)
count += 1

num += 1
output:

PS C:\sw> python p12.py

Hiral M. Patel Page 1


11

13

17

19

23

29

Practical: 13 Write a program to search an element in the list using for loop and
also demonstrate the use of “else” with for loop.
my_list = [5, 10, 15, 20, 25, 30]
search_element = int(input("Enter the element to search: "))
for item in my_list:
if item == search_element:
print("Element found in the list.")
break
else:
print("Element are not found in the list.")

output:

PS C:\sw> python p13.py

Enter the element to search: 10

Element found in the list.

Practical: 14 Write a Python Program to Check Armstrong Number.


num = int(input("Enter a number: "))
num_of_digits = len(str(num))
sum_of_powers = 0
original_num = num

while num > 0:


digit = num % 10
sum_of_powers += digit ** num_of_digits
num //= 10

if sum_of_powers == original_num:
print(f"{original_num} is an Armstrong number.")
else:

Hiral M. Patel Page 2


print(f"{original_num} is not an Armstrong number.")

ouput:

PS C:\sw> python p14.py

Enter a number: 153

153 is an Armstrong number.

Practical:15 Write a program to create one array from another array.


original_array = [1, 2, 3, 4, 5, 6]
new_array = original_array.copy()

print("Original Array:", original_array)


print("New Array:", new_array)

output:

PS C:\sw> python p15.py

Original Array: [1, 2, 3, 4, 5, 6]

New Array: [1, 2, 3, 4, 5, 6]

Practical: 16 Write a program to understand various methods of array class


mentioned: append, insert, remove, pop, index, tolist and count.
import array

arr = array.array( 'i' ,[10, 20,20, 30, 40, 50])

arr.append(60)
print("After append:", arr)

arr.insert(2, 25)
print("After insert:", arr)

arr.remove(40)
print("After remove:", arr)

popped_element = arr.pop()
print("After pop():", arr)
print("Popped element:", popped_element)

index_of_30 = arr.index(30)

Hiral M. Patel Page 3


print("Index of 30:", index_of_30)

arr_list = arr.tolist()
print("Array as list:", arr_list)

count_of_20 = arr.count(20)
print("Count of 20:", count_of_20)

output:

PS C:\sw> python p16.py

After append: array('i', [10, 20, 20, 30, 40, 50, 60])

After insert: array('i', [10, 20, 25, 20, 30, 40, 50, 60])

After remove: array('i', [10, 20, 25, 20, 30, 50, 60])

After pop(): array('i', [10, 20, 25, 20, 30, 50])

Popped element: 60

Index of 30: 4

Array as list: [10, 20, 25, 20, 30, 50]

Count of 20: 2

Practical: 17 Write a python program to display ascending and descending


order from given 10 Numbers.
numbers = []
print("Enter 10 numbers:")
for i in range(10):
num = int(input(f"Enter number {i+1}: "))
numbers.append(num)

ascending_order = sorted(numbers)
print("Numbers in Ascending Order:", ascending_order)

descending_order = sorted(numbers, reverse=True)


print("Numbers in Descending Order:", descending_order)

output:

python p17.py

Enter 10 numbers:

Enter number 1: 12

Hiral M. Patel Page 4


Enter number 2: 23

Enter number 3: 11

Enter number 4: 34

Enter number 5: 56

Enter number 6: 32

Enter number 7: 11

Enter number 8: 65

Enter number 9: 75

Enter number 10: 33

Numbers in Ascending Order: [11, 11, 12, 23, 32, 33, 34, 56, 65, 75]

Numbers in Descending Order: [75, 65, 56, 34, 33, 32, 23, 12, 11, 11]

Practical: 18 Write a Python program to print the duplicate elements of an


array.
arr = [1, 2, 3, 4, 5, 6, 7, 3, 8, 9, 2, 1]

duplicates = []
for i in range(len(arr)):
if arr[i] not in duplicates and arr.count(arr[i]) > 1:
duplicates.append(arr[i])
print("Duplicate elements:", duplicates)

output:

PS C:\sw> python p18.py

Duplicate elements: [1, 2, 3]

Practical: 19 Write Python programs to create functions and use it.


def add_numbers(a, b):
return a + b
num1 = 5
num2 = 10
result = add_numbers(num1, num2)
print(f"The sum of {num1} and {num2} is: {result}")

Output:

PS C:\sw> python p19.py

Hiral M. Patel Page 5


The sum of 5 and 10 is: 15

Practical: 20 Write a program to pass a list to a function and display it.


def greet(name):
print(f"Hello, {name}! Welcome to the Python world.")
user_name = input("Enter your name: ")
greet(user_name)

Output:

PS C:\sw> python p20.py

Enter your name: Hiral

Hello, Hiral! Welcome to the Python world.

Hiral M. Patel Page 6

You might also like