Python Advanced List Functions Cheat Sheet-2 with Examples

Python Advanced List Functions Cheat Sheet-2 with Examples




1. slice() Function

The slice() function is used to get a part (subsection) of a sequence (like list, string, tuple).

Example 1: Slice a list


my_list = [10, 20, 30, 40, 50]
sliced = my_list[slice(1, 4)]
print(sliced)

Output:


[20, 30, 40]

✅ Shorthand using [start:stop:step]


text = "Python"
print(text[0:4])  # 'Pyth'
print(text[::2])  # 'Pto'

Output:


Pyth
Pto

2. round() Function

The round() function rounds a number to the nearest integer or to a specified number of decimal places.

Example 1: Round to nearest integer


x = 4.6
print(round(x))

Output:


5

✅ Round to 2 decimal places:


y = 3.14159
print(round(y, 2))

Output:


3.14

3. max() Function

The max() function returns the largest item in an iterable or the largest of two or more arguments.

Example 1: Max in a list


nums = [5, 9, 2, 10]
print(max(nums))

Output:


10

✅ Max of direct values:


print(max(3, 8, 4))

Output:


8

4. min() Function

The min() function returns the smallest item in an iterable or the smallest of two or more arguments.

Example 1: Min in a tuple


values = (8, 3, 7, 1)
print(min(values))

Output:


1

✅ Min of direct values:


print(min(20, 10, 30))

Output:


10

5. sum() Function

The sum() function returns the sum of all items in an iterable (e.g., list or tuple).

Example 1: Sum of numbers in a list


numbers = [10, 20, 30]
print(sum(numbers))

Output:


60

✅ Sum with a starting value:


print(sum(numbers, 5))  # Starts from 5

Output:


65

6. abs() Function

The abs() function returns the absolute value of a number (i.e., it turns negative numbers into positive).

Example 1:


print(abs(-7))
print(abs(3.5))

Output:


7
3.5

7. ord() Function

The ord() function returns the Unicode code point (integer) of a single character.

Example 1:


print(ord('A'))
print(ord('z'))

Output:


65
122

⚠️ Only one character is allowed


# print(ord('AB')) → Error

8. chr() Function

The chr() function returns the character that corresponds to a Unicode code point.

Example 1:


print(chr(65))
print(chr(122))

Output:


A
z

9. divmod() Function

The divmod() function returns a tuple containing the quotient and the remainder when dividing two numbers.

Example 1:


result = divmod(10, 3)
print(result)

Output:


(3, 1)

✅ Unpack the result:


q, r = divmod(20, 7)
print("Quotient:", q)
print("Remainder:", r)

Output:


Quotient: 2
Remainder: 6

10. id() Function

The id() function returns the identity (memory address) of an object.

Example 1:


x = 100
print(id(x))

y = "hello"
print(id(y))

Output: (Memory addresses will vary)


4305788928
139821882088688

✅ Same ID means same object:


a = 5
b = a
print(id(a) == id(b))  # True

Output:


True

📚 Practice Questions:


📚 Related Topics:


📌 Bookmark this blog or follow for updates!

Python Logo

👍 Liked this post? Share it with friends or leave a comment below!

Previous Post Next Post

Contact Form