In Python, built-in functions are functions that are provided by the Python interpreter and are always available for use without the need for importing any modules.
These functions serve different purposes and are commonly used in Python programming for performing different operations.
Here's an overview of some important built-in functions in Python:
int(), float(), str(), bool(): Convert values to integers, floating-point numbers, strings, or booleans.
# Converting string to int strNumber = "88" floatNumber = 3.14 intNumber = int(strNumber) print(intNumber) # Output: 88 # Converting float to int intNumber = int(floatNumber) print(intNumber) # Output: 3 # Converting int to float floatNumber = float(intNumber) print(floatNumber) # Output: 3.0 # Converting string to float floatNumber = float(strNumber) print(floatNumber) # Output: 88.0 # Convert int to string strNumber = str(intNumber) print(strNumber) # Output: "3" # Convert float to string strNumber = str(floatNumber) print(strNumber) # Output: "88.0"
list(), tuple(), set(), dict(): Convert iterable objects to lists, tuples, sets, or dictionaries.
# Converting string to list strValue = "Good Morning" listValue = list(strValue) print(listValue) # Output: ['G', 'o', 'o', 'd', ' ', 'M', 'o', 'r', 'n', 'i', 'n', 'g'] # Converting tuples list to dictionary tuplesList = [("firstName", "Alice"), ("lastName", "Collin")] dictValue = dict(tuplesList) print(dictValue) # Output: {'firstName': 'Alice', 'lastName': 'Collin'} # Converting list to tuple listValue = [1, 2, 3, 4, 5, 6] tupleValue = tuple(listValue) print(tupleValue) # Output: (1, 2, 3, 4, 5, 6) # Converting list to set listValue = [7, 7, 7, 0, 0, 0, 2, 3, 2] setValue = set(listValue) print(setValue) # Output: {0, 2, 3, 7}
print(): Print objects to the standard output.
input(): Read input from the user as a string.
# Using print() to display message print("Display message on screen inside print method") # Output: Display message on screen inside print method
# Using input() to read user input fullName = input("Enter your full name: ") print("Hi,", fullName) # Hi, <fullname depends on user input>
abs(): Return the absolute value of a number.
max(), min(): Return the maximum or minimum value among the given arguments.
sum(): Return the sum of elements in an iterable.
# Declare integer and assign value intValue = -20 # Finding the absolute value print(abs(intValue)) # Output: 20
# Declare integer and assign value marks = [50, 35, 45, 11, 60] # Apply Built-in functions of python # Finding the maximum value print(max(marks)) # Output: 60 # Finding the minimum value print(min(marks)) # Output: 11 # Calculating the sum of values print(sum(marks)) # Output: 201
len(): Return the length of an object (e.g., list, string, dictionary).
range(): Generate a sequence of numbers.
enumerate(): Return an iterator that yields tuples of index and value pairs.
zip(): Return an iterator that aggregates elements from multiple iterables.
sorted(): Return a new sorted list from the elements of an iterable.
reversed(): Return an iterator that yields elements in reverse order.
filter(): Return an iterator that filters elements from an iterable based on a function.
map(): Return an iterator that applies a function to every item of an iterable.
# Using lambda with sorted() to sort a list of tuples based on the second element pairs = [(5, 'five'), (6, 'six'), (2, 'two')] sortedPairs = sorted(pairs, key=lambda x: x[1]) print(sortedPairs) # Output: [(5, 'five'), (6, 'six'), (2, 'two')]
numbers = [1, 2, 3, 4, 5] reverseNumber = reversed(numbers) # Output: return iterator object print(list(reverseNumber)) # Output: [5, 4, 3, 2, 1]
# Using lambda with map() to double each element of a list numbers = [1, 2, 3, 4, 5] squaredNumbers = list(map(lambda x: x * 2, numbers)) print(squaredNumbers) # Output: [2, 4, 6, 8, 10]
# Using lambda with filter() to filter even numbers from a list numbers = [2, 5, 7, 8, 10] evenNumbers = list(filter(lambda x: x % 2 == 0, numbers)) print(evenNumbers) # Output: [2, 8, 10]
type(): Return the type of an object.
id(): Return the identity (memory address) of an object.
isinstance(): Check if an object is an instance of a class.
# Declare string and assign value strValue = "Welcome" # Apply Built-in functions of python print(type(strValue)) # Output: <class 'str'> print(id(strValue)) # Output: 133694960265328 print(isinstance(strValue, str)) # Output: True
len(): Return the length of a string.
str.upper(), str.lower(): Convert a string to uppercase or lowercase.
str.strip(): Remove leading and trailing whitespace from a string.
str.split(), str.join(): Split a string into a list of substrings or join a list of strings into a single string.
str.replace(): accepts two arguments, the first argument source value that needs to be replaced and the second argument with whom it gets replaced.
strValue = "Welcome" # get the length of a string print(len(strValue)) # Output: 7 # String Methods print(strValue.upper()) # Output: WELCOME print(strValue.lower()) # Output: welcome print(strValue.split()) # Output: ['Welcome'] print(strValue.replace("Welcome", "Hello")) # Output: Hello strValue = " Welcome " print(len(strValue)) # Output: 9 print(strValue.strip()) # Output: Welcome