Professional Documents
Culture Documents
AI Labreport 1
AI Labreport 1
Lab Report NO 01
Name ID
For task 1: Write a Python Program to Count Even and Odd Numbers in a list.
1. Define a function count_even_odd that takes a list of numbers as input.
2. Initialize counters for even and odd numbers.
3. Use a for loop to iterate through each number in the provided list.
4. Check if the number is even or odd using the modulus operator (%).
5. Increment the corresponding counters based on whether the current number is even or odd.
6. Prompt the user to input a list of numbers separated by spaces.
7. Convert the user input, which is a string, into a list of integers.
8. Use a list comprehension to achieve this.
9. Call the count_even_odd function with the user-provided list of numbers.
10. Print the count of even and odd numbers obtained from the function.
11. For example, if the user inputs "2 4 5 7 8," the program will output the count of even
numbers (3) and odd numbers (2).
For task 2: Write a Python program to get the 4th element from the beginning and the 4th element
from the last of a tuple.
1. Define a function get_elements that takes a tuple (tuplex) as input.
2. Check if the length of the tuple is greater than or equal to 4.
3. If the tuple has enough elements, retrieve the 4th element from the beginning
(fourth_from_beginning) using index 3.
4. Retrieve the 4th element from the end (fourth_from_end) using negative index -4.
5. If the tuple has enough elements, return a tuple containing both the 4th element from the
beginning and the 4th element from the end.
6. If the tuple does not have enough elements, return a message indicating that.
7. Initialize a sample tuple (tuplex) with elements ("w", 3, "r", "e", "s", "o", "u", "r", "c", "e").
8. Call the get_elements function with the sample tuple.
9. Print the result obtained from the function, which includes the 4th element from the
beginning and the 4th element from the end.
4. IMPLEMENTATION
Task 1:
def count_even_odd(numbers):
even_count = 0
odd_count = 0
Task 02:
def get_elements(tuplex):
if len(tuplex) >= 4:
# Get the 4th element from the beginning
fourth_from_beginning = tuplex[3]
# Sample Input
tuplex = ("w", 3, "r", "e", "s", "o", "u", "r", "c", "e")
Task 01:
Task 02:
6. ANALYSIS AND DISCUSSION:
The lab report gives a quick rundown of what we learned in the "Introduction to Basic Operations
in Python." It covers the basics like assigning values to variables, doing math operations,
manipulating text, and using control structures for decision-making. The report stresses the
importance of hands-on learning through practical exercises and shows how these fundamental
skills are crucial for problem-solving. It also hints at how this basic knowledge sets the stage for
exploring more advanced topics in Python, making it a solid starting point for anyone diving into
programming with Python.