Objective

In this unit, we will explore Python's built-in functions. Python comes with a wide array of functions built into the language that can be used to perform various tasks, from simple ones like printing output to the console, to more complex ones like converting data types. By the end of this unit, you will understand how to use some of the most commonly used built-in functions in Python.

Introduction to Python's Built-in Functions

Python comes with a set of functions that are always available for use. These are known as built-in functions. You've already seen some of these in action, such as print() and len().

Python's built-in functions are a part of the Python Standard Library, and they're always available for you to use in your programs. You don't need to import any special modules to use these functions; you can just call them directly in your code.

You can find the full list of Python's built-in functions in the Python documentation. This list includes not only the function names, but also descriptions of what they do, their syntax, and examples of how to use them. It's a great resource to refer to when you're writing Python code.

In addition to the Python documentation, you can also use the help() function to get information about any function, including built-in functions. Just pass the function name (without parentheses) as an argument to help(), and it will print out a description of the function, its syntax, and sometimes even examples of how to use it. For example, to get help on the print function, you would write help(print).

Remember, Python's built-in functions are designed to help you perform common tasks, so they can be a big help in your coding projects. Don't hesitate to use them when you need them!

Commonly Used Built-in Functions

Here are some of the most commonly used built-in functions in Python:

  • print(): This function is used to print the specified message to the screen. The message can be a string, or any other object, the object will be converted into a string before written to the screen.
print("Hello, World!")
  • len(): This function returns the number of items in an object. The argument can be a sequence (such as a string, bytes, tuple, list, or range) or a collection (such as a dictionary, set, or frozen set).
print(len("Hello, World!"))  # Prints: 12
  • type(): This function returns the type of the specified object.
print(type("Hello, World!"))  # Prints: <class 'str'>
  • input(): This function allows user input. It pauses program execution and waits for the user to enter some text. Once the user presses Enter, the function returns the text entered by the user as a string.
name = input("Enter your name: ")
print(f"Hello, {name}!")

Type Conversion Functions

Python also provides several functions to convert values from one data type to another. These functions return a new object representing the converted value.

  • int(): This function converts the specified value into an integer.
print(int("123"))  # Prints: 123
  • float(): This function converts the specified value into a floating point number.
print(float("123.45"))  # Prints: 123.45
  • str(): This function converts the specified value into a string.
print(str(123))  # Prints: "123"

Project: Use Built-in Functions to Enhance Your Turtle Drawing Program

In this project, you'll use built-in functions to enhance the turtle drawing program you created in the previous unit. Here's an example:

import turtle

# Create a new turtle screen and set its background color
screen = turtle.Screen()
screen.bgcolor("white")

# Initialize a turtle object
t = turtle.Turtle()

# Functions to move the turtle
def move_forward():
    t.forward(100)

def move_backward():
    t.backward(100)

def turn_left():
    t.left(90)

def turn_right():
    t.right(90)

def move_to(x, y):
    t.goto(x, y)

def change_color():
    color = input("Enter a color: ")
    t.color(color)

# Bind the functions to the arrow keys
screen.onkey(move_forward, "Up")
screen.onkey(move_backward, "Down")
screen.onkey(turn_left, "Left")
screen.onkey(turn_right, "Right")
screen.onkey(change_color, "c")

# Bind the function to mouse clicks
screen.onscreenclick(move_to)

# Start listening for key presses and mouse clicks
screen.listen()

# Wait until the window is closed
turtle.done()

In this program, we've added a new function change_color that uses the input function to ask the user to enter a color. This function then changes the color of the turtle to the entered color. We've bound this function to the "c" key, so now when the user presses "c", they will be prompted to enter a color, and the turtle's color will change accordingly.

Try to modify this program to use more built-in functions. For example, you could use the len function to limit the length of the color string, or the str function to convert user input to a string.

In the next unit, we'll look at string operations and explore how we can manipulate and format strings in Python.