Built-in Functions (20 Programs)
Program 1: abs()
– Absolute Value of a Number
Program:
def absolute_value(num):
"""Return the absolute value of a number."""
return abs(num)
# Test the function
print("Absolute value of -10:", absolute_value(-10))
print("Absolute value of 5:", absolute_value(5))
Expected Output:
Absolute value of -10: 10
Absolute value of 5: 5
Program 2: all()
– Check if All Elements in List are True
Program:
def check_all_true(values):
"""Check if all elements in the list are True."""
return all(values)
# Test the function
print("All elements True:", check_all_true([True, True, True]))
print("All elements True:", check_all_true([True, False, True]))
Expected Output:
All elements True: True
All elements True: False
Program 3: any()
– Check if Any Element in List is True
Program:
def check_any_true(values):
"""Check if any element in the list is True."""
return any(values)
# Test the function
print("Any element True:", check_any_true([False, False, True]))
print("Any element True:", check_any_true([False, False, False]))
Expected Output:
Any element True: True
Any element True: False
Program 4: ascii()
– Return ASCII Representation of a String
Program:
def get_ascii_representation(text):
"""Get ASCII representation of a string."""
return ascii(text)
# Test the function
print("ASCII representation:", get_ascii_representation("Python ©"))
Expected Output:
ASCII representation: 'Python \xa9'
Program 5: bin()
– Convert Integer to Binary
Program:
def to_binary(num):
"""Convert an integer to binary."""
return bin(num)
# Test the function
print("Binary of 10:", to_binary(10))
print("Binary of 25:", to_binary(25))
Expected Output:
Binary of 10: 0b1010
Binary of 25: 0b11001
Program 6: bool()
– Convert Value to Boolean
Program:
def to_boolean(value):
"""Convert a value to boolean."""
return bool(value)
# Test the function
print("Boolean of 0:", to_boolean(0))
print("Boolean of 'Hello':", to_boolean("Hello"))
Expected Output:
Boolean of 0: False
Boolean of 'Hello': True
Program 7: chr()
– Convert Unicode Code to Character
Program:
def unicode_to_char(code):
"""Convert Unicode code to character."""
return chr(code)
# Test the function
print("Character for 97:", unicode_to_char(97))
print("Character for 65:", unicode_to_char(65))
Expected Output:
Character for 97: a
Character for 65: A
Program 8: divmod()
– Quotient and Remainder of Division
Program:
def quotient_remainder(a, b):
"""Get quotient and remainder of division."""
return divmod(a, b)
# Test the function
print("Quotient and remainder of 10 / 3:", quotient_remainder(10, 3))
print("Quotient and remainder of 20 / 6:", quotient_remainder(20, 6))
Expected Output:
Quotient and remainder of 10 / 3: (3, 1)
Quotient and remainder of 20 / 6: (3, 2)
Program 9: enumerate()
– Enumerate List Items with Index
Program:
def enumerate_list(items):
"""Enumerate items in a list with their index."""
return list(enumerate(items))
# Test the function
print("Enumerated list:", enumerate_list(["apple", "banana", "cherry"]))
Expected Output:
Enumerated list: [(0, 'apple'), (1, 'banana'), (2, 'cherry')]
Program 10: eval()
– Evaluate a Python Expression
Program:
def evaluate_expression(expression):
"""Evaluate a Python expression."""
return eval(expression)
# Test the function
print("Evaluation of '3 + 5':", evaluate_expression("3 + 5"))
print("Evaluation of '2 * 6':", evaluate_expression("2 * 6"))
Expected Output:
Evaluation of '3 + 5': 8
Evaluation of '2 * 6': 12
Program 11: filter()
– Filter Even Numbers from a List
Program:
def filter_even(numbers):
"""Filter even numbers from a list."""
return list(filter(lambda x: x % 2 == 0, numbers))
# Test the function
print("Even numbers:", filter_even([1, 2, 3, 4, 5, 6]))
Expected Output:
Even numbers: [2, 4, 6]
Program 12: float()
– Convert Value to Float
Program:
def to_float(value):
"""Convert a value to float."""
return float(value)
# Test the function
print("Float of 5:", to_float(5))
print("Float of '3.14':", to_float("3.14"))
Expected Output:
Float of 5: 5.0
Float of '3.14': 3.14
Program 13: format()
– Format Number with 2 Decimal Places
Program:
def format_number(num):
"""Format a number to 2 decimal places."""
return format(num, ".2f")
# Test the function
print("Formatted number:", format_number(3.14159))
print("Formatted number:", format_number(7.88888))
Expected Output:
Formatted number: 3.14
Formatted number: 7.89
Program 14: hex()
– Convert Integer to Hexadecimal
Program:
def to_hexadecimal(num):
"""Convert an integer to hexadecimal."""
return hex(num)
# Test the function
print("Hexadecimal of 255:", to_hexadecimal(255))
print("Hexadecimal of 16:", to_hexadecimal(16))
Expected Output:
Hexadecimal of 255: 0xff
Hexadecimal of 16: 0x10
Program 15: input()
– Take User Input (for Interactive Testing)
Program:
def get_user_input():
"""Get input from the user."""
user_input = input("Enter a message: ")
return f"You entered: {user_input}"
# Run in an interactive environment to see output.
Output will depend on user input.
Program 16: len()
– Length of a List
Program:
def list_length(lst):
"""Return the length of a list."""
return len(lst)
# Test the function
print("Length of list:", list_length([1, 2, 3, 4]))
Expected Output:
Length of list: 4
Program 17: max()
– Maximum of Three Numbers
Program:
def find_maximum(a, b, c):
"""Find the maximum of three numbers."""
return max(a, b, c)
# Test the function
print("Maximum of (3, 7, 5):", find_maximum(3, 7, 5))
Expected Output:
Maximum of (3, 7, 5): 7
Program 18: min()
– Minimum of Three Numbers
Program:
def find_minimum(a, b, c):
"""Find the minimum of three numbers."""
return min(a, b, c)
# Test the function
print("Minimum of (3, 7, 5):", find_minimum(3, 7, 5))
Expected Output:
Minimum of (3, 7, 5): 3
Program 19: round()
– Round a Number to Specified Digits
Program:
def round_number(num, digits):
"""Round a number to specified digits."""
return round(num, digits)
# Test the function
print("Rounded number:", round_number(3.14159, 2))
Expected Output:
Rounded number: 3.14
Program 20: sorted()
– Sort a List in Ascending Order
Program:
def sort_list(lst):
"""Sort a list in ascending order."""
return sorted(lst)
# Test the function
print("Sorted list:", sort_list([3, 1, 4, 2]))
Expected Output:
Sorted list: [1, 2, 3, 4]