python file
python file
num = 7
if num > 0:
print("Positive number")
else:
print("Non-positive number")
# For loop
print(i)
i=0
while i < 5:
i += 1
if i == 3:
if i == 5:
output:
Experiment – 02
# List
# Tuple
# Dictionary
output:
Experiment – 03
x = 10 # Global variable
def outer_function():
def inner_function():
nonlocal x
inner_function()
def modify_global():
global x
outer_function()
print("Global x after outer_function =", x)
modify_global()
output:
Experiment – 04
try:
result = a / b
print("Result:", result)
except ZeroDivisionError:
except ValueError:
output:
Experiment – 05
# Define a class
class Student:
self.name = name
self.grade = grade
def display(self):
print("Grade:", self.grade)
s1 = Student("Alice", "A")
s1.display()
output:
Experiment – 06
# Linear Search
for i in range(len(arr)):
if arr[i] == target:
return i
return -1
low = 0
high = len(arr) - 1
if arr[mid] == target:
return mid
low = mid + 1
else:
high = mid - 1
return -1
target = 12
# Perform searches
print("List:", numbers)
print("Target:", target)
Develop program to implement insertion sort , selection sort, bubble sort, &
merge sort.
# Insertion Sort
def insertion_sort(arr):
key = arr[i]
j=i-1
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = key
# Selection Sort
def selection_sort(arr):
for i in range(len(arr)):
min_idx = i
min_idx = j
# Bubble Sort
def bubble_sort(arr):
n = len(arr)
for i in range(n):
# Merge Sort
def merge_sort(arr):
if len(arr) > 1:
mid = len(arr) // 2
left_half = arr[:mid]
right_half = arr[mid:]
merge_sort(left_half)
merge_sort(right_half)
i=j=k=0
arr[k] = left_half[i]
i += 1
else:
arr[k] = right_half[j]
j += 1
k += 1
arr[k] = left_half[i]
i += 1
k += 1
arr[k] = right_half[j]
j += 1
k += 1
# Sample List
# Insertion Sort
insertion_sorted = arr.copy()
insertion_sort(insertion_sorted)
selection_sorted = arr.copy()
selection_sort(selection_sorted)
# Bubble Sort
bubble_sorted = arr.copy()
bubble_sort(bubble_sorted)
# Merge Sort
merge_sorted = arr.copy()
merge_sort(merge_sorted)
output:
Experiment – 08
(i) If – statement
# Example to check if a number is positive
number = 7
if number > 0:
print("The number is positive.")
output:
if number > 0:
print("The number is positive.")
else:
print("The number is negative.")
output:
if number > 0:
print("The number is positive.")
elif number < 0:
print("The number is negative.")
else:
print("The number is zero.")
output:
Experiment – 09
Demonstrate the following function which operates on string in
python with suitable example.
(i) Len
# Example string
text = "Hello, Python!"
output:
(ii) Strip
# Example string with leading and trailing spaces
text = " Hello, Python! "
output:
(iii) Rstrip
# Example string with trailing spaces
text = "Hello, Python! "
output:
(iv) Lstrip
# Example string with leading spaces
text = " Hello, Python!"
output:
(v) Find
# Example string
text = "Hello, Python! Welcome to Python programming."
output:
(vi) Rfind
# Example string
text = "Hello, Python! Welcome to Python programming."
(vii) Index
# Example string
text = "Hello, Python! Welcome to Python programming."
# Trying to search for a substring that doesn't exist (this will raise
an error)
try:
position_not_found = text.index("Java")
print("Substring 'Java' found at index:", position_not_found)
except ValueError:
print("Substring 'Java' not found in the string.")
output:
(viii) Rindex
# Example string
text = "Hello, Python! Welcome to Python programming."
# Trying to search for a substring that doesn't exist (this will raise
an error)
try:
position_not_found = text.rindex("Java")
print("Substring 'Java' found at index:", position_not_found)
except ValueError:
print("Substring 'Java' not found in the string.")
output:
Experiment – 10
Write a python program to demonstrate local and global
variables.
# Global variable
x = 10
def my_function():
# Local variable
x=5
print("Inside function, local x =", x)
output: