Welcome, explorer! Are you ready to venture into the realm of Python's built-in functions? These comprehensive tools can streamline your code, saving both time and effort. Our mission is to familiarize ourselves with some of Python's straightforward built-in functions. Let's embark on this journey!
Much like a built-in navigational system guiding astronauts, Python's built-in functions serve as accessible tools to navigate through coding tasks at any time. They operate as our guiding stars through the cosmic realm of programming.
Let's chart a course through Python's built-in functions:
The len()
function: This function counts elements in a list, set, tuple, or any data structure, much like counting candies in a box:
Python1candies_list = ['star1', 'star2', 'star3', 'star4'] 2print(len(candies_list)) # Output: 4 3 4candies_set = {'star1', 'star2', 'star3', 'star4'} 5print(len(candies_set)) # Output: 4 6 7candies_tuple = ('star1', 'star2', 'star3', 'star4') 8print(len(candies_tuple)) # Output: 4
The abs()
function: Functioning like a device determining a spaceship's absolute distance in light years from a specific point in space, this function gives the absolute value of a number.
Python1print(abs(-10)) # Output: 10 2print(abs(7)) # Output: 7
The round()
function: Similar to rounding off estimated distances or times, this function rounds a number to the nearest integer.
Python1print(round(10.675)) # Output: 11 2print(round(10.365)) # Output: 10 3print(round(7.5)) # Output: 8
The min()
and max()
functions: These functions find the smallest and largest items in a list, set, or tuple, just as one might measure the smallest and largest star masses in a galaxy:
Python1stars_list = [2.1, 3.2, 1.0, 5.4, 4.4] # Sizes of stars 2print(min(stars_list)) # Output: 1.0 3print(max(stars_list)) # Output: 5.4 4 5stars_set = {2.1, 3.2, 1.0, 5.4, 4.4} # Sizes of stars 6print(min(stars_set)) # Output: 1.0 7print(max(stars_set)) # Output: 5.4 8 9stars_tuple = (2.1, 3.2, 1.0, 5.4, 4.4) # Sizes of stars 10print(min(stars_tuple)) # Output: 1.0 11print(max(stars_tuple)) # Output: 5.4
The sum()
function: Analogous to summing up the masses of materials collected during space exploration, this function sums up the elements in a list, set, or tuple:
Python1elements_mass = [5, 3, 2, 6] # Mass here in kilograms 2print(sum(elements_mass)) # Output: 16 3 4elements_mass = {5, 3, 2, 6} # Mass here in kilograms 5print(sum(elements_mass)) # Output: 16 6 7elements_mass = (5, 3, 2, 6) # Mass here in kilograms 8print(sum(elements_mass)) # Output: 16
The sorted()
function: This function sorts items in a list in the same way artifacts discovered during a space expedition might be chronologically organized:
Python1artifacts = [50, 100, 25, 75] # Antique value here in arbitrary unit 2print(sorted(artifacts)) # Output: [25, 50, 75, 100]
You can also sort elements in a reversed order:
Python1print(sorted(artifacts, reverse=True)) # Output: [100, 75, 50, 25]
You've learned to use Python’s built-in functions len()
, abs()
, round()
, min()
, max()
, sum()
, and sorted()
. Keep practicing with these functions to improve your programming skills. You're now ready for quizzes and exercises designed to further hone these skills. Keep going and keep exploring!