Class 9 AI Chapter - Python Topic - Function - Arvindzeclass - NCERT Solutions

Post Top Ad

Wednesday, July 30, 2025

Class 9 AI Chapter - Python Topic - Function

 

Class 9 AI

Class 9 Artificial Intelligence Code 417 Solutions
Session 2025 - 26

Artificial Intelligence class 9 code 417 solutions PDF and Question Answer are provided and class 9 AI book code 417 solutions. Class 9 AI Syllabus. Class 9 AI Book. First go through Artificial Intelligence (code 417 class 9 with solutions) and then solve MCQs and Sample Papers, information technology code 417 class 9 solutions pdf is the vocational subject.


--------------------------------------------------

Chapter - Introduction to Python 
Other Topics

-------------------------------------------------


Comparing Lists

In Python, comparing lists means checking if two or more lists are equal, or how they relate to each other in terms of content or order. Here’s a detailed explanation with examples:

1. Comparing if Lists Are Equal

Two lists are considered equal if:

  • They have the same elements
  • In the same order
list1 = [1, 2, 3]
list2 = [1, 2, 3]
list3 = [3, 2, 1]

print(list1 == list2)  # True
print(list1 == list3)  # False (order matters)


✅ 2. Using != to Check if Lists Are Different

list1 = [1, 2, 3]
list2 = [4, 5, 6]

print(list1 != list2)  # True


✅ 3. Checking if Two Lists Have the Same Elements (Ignore Order)

Use sorted() or convert to set:

list1 = [1, 2, 3]
list2 = [3, 2, 1]

# Method 1: Using sorted
print(sorted(list1) == sorted(list2))  # True

# Method 2: Using set
print(set(list1) == set(list2))  # True (but ignores duplicates)


4. Comparing Element by Element

list1 = [1, 2, 3]
list2 = [1, 4, 3]

for a, b in zip(list1, list2):
    print(a == b)

Output:

True
False
True


5. Using all() with zip() to Check Element-wise Equality

list1 = [1, 2, 3]
list2 = [1, 2, 3]

result = all(a == b for a, b in zip(list1, list2))
print(result)  # True


6. Check if One List Is a Sublist of Another

list1 = [1, 2, 3, 4, 5]
sublist = [2, 3]

# Convert to string or use slicing
print(str(sublist)[1:-1] in str(list1))  # Not reliable
# Better way:
found = False
for i in range(len(list1) - len(sublist) + 1):
    if list1[i:i+len(sublist)] == sublist:
        found = True
        break

print(found)  # True


💡 Summary Table:

Operation Code Example Output
Equal lists list1 == list2 True/False
Different lists list1 != list2 True/False
Same elements (ignore order) set(list1) == set(list2) True/False
Sorted comparison sorted(list1) == sorted(list2) True/False
Element-wise compare zip(list1, list2) + loop Element by element


Function in Python

In Python, a function is a reusable block of code that performs a specific task. You can call a function whenever you want to execute that task. Python has built-in functions like print(), and also allows users to create their own functions using def.

📚 List

A list in Python is a collection of items (elements) that is ordered, changeable (mutable), and allows duplicate values.
Example:

fruits = ["apple", "banana", "cherry"]


🧠 List Functions in Python

1. append()

Adds an item to the end of the list.

fruits = ["apple", "banana"]
fruits.append("cherry")
print(fruits)  # Output: ['apple', 'banana', 'cherry']

2. extend()

Adds multiple items to the end of the list.

fruits = ["apple", "banana"]
fruits.extend(["cherry", "orange"])
print(fruits)  # Output: ['apple', 'banana', 'cherry', 'orange']

3. insert(index, value)

Inserts an item at a specific index.

fruits = ["apple", "banana"]
fruits.insert(1, "cherry")
print(fruits)  # Output: ['apple', 'cherry', 'banana']

4. remove(value)

Removes the first occurrence of the specified value.

fruits = ["apple", "banana", "cherry", "banana"]
fruits.remove("banana")
print(fruits)  # Output: ['apple', 'cherry', 'banana']

5. pop(index)

Removes the item at the given index and returns it. If no index is given, it removes the last item.

fruits = ["apple", "banana", "cherry"]
fruits.pop(1)
print(fruits)  # Output: ['apple', 'cherry']


✏️ Modifying Existing Values in a List

You can modify elements by directly accessing their index.

fruits = ["apple", "banana", "cherry"]
fruits[1] = "orange"
print(fruits)  # Output: ['apple', 'orange', 'cherry']


🔴 Remove Elements from a List

Sometimes you may want to delete an element:

  • Because it's no longer needed
  • To avoid duplicates
  • To update or clean your data


Methods to Remove Elements from a List

1. remove(value)

  • Removes the first occurrence of the specified value.
  • Raises an error if the value is not found.
fruits = ["apple", "banana", "cherry", "banana"]
fruits.remove("banana")
print(fruits)  # Output: ['apple', 'cherry', 'banana']

✅ Removes only the first 'banana', not all.


💥 Error Example:

fruits = ["apple", "banana"]
fruits.remove("orange")  # ❌ ValueError: list.remove(x): x not in list

2. pop(index)

  • Removes the element at the specified index.
  • If no index is given, removes the last item.
  • Returns the removed item.
fruits = ["apple", "banana", "cherry"]
removed_item = fruits.pop(1)
print(removed_item)  # Output: 'banana'
print(fruits)        # Output: ['apple', 'cherry']

💥 Error Example:

fruits = ["apple"]
fruits.pop(5)  # ❌ IndexError: pop index out of range

3. del statement

  • Deletes an element by index or deletes a slice (multiple elements).
  • Does not return the value.
fruits = ["apple", "banana", "cherry", "orange"]
del fruits[1]         # Removes 'banana'
print(fruits)         # Output: ['apple', 'cherry', 'orange']

del fruits[0:2]       # Removes 'apple' and 'cherry'
print(fruits)         # Output: ['orange']

✅ Use del when you want to remove items by position or slice.

4. clear()

  • Removes all elements from the list.
  • The list becomes empty [].
fruits = ["apple", "banana", "cherry"]
fruits.clear()
print(fruits)  # Output: []

✅ Use this when you want to reset the list.


🧠 Summary Table

Method Removes by Returns Value Errors if not found? Example
remove() Value No ✅ Yes (if value not found) fruits.remove("banana")
pop() Index (or last) ✅ Yes ✅ Yes (if index is wrong) fruits.pop(2)
del Index or slice No ✅ Yes (if index wrong) del fruits[0:2]
clear() All elements No ❌ No error fruits.clear()



No comments:

Post a Comment

Post Top Ad