- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
10 Python In-Built Functions You Should Know
Python, as a flexible and robust coding language, has captivated the hearts of coders, data wizards, and software artisans globally. The large-scale adoption of Python roots back to its vast array of native commands that ease intricate procedures, curtail development duration, and enhance script legibility. We delve into ten crucial Python native commands in this piece that every coder needs to grasp for a seamless and productive coding voyage.
Using len()
The len() command is a straightforward but indispensable mechanism for figuring out the size (i.e., the count of components) of a designated iterable like a list, tuple, or string.
Example
expression = "Python" size = len(expression) print(size)
Output
6
Using sum()
The sum() command provides the aggregate of all components in an iterable. This command spares you the hassle of crafting a loop to compute the sum of a list or tuple.
Example
digits = [1, 2, 3, 4, 5] aggregate = sum(digits) print(aggregate)
Output
15
Using map()
The map() command implements a particular function to all components in an iterable (e.g., list, tuple) and generates an iterator. This command comes in handy when there's a need to manipulate each component in a collection employing a specific function.
Example
def square(y): return y * y digits = [1, 2, 3, 4, 5] squared_digits = map(square, digits) print(list(squared_digits))
Output
[1, 4, 9, 16, 25]
Using filter()
The filter() command segregates components from an iterable based on a prescribed condition. It accepts two arguments: a function that delineates the filtering condition and the iterable for filtration. The outcome is an iterator incorporating elements that satisfy the condition.
Example
def is_even(y): return y % 2 == 0 digits = [1, 2, 3, 4, 5] even_digits = filter(is_even, digits) print(list(even_digits))
Output
[2, 4]
Using zip()
The zip() command combines two or more iterables, yielding an iterator of tuples, where the inaugural thing of each exceeded iterable is blended together, and after that the ensuing element of every exceeded iterable is matched together, and so forward. This command is particularly valuable after you have to solidify data from differing sources.
Example
identities = ['Alice', 'Bob', 'Charlie'] years = [25, 30, 35] merged_data = zip(identities, years) print(list(merged_data))
Output
[('Alice', 25), ('Bob', 30), ('Charlie', 35)]
Using sorted()
The sorted() command generates a new sorted list from the designated iterable. By default, it arranges the components in an ascending order. By using the optional ‘key’ and the reverse contention, sorting can be adjusted.
Example
digits = [5, 2, 1, 4, 3] sorted_digits = sorted(digits) print(sorted_digits)
Output
[1, 2, 3, 4, 5]
Using any() and all()
The any() command yields True in the event that at least a single component of an iterable is true, and False something else. The all() command, conversely, gives True on the off chance that all elements of an iterable are true, and False otherwise. These commands are practical when you need to assess multiple conditions in a collection.
Example
list1 = [True, False, True] list2 = [True, True, True] print(any(list1)) print(all(list1)) print(all(list2))
Output
True False True
Using enumerate()
The enumerate() command is used to include a counter to an iterable, returning an enumerated object. It takes two arguments: the iterable and an optional 'start' value for the counter. This command becomes especially valuable when both the index and value of elements are required within a loop.
Example
fruit_types = ['apple', 'banana', 'cherry'] for count, value in enumerate(fruit_types, start=1): print(f"{count}. {value}")
Output
1. apple 2. banana 3. cherry
Using range()
The range() command produces a series of digits, usually used in loops for repeating a particular number. One, two or three arguments can be recognised as − 'start' (optional), 'stop', and 'step' (optional). Default value of start and ‘step’ is set to 0 and 1 respectively.
Example
for i in range(5): print(i) for i in range(1, 5, 2): print(i)
Output
0 1 2 3 4 1 3
Using isinstance()
The isinstance() command confirms if a given question is an occurrence or subclass of an indicated lesson. It returns True if the object is an occasion of the lesson or a subclass thereof, and False otherwise. This command gets to be valuable when there's a need to approve input data types or oversee numerous types of input inside a function.
Example
numeral = 5 outcome = isinstance(numeral, int) print(outcome) expression = 'Python' outcome = isinstance(expression, int) print(outcome)
Output
True False
Conclusion
Python's native commands are indispensable tools for coders, facilitating them to craft concise, effective, and legible scripts. The ten commands tackled in this piece are by no means an exhaustive roster, but they signify a solid foundation for any coder to build upon. By mastering these commands and exploring other native Python tools, you'll be proficiently equipped to address a broad spectrum of coding challenges.