Common Built-in Functions: len(), type(), range(), etc.
Following our exploration of Operator Precedence and Associativity, this article introduces some of Python's most common and useful built-in functions. These functions are always available to you, without the need to import any modules.
📚 Prerequisites
A basic understanding of Python data types and variables.
🎯 Article Outline: What You'll Master
In this article, you will learn:
- ✅ How to use the
len()function to find the length of an object. - ✅ How to use the
type()function to determine the data type of a variable. - ✅ How to use the
range()function to generate a sequence of numbers. - ✅ Other useful built-in functions.
🧠 Section 1: len() - Finding the Length
The len() function returns the number of items in an object. It's commonly used with strings, lists, tuples, and dictionaries.
my_string = "Hello, World!"
print(len(my_string)) # Output: 13
my_list = [1, 2, 3, 4, 5]
print(len(my_list)) # Output: 5
💻 Section 2: type() - Checking the Data Type
The type() function returns the data type of an object. This is useful for debugging and understanding your code.
my_string = "Hello, World!"
print(type(my_string)) # Output: <class 'str'>
my_integer = 10
print(type(my_integer)) # Output: <class 'int'>
🛠️ Section 3: range() - Generating a Sequence of Numbers
The range() function generates a sequence of numbers. It's often used in for loops to repeat a block of code a specific number of times.
range(stop):
for i in range(5):
print(i) # Output: 0, 1, 2, 3, 4
range(start, stop):
for i in range(2, 5):
print(i) # Output: 2, 3, 4
range(start, stop, step):
for i in range(0, 10, 2):
print(i) # Output: 0, 2, 4, 6, 8
🔬 Section 4: Other Useful Built-in Functions
Here are a few other useful built-in functions:
abs(): Returns the absolute value of a number.max(): Returns the largest item in an iterable.min(): Returns the smallest item in an iterable.sum(): Returns the sum of all items in an iterable.round(): Rounds a number to a specified number of decimal places.
💡 Conclusion & Key Takeaways
You've now learned about some of Python's most common built-in functions. These functions are powerful tools that will help you write more concise and efficient code.
Let's summarize the key takeaways:
len(): Returns the length of an object.type(): Returns the data type of an object.range(): Generates a sequence of numbers.- Other Functions:
abs(),max(),min(),sum(), andround()are useful for working with numbers.
Challenge Yourself:
Write a script that uses the range() function to print the numbers from 1 to 10, and then uses the sum() function to print the sum of those numbers.
➡️ Next Steps
This article concludes our exploration of the absolute basics of Python. In the next series, we'll start learning about "Control Flow - Conditionals and Loops in Python".
Happy coding!
Glossary (Python Terms)
- Built-in Function: A function that is always available in Python, without the need to import any modules.
- Iterable: An object that can be looped over, such as a string, list, or tuple.