Page 106 - KEC Khaitan C8 Flipbook
P. 106
You will get the following output:
Output
3
Computer
40
LIST FUNCTIONS AND METHODS
Some commonly used functions and methods of List are as follows:
len(): This function returns the number of items in a list.
list(): This function converts an iterable (like a tuple, string, or generator) into a list.
max(): This function returns the largest item in the list.
min(): This function returns the smallest item in the list.
sum(): This function returns the sum of all items in the list (useful for lists of numbers).
sorted(): This function returns a new list containing all items from the original list in sorted
order without modifying the original list.
reversed(): This function returns an iterator that accesses the given list in reverse order.
append(x): This function adds item x to the end of the list.
extend(iterable): This function appends all items from the given iterable to the list.
insert(i, x): This function inserts item x at index i.
remove(x): This function removes the first occurrence of x from the list.
pop([i]): This function removes and returns the item at index i. If no index is specified, it removes
and returns the last item.
clear(): This function removes all items from the list, leaving it empty.
Program15.py
File Edit Format Run Options Window Help
# Starting with a list of numbers.
numbers = [3, 1, 4, 1, 5, 9, 2, 6]
print("Original list:", numbers)
# List's Functions
# use of len() fucntion
length = len(numbers)
print("Length of list (len):", length)
# use of list() fucntion
tuple_data = (10, 20, 30)
new_list = list(tuple_data)
print("Converted tuple to list (list):", new_list)
104 Premium Edition-VIII

