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

Python Lab Programs

Uploaded by

afshanhrr
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)
8 views

Python Lab Programs

Uploaded by

afshanhrr
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/ 4

# Function to calculate the average of two test marks

def calculate_average(mark1, mark2):

return (mark1 + mark2) / 2

# Accept three test marks from the user

test1 = float(input("Enter the mark for Test 1: "))

test2 = float(input("Enter the mark for Test 2: "))

test3 = float(input("Enter the mark for Test 3: "))

# Calculate the averages for all combinations of two test marks

average1 = calculate_average(test1, test2)

average2 = calculate_average(test1, test3)

average3 = calculate_average(test2, test3)

# Find the highest average

best_average = max(average1, average2, average3)

# Output the result

print(f"The better of the two test averages is: {best_average:.2f}")


# Function to find the smallest and largest numbers in a list

def find_smallest_largest(numbers):

smallest = min(numbers)

largest = max(numbers)

return smallest, largest

# Accept a list of numbers from the user

numbers = list(map(int, input("Enter the numbers separated by spaces: ").split()))

# Find the smallest and largest numbers

smallest, largest = find_smallest_largest(numbers)

# Output the result

print(f"The smallest number is: {smallest}")

print(f"The largest number is: {largest}")

# Function to sort numbers in ascending and descending order

def sort_numbers(numbers):

ascending = sorted(numbers)

descending = sorted(numbers, reverse=True)

return ascending, descending

# Accept a list of numbers from the user

numbers = list(map(int, input("Enter the numbers separated by spaces: ").split()))

# Sort the numbers in ascending and descending order

ascending, descending = sort_numbers(numbers)

# Output the result

print(f"Numbers in ascending order: {ascending}")

print(f"Numbers in descending order: {descending}")


#bubble sort

def bubble_sort(arr):

n = len(arr)

# Traverse through all array elements

for i in range(n - 1):

# Last i elements are already sorted

for j in range(0, n - i - 1):

# Swap if the element found is greater than the next element

if arr[j] > arr[j + 1]:

arr[j], arr[j + 1] = arr[j + 1], arr[j]

arr = [64, 34, 25, 12, 22, 11, 90]

print("Original array:", arr)

bubble_sort(arr)

print("Sorted array:", arr)

#uppercase n lowercase

def analyze_sentence(sentence):

words = sentence.split()

num_words = len(words)

num_digits = sum(char.isdigit() for char in sentence)

num_upper = sum(char.isupper() for char in sentence)

num_lower = sum(char.islower() for char in sentence)

return num_words, num_digits, num_upper, num_lower

# Input from the user

sentence = input("Enter a sentence: ")

# Analyze the sentence

num_words, num_digits, num_upper, num_lower = analyze_sentence(sentence)


# Display results

print(f"Number of words: {num_words}")

print(f"Number of digits: {num_digits}")

print(f"Number of uppercase letters: {num_upper}")

print(f"Number of lowercase letters: {num_lower}")

#pattern recognition

import re

def find_pattern_regex(text, pattern):

matches=re.findall(pattern,text)

return matches

text = "abc123abcxyzabc"

pattern = "abc"

matches = find_pattern_regex(text, pattern)

print("Matches using regular expressions:", matches)

You might also like