Python Lab
Python Lab
Python Lab
Interactive Interpreter:
Python Script:
def indentation_error_example():
# Corrected Script
def corrected_script():
3. Write a script to find the Sum and average of first n natural numbers:
def sum_and_average(n):
total = sum(numbers)
average = total / n
result = sum_and_average(n)
OUTPUT
SCRIPTING LANGUAGE USING (PYTHON)
4. Given 2 strings, s1 and s2, create a new string by appending s2 in the middle of s1:
def append_middle(s1, s2):
middle_index = len(s1) // 2
result = s1[:middle_index] + s2 + s1[middle_index:]
return result
OUTPUT
return s == s[::-1]
if is_palindrome(word):
print("It's a palindrome.")
else:
OUTPUT
SCRIPTING LANGUAGE USING (PYTHON)
6. Write a script using a for loop that loops over a sequence:Write a program add.py
that takes 2 numbers as command line arguments and prints their sum:
# add.py
import sys
if len(sys.argv) != 3:
else:
num1 = float(sys.argv[1])
num2 = float(sys.argv[2])
print(f"Sum: {result}")
OUTPUT
numbers = [1, 2, 3, 4, 5]
print(num)
OUTPUT
SCRIPTING LANGUAGE USING (PYTHON)
8. Write a script to count the numbers of characters in the string and store them in a
dictionary data structure:
def count_characters(s):
char_count = {}
for char in s:
char_count[char] = char_count.get(char, 0) + 1
return char_count
9. Write a program to use split and join methods in the string and trace a birthday with
a dictionary data structure:
def add_birthday(dictionary, name, birthday):
dictionary[name] = birthday
def main():
# Initialize an empty dictionary to store birthdays
birthday_dict = {}
if __name__ == "__main__":
main()
10. Write a script that combines more than one list into a dictionary:
# Combine lists into a dictionary
OUTPUT
SCRIPTING LANGUAGE USING (PYTHON)
11. Compute the GCD (Greatest Common Divisor) & LCM (Least Common Multiple) of
two numbers:
import math
return math.gcd(x, y)
print(f"GCD: {gcd_result}")
print(f"LCM: {lcm_result}")
OUTPUT
SCRIPTING LANGUAGE USING (PYTHON)
def main():
# Set the range for finding prime numbers
start_range = 10
end_range = 50
if __name__ == "__main__":
main()
OUTPUT
SCRIPTING LANGUAGE USING (PYTHON)
max_number = max(numbers)
OUTPUT
SCRIPTING LANGUAGE USING (PYTHON)
# Example usage:
arr = [1, 3, 5, 7, 9, 11, 13]
target = 7
result = linear_search(arr, target)
if result != -1:
print(f"Linear Search: Element {target} found at index {result}.")
else:
print(f"Linear Search: Element {target} not found.")
if mid_value == target:
return mid # Return the index if the target is found
elif mid_value < target:
low = mid + 1
else:
high = mid - 1
# Example usage:
sorted_arr = [1, 3, 5, 7, 9, 11, 13]
target = 7
result = binary_search(sorted_arr, target)
if result != -1:
print(f"Binary Search: Element {target} found at index {result}.")
else:
print(f"Binary Search: Element {target} not found.")
OUTPUT
SCRIPTING LANGUAGE USING (PYTHON)
19. Write a function nearly equal to test whether two strings are nearly equal:
def nearly_equal(str1, str2):
return sum(c1 != c2 for c1, c2 in zip(str1, str2)) <= 1
if nearly_equal(string1, string2):
print("Strings are nearly equal.")
else:
print("Strings are not nearly equal.")
OUTPUT
def word_count(text):
words = text.split()
return len(words)
if len(sys.argv) != 2:
print("Usage: python word_count.py <text>")
else:
input_text = sys.argv[1]
count = word_count(input_text)
print(f"Word count: {count}")
OUTPUT
input_list = [1, 2, 3, 2, 4, 5, 6, 4]
result_duplicates = find_duplicates(input_list)
print(f"Duplicates in the list: {result_duplicates}")
OUTPUT
SCRIPTING LANGUAGE USING (PYTHON)
23. Write a script to calculate the age in years, months, and days of a person:
def calculate_age(birthdate):
current_date = datetime.now()
# Calculate the difference between the birthdate and the current date
months = remaining_days // 30
days = remaining_days % 30
def main():
if __name__ == "__main__":
main()
OUTPUT
def search_digits(input_string):
digits = re.findall(r'\d', input_string)
return digits