0% found this document useful (0 votes)
4 views4 pages

Build Function .Py

The document provides an overview of built-in functions in Python that can be used with dictionaries, including len(), type(), str(), dict(), max(), min(), sorted(), all(), any(), and sum(). Each function is explained with examples demonstrating its usage. The document highlights how to manipulate and interact with dictionary data effectively.

Uploaded by

rj0110865
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

Build Function .Py

The document provides an overview of built-in functions in Python that can be used with dictionaries, including len(), type(), str(), dict(), max(), min(), sorted(), all(), any(), and sum(). Each function is explained with examples demonstrating its usage. The document highlights how to manipulate and interact with dictionary data effectively.

Uploaded by

rj0110865
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

In Python, dictionaries are a built-in data structure, and they come with various built-in functions

(technically, methods) that allow you to manipulate and interact with dictionary data. Here are the
main built-in functions that are applicable to dictionaries:

Common Python Built-in Functions Used with Dictionaries

1. len()
Returns the number of items (key-value pairs) in the dictionary.

person = {"name": "John", "age": 30, "city": "New York"}

print(len(person)) # Output: 3

type()

Returns the type of the dictionary object.

person = {"name": "John", "age": 30}

print(type(person)) # Output: <class 'dict'>

str()
Converts the dictionary to its string representation

person = {"name": "John", "age": 30}

print(str(person)) # Output: "{'name': 'John', 'age': 30}"

dict()
This is the built-in Python constructor to create a dictionary.

new_dict = dict(name="John", age=30)

print(new_dict) # Output: {'name': 'John', 'age': 30}

max()
Returns the maximum key in the dictionary. By default, it compares the keys (not values).

data = {1: "a", 2: "b", 3: "c"}

print(max(data)) # Output: 3

min()
Returns the minimum key in the dictionary.

data = {1: "a", 2: "b", 3: "c"}

print(min(data)) # Output: 1

sorted()
Returns a sorted list of the dictionary's keys.

data = {3: "c", 1: "a", 2: "b"}

print(sorted(data)) # Output: [1, 2, 3]

all()
Returns True if all keys in the dictionary are truthy, or if the dictionary is empty.
data = {1: "a", 2: "b", 0: "c"} # 0 is falsy

print(all(data)) # Output: False

any()
Returns True if at least one key in the dictionary is truthy. If the dictionary is empty, it returns False.

data = {0: "a", 0: "b", 3: "c"} # 3 is truthy

print(any(data)) # Output: True

sum()
Returns the sum of the keys (only works if keys are numeric).

person = {"name": "John", "age": 30, "city": "New York"}

# Get the length of the dictionary (number of key-value pairs)

print(len(person)) # Output: 3

# Get the type of the object

print(type(person)) # Output: <class 'dict'>

# Convert the dictionary to a string representation

print(str(person)) # Output: "{'name': 'John', 'age': 30, 'city': 'New York'}"

# Create a dictionary using the dict() constructor

new_dict = dict(name="Alice", age=25)

print(new_dict) # Output: {'name': 'Alice', 'age': 25}

# Get the maximum key

data = {1: "a", 2: "b", 3: "c"}

print(max(data)) # Output: 3

# Get the minimum key

print(min(data)) # Output: 1

# Sort the dictionary keys

print(sorted(data)) # Output: [1, 2, 3]


# Check if all keys are truthy

print(all(data)) # Output: True

# Check if any key is truthy

print(any(data)) # Output: True

# Get the sum of the keys

print(sum(data)) # Output: 6

Example Summary:

person = {"name": "John", "age": 30, "city": "New York"}

# Get the length of the dictionary (number of key-value pairs)

print(len(person)) # Output: 3

# Get the type of the object

print(type(person)) # Output: <class 'dict'>

# Convert the dictionary to a string representation

print(str(person)) # Output: "{'name': 'John', 'age': 30, 'city': 'New York'}"

# Create a dictionary using the dict() constructor

new_dict = dict(name="Alice", age=25)

print(new_dict) # Output: {'name': 'Alice', 'age': 25}

# Get the maximum key

data = {1: "a", 2: "b", 3: "c"}

print(max(data)) # Output: 3
# Get the minimum key

print(min(data)) # Output: 1

# Sort the dictionary keys

print(sorted(data)) # Output: [1, 2, 3]

# Check if all keys are truthy

print(all(data)) # Output: True

# Check if any key is truthy

print(any(data)) # Output: True

# Get the sum of the keys

print(sum(data)) # Output: 6

You might also like