Lädt...


🔧 Essential Python Built-in Functions: Examples and Usage


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

Python, known for its simplicity and readability, comes with a rich set of built-in functions that simplify common programming tasks. These functions allow developers to perform various operations without having to write extensive code. In this article, we will explore some essential built-in functions in Python, including len(), max(), sum(), and others, along with practical examples to illustrate their usage.

1. len()

The len() function returns the number of items in an object, which can be a string, list, tuple, or any other iterable.

Example:

# Using len() with a list
my_list = [1, 2, 3, 4, 5]
print(len(my_list))  # Output: 5

# Using len() with a string
my_string = "Hello, World!"
print(len(my_string))  # Output: 13

2. max()

The max() function returns the largest item from an iterable or the largest of two or more arguments. It is useful for finding the maximum value in a list or tuple.

Example:

# Using max() with a list
numbers = [1, 3, 2, 5, 4]
print(max(numbers))  # Output: 5

# Using max() with multiple arguments
print(max(10, 20, 30))  # Output: 30

3. sum()

The sum() function computes the sum of all items in an iterable, such as a list or tuple. It can also take a second argument, which serves as the starting value.

Example:

# Using sum() with a list
numbers = [1, 2, 3, 4, 5]
print(sum(numbers))  # Output: 15

# Using sum() with a starting value
print(sum(numbers, 10))  # Output: 25 (15 + 10)

4. min()

The min() function returns the smallest item from an iterable or the smallest of two or more arguments.

Example:

# Using min() with a list
numbers = [1, 3, 2, 5, 4]
print(min(numbers))  # Output: 1

# Using min() with multiple arguments
print(min(10, 20, 30))  # Output: 10

5. sorted()

The sorted() function returns a new sorted list from the elements of any iterable, while the original iterable remains unchanged.

Example:

# Using sorted() with a list
numbers = [3, 1, 4, 1, 5, 9]
sorted_numbers = sorted(numbers)
print(sorted_numbers)  # Output: [1, 1, 3, 4, 5, 9]
print(numbers)  # Output: [3, 1, 4, 1, 5, 9] (original list is unchanged)

6. abs()

The abs() function returns the absolute value of a number, which is the non-negative value of the number without regard to its sign.

Example:

print(abs(-5))  # Output: 5
print(abs(3.14))  # Output: 3.14

7. round()

The round() function rounds a number to a specified number of decimal places. If no second argument is provided, it rounds to the nearest integer.

Example:

print(round(3.14159, 2))  # Output: 3.14
print(round(3.5))  # Output: 4 (rounds to nearest even)

8. type()

The type() function returns the type of an object, which can be useful for debugging or understanding the data being processed.

Example:

print(type(42))  # Output: <class 'int'>
print(type("Hello"))  # Output: <class 'str'>
print(type([1, 2, 3]))  # Output: <class 'list'>

9. all()

The all() function returns True if all elements of the iterable are true (or if the iterable is empty). This function is helpful for validating conditions across multiple items.

Example:

print(all([True, True, False]))  # Output: False
print(all([1, 2, 3]))  # Output: True

10. any()

The any() function returns True if any element of the iterable is true. If the iterable is empty, it returns False.

Example:

print(any([False, False, True]))  # Output: True
print(any([0, "", None]))  # Output: False

Conclusion

Python's built-in functions provide essential tools for developers, making it easier to perform common operations with minimal code. Functions like len(), max(), sum(), and others help enhance the efficiency and readability of Python programs. By understanding and utilizing these functions, you can significantly streamline your coding process and improve the quality of your code. Whether you're a beginner or an experienced developer, mastering these built-in functions will benefit your Python programming journey.

...

🔧 Essential Python Built-in Functions: Examples and Usage


📈 54.01 Punkte
🔧 Programmierung

🔧 First-Class Functions, Higher-Order Functions, and Closures in Python – Explained with Code Examples


📈 35.68 Punkte
🔧 Programmierung

🔧 Functions of Commercial Bank: Primary Functions and Secondary Functions


📈 30 Punkte
🔧 Programmierung

🔧 Exploring SQL Functions: Harnessing the Power of Built-in Functions


📈 28.02 Punkte
🔧 Programmierung

🐧 Python Time Module Examples Usage


📈 25.52 Punkte
🐧 Linux Tipps

🔧 Understanding Floats in Python: Essential Tips and Examples


📈 25.27 Punkte
🔧 Programmierung

🔧 Python Program Examples – Simple Code Examples for Beginners


📈 25.2 Punkte
🔧 Programmierung

🔧 Understanding Python Regex Functions, with Examples


📈 24.81 Punkte
🔧 Programmierung

🔧 A Guide to Python Lambda Functions, with Examples


📈 24.81 Punkte
🔧 Programmierung

🔧 Durable functions in Python for Azure Functions | Azure Friday


📈 24.42 Punkte
🔧 Programmierung

📰 Comparing List Comprehensions vs. Built-In Functions in Python: Which Is Better?


📈 23.75 Punkte
🔧 AI Nachrichten

🐧 Python: Check file existence with built-in functions


📈 23.75 Punkte
🐧 Linux Tipps

🔧 Guide to Python's Built-in Functions


📈 23.75 Punkte
🔧 Programmierung

🔧 Python Built-in Functions


📈 23.75 Punkte
🔧 Programmierung

🔧 Python - Built-in functions - all()


📈 23.75 Punkte
🔧 Programmierung

🔧 Python - Built-in functions - abs()


📈 23.75 Punkte
🔧 Programmierung

🔧 Usage Metering and Usage-Based Billing for the Cloud


📈 21.86 Punkte
🔧 Programmierung

🔧 Introducing Laravel Usage Limiter Package: Track and restrict usage limits for users or accounts.


📈 21.86 Punkte
🔧 Programmierung

🔧 Introduction to Git and GitHub for Beginners: Essential Commands and Usage Tips


📈 21.62 Punkte
🔧 Programmierung

🔧 Grep Command in Linux – Usage, Options, and Syntax Examples


📈 21.54 Punkte
🔧 Programmierung

🔧 Mastering React Hooks: Usage and Examples Explained


📈 21.54 Punkte
🔧 Programmierung

🔧 $in, $nin, $implicit in MongoDB: Examples and Usage🔥


📈 21.54 Punkte
🔧 Programmierung

🔧 DataTable in C# – Usage And Examples 🧪


📈 21.54 Punkte
🔧 Programmierung

🔧 functions and their inverses: 2 insightful examples.


📈 20.83 Punkte
🔧 Programmierung

🔧 Should You Use Try-Catch Around Entire Functions in PHP? Best Practices and Examples


📈 20.83 Punkte
🔧 Programmierung

🔧 How to Write Better Names for Your Variables, Functions, and Classes – With Examples


📈 20.83 Punkte
🔧 Programmierung

matomo