[go: up one dir, main page]

0% found this document useful (0 votes)
5 views6 pages

Functions

Uploaded by

gtecstudent795
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)
5 views6 pages

Functions

Uploaded by

gtecstudent795
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
You are on page 1/ 6

1.

append()

• Adds an item to the end of the list.

• Syntax: list.append(item)

• Example:

python

CopyEdit

fruits = ["apple", "banana"]

fruits.append("cherry")

print(fruits) # ['apple', 'banana', 'cherry']

2. extend()

• Adds all elements of an iterable (like a list, tuple, etc.) to the end of the list.

• Syntax: list.extend(iterable)

• Example:

python

CopyEdit

fruits = ["apple", "banana"]

fruits.extend(["cherry", "date"])

print(fruits) # ['apple', 'banana', 'cherry', 'date']

3. insert()

• Inserts an item at a given position in the list.

• Syntax: list.insert(index, item)

• Example:

python

CopyEdit

fruits = ["apple", "banana"]

fruits.insert(1, "cherry")

print(fruits) # ['apple', 'cherry', 'banana']

4. remove()

• Removes the first occurrence of an item from the list.

• Syntax: list.remove(item)

• Example:
python

CopyEdit

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

fruits.remove("banana")

print(fruits) # ['apple', 'cherry']

5. pop()

• Removes and returns an item from a given index. If no index is specified, it removes and
returns the last item.

• Syntax: list.pop(index)

• Example:

python

CopyEdit

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

popped_item = fruits.pop(1)

print(fruits) # ['apple', 'cherry']

print(popped_item) # 'banana'

6. clear()

• Removes all items from the list, leaving it empty.

• Syntax: list.clear()

• Example:

python

CopyEdit

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

fruits.clear()

print(fruits) # []

7. index()

• Returns the index of the first occurrence of a specified item.

• Syntax: list.index(item)

• Example:

python

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

index = fruits.index("banana")

print(index) # 1

8. count()

• Returns the number of times a specified item appears in the list.

• Syntax: list.count(item)

• Example:

python

CopyEdit

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

count = fruits.count("banana")

print(count) # 2

9. sort()

• Sorts the items in the list in ascending order. For descending order, use reverse=True.

• Syntax: list.sort(reverse=False)

• Example:

python

CopyEdit

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

fruits.sort()

print(fruits) # ['apple', 'banana', 'cherry']

10. reverse()

• Reverses the order of the items in the list.

• Syntax: list.reverse()

• Example:

python

CopyEdit

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

fruits.reverse()

print(fruits) # ['cherry', 'banana', 'apple']

11. copy()
• Returns a shallow copy of the list.

• Syntax: list.copy()

• Example:

python

CopyEdit

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

fruits_copy = fruits.copy()

print(fruits_copy) # ['apple', 'banana', 'cherry']

12. join() (String Method, often used with lists of strings)

• Joins all elements of a list (of strings) into a single string.

• Syntax: 'separator'.join(list)

• Example:

python

CopyEdit

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

result = ", ".join(fruits)

print(result) # 'apple, banana, cherry'

13. list()

• Converts an iterable (like a string, tuple, or set) into a list.

• Syntax: list(iterable)

• Example:

python

CopyEdit

string = "hello"

list_string = list(string)

print(list_string) # ['h', 'e', 'l', 'l', 'o']

14. del (delete statement)

• Removes an element at a specific index or the entire list.

• Syntax: del list[index] or del list

• Example:

python
CopyEdit

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

del fruits[1]

print(fruits) # ['apple', 'cherry']

15. max()

• Returns the largest item in a list.

• Syntax: max(list)

• Example:

python

CopyEdit

numbers = [1, 2, 3, 4, 5]

largest = max(numbers)

print(largest) # 5

16. min()

• Returns the smallest item in a list.

• Syntax: min(list)

• Example:

python

CopyEdit

numbers = [1, 2, 3, 4, 5]

smallest = min(numbers)

print(smallest) # 1

17. sum()

• Returns the sum of all elements in the list (useful with numeric lists).

• Syntax: sum(list)

• Example:

python

CopyEdit

numbers = [1, 2, 3, 4, 5]

total = sum(numbers)

print(total) # 15
18. any()

• Returns True if any element in the list evaluates to True. Otherwise, returns False.

• Syntax: any(list)

• Example:

python

CopyEdit

values = [0, False, 3, None]

print(any(values)) # True

19. all()

• Returns True if all elements in the list evaluate to True. Otherwise, returns False.

• Syntax: all(list)

• Example:

python

CopyEdit

values = [True, True, False]

print(all(values)) # False

20. enumerate()

• Adds a counter to an iterable and returns it as an enumerate object.

• Syntax: enumerate(iterable)

• Example:

python

CopyEdit

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

for index, fruit in enumerate(fruits):

print(index, fruit)

You might also like