0% found this document useful (0 votes)
12 views2 pages

Practical New File

The document provides an overview of list operations in Python, specifically the append() and remove() methods, including their syntax and examples. It also covers statistical functions using the statistics library to calculate mean, median, and mode with relevant definitions and examples. Additionally, it includes practice questions to reinforce the concepts discussed.

Uploaded by

Astha Ghosh
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)
12 views2 pages

Practical New File

The document provides an overview of list operations in Python, specifically the append() and remove() methods, including their syntax and examples. It also covers statistical functions using the statistics library to calculate mean, median, and mode with relevant definitions and examples. Additionally, it includes practice questions to reinforce the concepts discussed.

Uploaded by

Astha Ghosh
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

1.

AI Practical – List Operations


1. append() – Add to the End of the List

• Purpose: Adds a new element at the end of the list.


• Syntax:
• list_name.append(item)
• Example:
• fruits = ["apple", "banana"]
• [Link]("cherry")
• print(fruits) # ["apple", "banana", "cherry"]

Only one item can be appended at a time.

2. remove() – Delete by Value

• Purpose: Removes the first occurrence of the specified element.


• Syntax:
• list_name.remove(item)
• Example:
• fruits = ["apple", "banana", "cherry", "banana"]
• [Link]("banana")
• print(fruits) # ["apple", "cherry", "banana"]

If the item is not in the list, remove() will give an error.

Practice Questions

1. Create a list colors = ["red", "green"] and add "blue" to it.


2. Create a list animals = ["cat", "dog", "cat"] and remove "cat".
3. Start with numbers = [1, 2, 3] → append 4 and then remove 2.
4. What happens if you try to remove "pink" from colors = ["red", "blue"]?

2. AI Practical – Mean, Median & Mode


We use the statistics library in Python to find the mean, median, and mode of a list of
numbers.

1. Mean (Average)
• Definition: The sum of all values divided by the total number of values.
• Example:

import statistics
data = [10, 20, 30, 40]
print(“mean= ”,[Link](data))

Here, (10 + 20 + 30 + 40) ÷ 4 = 25

2. Median (Middle Value)

• Definition: The middle value when the numbers are arranged in order.
• If there are odd numbers, it’s the exact middle.
• If there are even numbers, it’s the average of the two middle values.
• Example:

import statistics
data = [10, 20, 30, 40, 50]
print(“median= ”,[Link](data)) # Output: 30 (middle number)

3. Mode (Most Frequent Value)

• Definition: The value that appears most often in the list.


• Example:

import statistics
data = [10, 20, 20, 30, 40]
print(“mode= ”,[Link](data)) )) # Output: 20

20 appears twice, more than any other number.

Full Example:
import statistics

data = [10, 20, 20, 30, 40]

print("Mean:", [Link](data)) # 24
print("Median:", [Link](data)) # 20
print("Mode:", [Link](data)) # 20

You might also like