Python Lab Report to
Python Lab Report to
Date: 5/12/2024
Objective:
Experiment:
1. Problem Statement:
2. Algorithm/Logic:
Use Python's built-in max() function to find the largest of the three
numbers.
return max(a, b, c)
4. Execution Process:
To test the function, I provided different sets of inputs (e.g., 10, 20, 5) and
verified the output by printing the result.
Observations:
Output:
Conclusion:
This task demonstrated how to define a simple function and use built-in
functions like max() for common operations. It reinforced concepts such as
function creation, argument passing, and utilizing ’s extensive standard
library.
Lab Report
Exercise Number: [2]
Date: 5/12/2024
Objective:
Experiment:
1. Problem Statement:
Write a function to sum all the numbers in a list. The function should take
a list of numbers as input and return the sum of all the numbers in the list.
2. Algorithm/Logic:
Use 's built-in sum() function to calculate the sum of the list elements.
3. Code:
def sum_of_list(lst):
return sum(lst)
4. Execution Process:
To test the function, I provided different lists (e.g., [1, 2, 3, 4]) and printed
the result to verify correctness.
Observations:
The function worked as expected and returned the correct sum of all the
numbers in the list.
Output:
Conclusion:
This task helped reinforce the use of 's built-in functions like sum() to
perform common operations on lists. It also demonstrated how to handle
lists effectively and compute aggregates.
Lab Report
Objective:
The objective of this lab is to practice defining functions that work with
conditionals and range checks, using basic logical operations.
Experiment:
1. Problem Statement:
2. Algorithm/Logic:
3. Code:
4. Execution Process:
I defined the function is_in_range() which checks if the number falls
between the start and end values (inclusive).
I tested the function using various inputs like 5, 1, 10 and verified the
output.
Observations:
I tested edge cases such as when the number is equal to the start or end
of the range, and the function handled them correctly.
Output:
Conclusion:
Lab Report
Date: 5/12/2024
Course Title: 24Z110 Programming Lab
Objective:
Experiment:
1. Problem Statement:
2. Algorithm/Logic:
3. Code:
def count_case(s):
upper = 0
lower = 0
for char in s:
if char.isupper():
upper += 1
elif char.islower():
lower += 1
4. Execution Process:
I tested the function with a sample string 'The quick Brow Fox' and printed
the result to verify correctness.
Observations:
It handled mixed case strings, as well as edge cases like an empty string
or strings without uppercase or lowercase characters.
Output:
# Output:
Conclusion:
This task helped reinforce the use of string manipulation functions and
conditional checks to solve problems involving text processing. I practiced
counting specific types of characters using ’s built-in string methods.
Lab Report
Date: 5/12/2024
Course Title: 24Z110 Programming Lab
Objective:
Experiment:
1. Problem Statement:
2. Algorithm/Logic:
If the number is less than or equal to 1, return False (since prime numbers
are greater than 1).
Loop through all numbers from 2 to the square root of the number and
check if the number is divisible by any of them.
3. Code:
def is_prime(num):
if num <= 1:
return False
return False
return True
4. Execution Process:
I used a loop to check if any number from 2 to the square root of the given
number divides it.
Date: 5/12/2024
Objective:
Experiment:
1. Problem Statement:
2. Algorithm/Logic:
Check if the set of characters in the string contains all the letters of the
English alphabet.
3. Code:
import string
def is_pangram(s):
s = s.lower()
alphabet_set = set(string.ascii_lowercase)
I defined the function is_pangram() and tested it with strings like "The
quick brown fox jumps over the lazy dog" and "Hello World!".
Observations:
I tested both uppercase and lowercase inputs, and the function worked in
all cases.
Output:
Conclusion:
This task helped me understand how to work with strings and sets in . The
use of set() provided an efficient way to compare the characters of a
string to the complete alphabet, demonstrating how to check for
conditions like a pangram.
Lab Report
Date: 5/12/2024
Objective:
Experiment:
1. Problem Statement:
2. Algorithm/Logic:
Split the string into a list of words using the split() function with '-' as the
delimiter.
Join the sorted words back together with a hyphen and print the result.
3. Code:
def sort_hyphenated_words():
words = input_str.split('-')
words.sort()
print('-'.join(words))
4. Execution Process:
I split the string into a list of words, sorted them alphabetically, and joined
them back into a single string.
Observations:
Output:
# Output: black-green-red-white-yellow
Conclusion:
Lab Report
Date: 5/12/2024
Objective:
The objective of this lab is to practice defining and calling functions within
other functions, as well as to reinforce the concept of function scope.
Experiment:
1. Problem Statement:
2. Algorithm/Logic:
The outer function should invoke the inner function and potentially use its
result.
3. Code:
def outer_function():
def inner_function():
inner_function()
outer_function()
4. Execution Process:
I invoked the inner function from within the outer function to demonstrate
nested function calls.
Observations:
The program correctly executed the inner function from within the outer
function.
Output:
Conclusion:
This task demonstrated the concept of nested functions and the scope of
functions in . I learned how functions can be defined and called within
other functions, which is useful for organizing code logically.
Lab Report
Date: 5/12/2024
Objective:
The objective of this lab is to practice finding the largest item in a given
list using 's built-in functions.
Experiment:
1. Problem Statement:
Write a function to find the largest item from a given list. The function
should take a list of numbers and return the largest value.
2. Algorithm/Logic:
Use 's built-in max() function to find the largest number in the list.
3. Code:
def largest_item(lst):
return max(lst)
4. Execution Process:
I tested the function with different lists, such as [10, 20, 5] and [7, 3, 9,
12], to ensure it works correctly.
Observations:
Output:
Conclusion:
This task reinforced the use of 's built-in max() function to find the largest
item in a list. I practiced working with lists and utilizing efficient built-in
functions.
Date: 5/12/2024
Objective:
Experiment:
1. Problem Statement:
Define a function that accepts a roll number and returns whether the
student is present or absent. The function will simulate a simple
attendance system.
2. Algorithm/Logic:
3. Code:
def check_attendance(roll_number):
if roll_number in present_students:
return "Present"
else:
return "Absent"
4. Execution Process:
Observations:
The function correctly identified if the roll number was present or absent
in the list.
I tested with multiple roll numbers and the results matched the expected
outputs.
Output:
Conclusion:
This task helped me practice working with lists and conditionals. I learned
how to check for a student’s attendance using roll numbers, simulating an
attendance system.
Lab Report
Date: 5/12/2024
Course Title: 24Z110 Programming Lab
Objective:
Experiment:
1. Problem Statement:
2. Algorithm/Logic:
3. Code:
def count_vowels_consonants(word):
vowels = "aeiou"
word = word.lower()
vowel_count = 0
consonant_count = 0
for char in word:
if char.isalpha():
if char in vowels:
vowel_count += 1
else:
consonant_count += 1
4. Execution Process:
Observations:
The function correctly counted the vowels and consonants in each word.
Output:
Conclusion:
Lab Report
Date: 5/12/2024
Objective:
Experiment:
1. Problem Statement:
Define a function that accepts a word in lowercase and returns the word in
uppercase.
2. Algorithm/Logic:
3. Code:
def to_uppercase(word):
return word.upper()
4. Execution Process:
I tested the function with several lowercase words like "hello", "world",
and "".
The function correctly returned the uppercase version of the input string.
Observations:
Output:
Lab Report
Date: 5/12/2024
Objective:
The objective of this lab is to practice calculating the area of a circle using
the radius.
Experiment:
1. Problem Statement:
Define a function that accepts the radius of a circle and returns its area.
2. Algorithm/Logic:
3. Code:
import math
def circle_area(radius):
4. Execution Process:
I tested the function with different values of the radius, such as 5 and 10,
to calculate the corresponding areas.
Observations:
Conclusion:
This task helped me understand how to use 's math module and apply
mathematical formulas in programming. I successfully calculated the area
of a circle.
Lab Report
Date: 5/12/2024
Objective:
Experiment:
1. Problem Statement:
Define a function that accepts a number and returns whether the number
is even or odd.
2. Algorithm/Logic:
3. Code:
def check_even_odd(num):
if num % 2 == 0:
return "Even"
else:
return "Odd"
4. Execution Process:
Observations:
The function correctly identified whether the number was even or odd.
Output:
print(check_even_odd(4)) # Output: Even
Conclusion:
Lab Report
Date: 5/12/2024
Objective:
The objective of this lab is to practice writing a function that accepts two
numbers as arguments and returns their sum.
Experiment:
1. Problem Statement:
Write a function that accepts two numbers as arguments and returns the
sum of those numbers.
2. Algorithm/Logic:
3. Code:
4. Execution Process:
I tested the function with different pairs of numbers, such as (10, 20) and
(5, 5).
Observations:
Output:
Conclusion:
This task helped me practice basic function creation and arithmetic
operations. I successfully wrote a function to calculate the sum of two
numbers.
Lab Report
Date: 5/12/2024
Objective:
Experiment:
1. Problem Statement:
2. Algorithm/Logic:
3. Code:
def create_dictionary():
sample_dict = {
"name": "John",
"age": 25,
return sample_dict
4. Execution Process:
The function was tested by printing the dictionary to verify its correctness.
Observations:
Output:
print(create_dictionary())
Lab Report
Date: 5/12/2024
The objective of this lab is to write a program that prints twin primes less
than 1000. Twin primes are pairs of prime numbers that have a difference
of 2.
Experiment:
1. Problem Statement:
Write a program to print twin primes less than 1000. Twin primes are
pairs of consecutive prime numbers with a difference of 2.
2. Algorithm/Logic:
Check each number and its next consecutive number to see if both are
prime and their difference is 2.
3. Code:
def is_prime(num):
if num < 2:
return False
if num % i == 0:
return False
return True
def twin_primes():
for i in range(2, 1000):
print(f"({i}, {i + 2})")
4. Execution Process:
I tested the function by printing twin primes for numbers between 2 and
1000.
I used the is_prime() function to check for prime numbers and verified if
the difference between two consecutive primes is exactly 2.
Observations:
Output:
twin_primes()
# Output: (3, 5), (5, 7), (11, 13), (17, 19), ...
Conclusion:
This task helped me understand how to identify prime numbers and check
for twin primes. I used a combination of loops and conditions to find twin
primes less than 1000.
Lab Report
Date: 5/12/2024
The objective of this lab is to calculate the sum of the cubes of individual
digits of a number and use it to check for Armstrong numbers.
Experiment:
1. Problem Statement:
Write a function cubesum() that accepts an integer and returns the sum of
the cubes of its digits. Then, use this function to create PrintArmstrong()
and isArmstrong() to check if a number is Armstrong.
2. Algorithm/Logic:
Define the cubesum() function that calculates the sum of the cubes of
digits of a number.
3. Code:
def cubesum(num):
def isArmstrong(num):
def PrintArmstrong(limit):
if isArmstrong(num):
print(num)
4. Execution Process:
I used the cubesum() function to calculate the sum of cubes of digits for
any given number.
I tested isArmstrong() with numbers like 153 and 370 to verify if they are
Armstrong numbers.
Observations:
Output:
PrintArmstrong(1000)
Conclusion:
Lab Report
Date: 5/12/2024
The objective of this lab is to find the sum of proper divisors of a number.
Experiment:
1. Problem Statement:
Write a function sumPdivisors() that finds the sum of the proper divisors of
a number. Proper divisors are divisors of a number excluding the number
itself.
2. Algorithm/Logic:
Find all divisors of the number and sum them, excluding the number itself.
3. Code:
def sumPdivisors(num):
divisors_sum = 0
if num % i == 0:
divisors_sum += i
return divisors_sum
4. Execution Process:
I tested the function with a number like 36 to check if the sum of its
proper divisors is calculated correctly.
I verified that the function excluded the number itself when summing the
divisors.
Observations:
Output:
Conclusion:
Lab Report
Date: 5/12/2024
Experiment:
1. Problem Statement:
Write a function that checks whether two numbers are amicable numbers.
Two numbers are amicable if the sum of the proper divisors of each
number equals the other number.
2. Algorithm/Logic:
For two numbers a and b, calculate the sum of the proper divisors of a and
b. If the sum of divisors of a equals b and the sum of divisors of b equals
a, then they are amicable.
3. Code:
def sumPdivisors(num):
divisors_sum = 0
if num % i == 0:
divisors_sum += i
return divisors_sum
4. Execution Process:
I tested the program with the example of 220 and 284 to verify the result.
Observations:
Output:
Conclusion: