Built-In Functions in Python

Python comes with a lot of functionality inclued, so let's have a look what's available.

Built-In Functions in Python

Python comes with a lot of functionality inclued, so let's have a look what's available.

Lesson Objectives

Lesson Objectives

  • What is a function ?

  • Built-In Functions in Python

  • How to use them?

Are you Ready?

Lesson Summary

Lesson Summary

Built-In Functions in Python

Here is a Table of all Built-In Functions in Python that are sorted so you know what you need to learn and what can be ignored completely.

Built-In Functions

All

✨Essential

Regular

📦OOP

🔹Basic

❌Ignore

enumerate()

Adds counter to an iterable by pairing each element with its position index, creating a sequence of tuples.

# Materials list
materials = ["concrete", "steel", "glass", "wood", "brick"]

# Add Iteration Count with enumerate()
for n, material in enumerate(materials):
    print(f"{n}. {material}")
  
# 0. concrete
# 1. steel
# 2. glass
# 3. wood
# 4. brick

isinstance()

Checks if an object is an instance of a specified class or type.

x = 42
y = "hello"
z = [1, 2, 3]

print(isinstance(x, int))     # True
print(isinstance(y, str))     # True
print(isinstance(z, list))    # True
print(isinstance(x, float))   # False


# Check against multiple types
print(isinstance(x, (int, float)))  # True - x is an int or float


# Check for subclasses
class Animal:
    pass

class Dog(Animal):
    pass

fido = Dog()
print(isinstance(fido, Dog))     # True
print(isinstance(fido, Animal))  # True - subclass relationship

len()

Returns the number of items in an object.

# Length of a string (number of characters)
text = "hello world"
print(len(text))  # 11

# Length of a list (number of elements)
numbers = [10, 20, 30, 40, 50]
print(len(numbers))  # 5

# Length of a dictionary (number of key-value pairs)
person = {"name": "Erik", 
          "age": 29, 
          "city": "Vienna"}
print(len(person))  # 3

open()

Creates a file object, allowing you to read from or write to files using different modes ('r' for read, 'w' for write, 'a' for append).

# Recommended way using 'with' context-manager for auto-closing file.

# Read
with open('example.txt', 'r') as file:
   content = file.read()
   print(content)

# Write
with open('example.txt', 'w') as file:
   file.write('Hello, Python!')


# Basic file reading ( NOT RECOMMENDED as file might get corrupted is not closed.)
file = open('example.txt', 'r')
content = file.read()
print(content)
file.close()

print()

Outputs text to the console, with options to separate arguments and control line endings.

print("Hello World.")

range()

generates a sequence of numbers with optional start, stop, and step parameters.

# Creating a range of numbers
print(list(range(5)))       # Output: [0, 1, 2, 3, 4]
print(list(range(2, 7)))    # Output: [2, 3, 4, 5, 6]
print(list(range(1, 10, 2))) # Output: [1, 3, 5, 7, 9]


# Often used in Loops
for n in range(10):
    print(f'Attemprt # {n}')

sorted()

Returns a new sorted list from the given iterable, with optional reverse sorting.

# Sorted - Numeric
numbers = [5, 2, 8, 1, 9]
print(sorted(numbers))               # Output: [1, 2, 5, 8, 9]
print(sorted(numbers, reverse=True)) # Output: [9, 8, 5, 2, 1]

# Sorted - Alphabetic
materials = ['Steel', 'Concrete', 'Wood', 'Brick']
print(sorted(materials))        # Output: ['Brick', 'Concrete', 'Steel', 'Wood']


# Sorting a dictionary by value without lambda or imports
materials = {'Steel': 120, 
             'Concrete': 75, 
             'Wood': 40, 
             'Brick': 95}

# Define a function to return the second element (value)
def get_value(item):
    return item[1]

sorted_materials = sorted(materials.items(), key=get_value)

print(sorted_materials)  # Output: [('Wood', 40), ('Concrete', 75), ('Brick', 95), ('Steel', 120)]

type()

Returns the type of an object, helping identify its data type or class, often use for debugging and understanding variables.

print(type('Hello World')) # <class 'str'>
print(type(69)) # <class 'int'>
print(type([1,2,3])) # <class 'list'>
print(type(None)) # <class 'NoneType'>

zip()

Creates an iterator of tuples by combining elements from multiple iterables, useful for pairing related architectural data.

materials = ['Concrete', 'Steel', 'Wood']
strengths = [25, 50, 15]

# Combining materials and their strengths
zip_materials      = zip(materials, strengths) #<zip object at 0x7f59a148dcc0>
material_strengths = list(zip_materials)
print(material_strengths)  # Output: [('Concrete', 25), ('Steel', 50), ('Wood', 15)]

Built-In Functions

All

✨Essential

Regular

📦OOP

🔹Basic

❌Ignore

enumerate()

Adds counter to an iterable by pairing each element with its position index, creating a sequence of tuples.

# Materials list
materials = ["concrete", "steel", "glass", "wood", "brick"]

# Add Iteration Count with enumerate()
for n, material in enumerate(materials):
    print(f"{n}. {material}")
  
# 0. concrete
# 1. steel
# 2. glass
# 3. wood
# 4. brick

isinstance()

Checks if an object is an instance of a specified class or type.

x = 42
y = "hello"
z = [1, 2, 3]

print(isinstance(x, int))     # True
print(isinstance(y, str))     # True
print(isinstance(z, list))    # True
print(isinstance(x, float))   # False


# Check against multiple types
print(isinstance(x, (int, float)))  # True - x is an int or float


# Check for subclasses
class Animal:
    pass

class Dog(Animal):
    pass

fido = Dog()
print(isinstance(fido, Dog))     # True
print(isinstance(fido, Animal))  # True - subclass relationship

len()

Returns the number of items in an object.

# Length of a string (number of characters)
text = "hello world"
print(len(text))  # 11

# Length of a list (number of elements)
numbers = [10, 20, 30, 40, 50]
print(len(numbers))  # 5

# Length of a dictionary (number of key-value pairs)
person = {"name": "Erik", 
          "age": 29, 
          "city": "Vienna"}
print(len(person))  # 3

open()

Creates a file object, allowing you to read from or write to files using different modes ('r' for read, 'w' for write, 'a' for append).

# Recommended way using 'with' context-manager for auto-closing file.

# Read
with open('example.txt', 'r') as file:
   content = file.read()
   print(content)

# Write
with open('example.txt', 'w') as file:
   file.write('Hello, Python!')


# Basic file reading ( NOT RECOMMENDED as file might get corrupted is not closed.)
file = open('example.txt', 'r')
content = file.read()
print(content)
file.close()

print()

Outputs text to the console, with options to separate arguments and control line endings.

print("Hello World.")

range()

generates a sequence of numbers with optional start, stop, and step parameters.

# Creating a range of numbers
print(list(range(5)))       # Output: [0, 1, 2, 3, 4]
print(list(range(2, 7)))    # Output: [2, 3, 4, 5, 6]
print(list(range(1, 10, 2))) # Output: [1, 3, 5, 7, 9]


# Often used in Loops
for n in range(10):
    print(f'Attemprt # {n}')

sorted()

Returns a new sorted list from the given iterable, with optional reverse sorting.

# Sorted - Numeric
numbers = [5, 2, 8, 1, 9]
print(sorted(numbers))               # Output: [1, 2, 5, 8, 9]
print(sorted(numbers, reverse=True)) # Output: [9, 8, 5, 2, 1]

# Sorted - Alphabetic
materials = ['Steel', 'Concrete', 'Wood', 'Brick']
print(sorted(materials))        # Output: ['Brick', 'Concrete', 'Steel', 'Wood']


# Sorting a dictionary by value without lambda or imports
materials = {'Steel': 120, 
             'Concrete': 75, 
             'Wood': 40, 
             'Brick': 95}

# Define a function to return the second element (value)
def get_value(item):
    return item[1]

sorted_materials = sorted(materials.items(), key=get_value)

print(sorted_materials)  # Output: [('Wood', 40), ('Concrete', 75), ('Brick', 95), ('Steel', 120)]

type()

Returns the type of an object, helping identify its data type or class, often use for debugging and understanding variables.

print(type('Hello World')) # <class 'str'>
print(type(69)) # <class 'int'>
print(type([1,2,3])) # <class 'list'>
print(type(None)) # <class 'NoneType'>

zip()

Creates an iterator of tuples by combining elements from multiple iterables, useful for pairing related architectural data.

materials = ['Concrete', 'Steel', 'Wood']
strengths = [25, 50, 15]

# Combining materials and their strengths
zip_materials      = zip(materials, strengths) #<zip object at 0x7f59a148dcc0>
material_strengths = list(zip_materials)
print(material_strengths)  # Output: [('Concrete', 25), ('Steel', 50), ('Wood', 15)]

Homework

Homework

Go through all ✨Essential Functions and try them out yourself.

It's great to see how other use them, but it's 10 times better when you try it yourself and experiment with it so you remember how it works.

Feel free to share in the community what is your favorite so far.

⌨️ Happy Coding!

Questions

Questions

Should I learn all functions?

Should I learn all functions?

When do I need OOP functions?

When do I need OOP functions?

Should I use ChatGPT as beginner?

Should I use ChatGPT as beginner?

Discuss Lesson

P.S. Sometimes this chat might experience connection issues.

Use Discord App for best experience.

Discuss Lesson

P.S. Sometimes this chat might experience connection issues.

Use Discord App for best experience.

Discuss Lesson

P.S. Sometimes this chat might experience connection issues.

Use Discord App for best experience.

Unlock Community

The pyRevit Hackers Community is only available with pyRevit Hackers Bundle.
Upgrade Here to Get Access to the community and all pyRevit Courses.

Use coupon code "upgrade" to get 150EUR Discount as a member.

⌨️ Happy Coding!

Unlock Community

The pyRevit Hackers Community is only available with pyRevit Hackers Bundle.
Upgrade Here to Get Access to the community and all pyRevit Courses.

Use coupon code "upgrade" to get 150EUR Discount as a member.

⌨️ Happy Coding!

Unlock Community

The pyRevit Hackers Community is only available with pyRevit Hackers Bundle.
Upgrade Here to Get Access to the community and all pyRevit Courses.

Use coupon code "upgrade" to get 150EUR Discount as a member.

⌨️ Happy Coding!

© 2023-2025 EF Learn Revit API

© 2023-2025 EF Learn Revit API

© 2023-2025 EF Learn Revit API