Python functions are a fundamental building block of Python programming. They allow you to encapsulate blocks of code into reusable components. Here's a brief overview of some common Python functions.
Common Python Functions
print()
: Outputs the specified message to the console.print("Hello, World!")
len()
: Returns the length of an object.len([1, 2, 3, 4, 5])
returns5
range()
: Returns a sequence of numbers, starting from 0 by default, and increments by 1 (also can be specified).range(5)
returns[0, 1, 2, 3, 4]
sum()
: Returns the sum of the numbers in an iterable.sum([1, 2, 3, 4, 5])
returns15
Useful Functions
input()
: Prompts the user to input some text.name = input("Enter your name: ")
open()
: Opens a file and returns a file object.file = open("example.txt", "r")
split()
: Splits a string into a list where each word is a list item."hello world".split(" ")
returns["hello", "world"]
For more information on Python functions, check out our Python Functions Guide.