Python is a versatile programming language known for its simplicity and readability. One of the powerful features of Python is its reference functions. These functions help you manipulate and access various data types, making programming more efficient. In this article, we’ll go through some of the built-in reference functions in Python with examples, tables, and detailed explanations to make it easy for beginners to understand.
I. Built-in Functions
Python comes with a variety of built-in functions that perform essential operations. Below, we will explore some of the most commonly used functions.
A. abs()
The abs() function returns the absolute value of a number.
example = abs(-5)
print(example) # Output: 5
B. all()
The all() function returns True if all elements in an iterable are true.
example = all([True, True, False])
print(example) # Output: False
C. any()
The any() function returns True if any element in an iterable is true.
example = any([True, False, False])
print(example) # Output: True
D. ascii()
The ascii() function returns the printable representation of an object.
example = ascii("Hello 🌍")
print(example) # Output: 'Hello \U0001f30d'
E. bin()
The bin() function converts an integer number to a binary string.
example = bin(5)
print(example) # Output: '0b101'
F. breakpoint()
The breakpoint() function drops into the debugger at the call site.
breakpoint()
G. callable()
The callable() function checks if an object appears callable (like a function).
example = callable(print)
print(example) # Output: True
H. chr()
The chr() function returns a string representing a character whose Unicode code point is the integer provided.
example = chr(97)
print(example) # Output: 'a'
I. classmethod()
The classmethod() function transforms a method into a class method.
class MyClass:
@classmethod
def my_method(cls):
return cls
print(MyClass.my_method()) # Output:
J. compile()
The compile() function compiles the source into a code or AST object.
code = compile('print("Hello World")', '', 'exec')
exec(code) # Output: Hello World
K. copyright()
The copyright() function returns copyright information.
print(copyright())
L. credits()
The credits() function displays credits for the Python community.
credits()
M. delattr()
The delattr() function deletes an attribute from an object.
class MyClass:
pass
obj = MyClass()
obj.name = "John"
delattr(obj, 'name')
print(hasattr(obj, 'name')) # Output: False
N. dir()
The dir() function returns a list of names in the current local scope or the list of attributes of an object.
example = dir([1, 2, 3])
print(example) # Lists methods of list
O. divmod()
The divmod() function returns a tuple of quotient and remainder.
example = divmod(5, 2)
print(example) # Output: (2, 1)
P. eval()
The eval() function parses the expression passed and executes it.
result = eval('3 + 5')
print(result) # Output: 8
Q. exec()
The exec() function executes the Python code in the form of a string.
exec('x = 10')
print(x) # Output: 10
R. exit()
The exit() function exits the interpreter.
exit()
S. format()
The format() function formats a specified value.
formatted = "{} is {}".format("Python", "awesome")
print(formatted) # Output: Python is awesome
T. frozenset()
The frozenset() function returns a frozenset object, which is an immutable version of a set.
example = frozenset([1, 2, 2, 3])
print(example) # Output: frozenset({1, 2, 3})
U. getattr()
The getattr() function returns the value of a named attribute of an object.
class MyClass:
attr = 10
obj = MyClass()
print(getattr(obj, 'attr')) # Output: 10
V. globals()
The globals() function returns a dictionary representing the current global symbol table.
example = globals()
print(example) # Returns global namespace dictionary
W. hasattr()
The hasattr() function checks if an object has a specific attribute.
class MyClass:
attr = 5
obj = MyClass()
print(hasattr(obj, 'attr')) # Output: True
X. hash()
The hash() function returns the hash value of an object if it has one.
example = hash("hello")
print(example) # Outputs hash of the string
Y. hex()
The hex() function converts an integer to a hexadecimal string.
example = hex(255)
print(example) # Output: '0xff'
Z. id()
The id() function returns the identity of an object.
example = id("hello")
print(example) # Outputs unique id of the string
AA. input()
The input() function allows user input.
user_input = input("Enter something: ")
print(user_input)
AB. int()
The int() function converts a value to an integer.
example = int("42")
print(example) # Output: 42
AC. isinstance()
The isinstance() function checks if an object is an instance of a specific class.
example = isinstance(10, int)
print(example) # Output: True
AD. issubclass()
The issubclass() function checks if a class is a subclass of another class.
class A: pass
class B(A): pass
example = issubclass(B, A)
print(example) # Output: True
AE. len()
The len() function returns the length of an object.
example = len("Hello")
print(example) # Output: 5
AF. license()
The license() function returns the license information.
print(license())
AG. list()
The list() function creates a list from an iterable.
example = list((1, 2, 3))
print(example) # Output: [1, 2, 3]
AH. locals()
The locals() function updates the local symbol table.
example = locals()
print(example) # Returns current local namespace dictionary
AI. map()
The map() function applies a function to every item in an iterable.
example = list(map(lambda x: x * 2, [1, 2, 3]))
print(example) # Output: [2, 4, 6]
AJ. max()
The max() function returns the largest item in an iterable.
example = max([1, 5, 3])
print(example) # Output: 5
AK. memoryview()
The memoryview() function returns a memory view object.
example = memoryview(bytes(5))
print(example) # Output:
AL. min()
The min() function returns the smallest item in an iterable.
example = min([1, 5, 3])
print(example) # Output: 1
AM. next()
The next() function retrieves the next item from an iterator.
example = iter([1, 2, 3])
print(next(example)) # Output: 1
AN. object()
The object() function returns a new featureless object.
example = object()
print(example) # Output:
AO. oct()
The oct() function converts an integer to an octal string.
example = oct(8)
print(example) # Output: '0o10'
AP. open()
The open() function opens a file and returns a corresponding file object.
file = open('test.txt', 'w')
file.write("Hello World")
file.close()
AQ. ord()
The ord() function returns the Unicode code point for a given character.
example = ord('A')
print(example) # Output: 65
AR. pow()
The pow() function returns the value of a number raised to a power.
example = pow(2, 3)
print(example) # Output: 8
AS. print()
The print() function outputs data to the console.
print("Hello World") # Output: Hello World
AT. property()
The property() function returns a property attribute.
class MyClass:
def __init__(self):
self._value = 0
@property
def value(self):
return self._value
obj = MyClass()
print(obj.value) # Output: 0
AU. quit()
The quit() function exits from Python.
quit()
AV. range()
The range() function generates a sequence of numbers.
example = list(range(5))
print(example) # Output: [0, 1, 2, 3, 4]
AW. repr()
The repr() function returns a string representation of an object.
example = repr("hello")
print(example) # Output: "'hello'"
AX. reversed()
The reversed() function returns a reversed iterator.
example = list(reversed([1, 2, 3]))
print(example) # Output: [3, 2, 1]
AY. round()
The round() function rounds a number to a specified number of decimal places.
example = round(3.14159, 2)
print(example) # Output: 3.14
AZ. set()
The set() function creates a set object.
example = set([1, 2, 2, 3])
print(example) # Output: {1, 2, 3}
BA. slicing()
Python provides slicing techniques to access parts of sequences.
example = [1, 2, 3, 4, 5]
sliced = example[1:4]
print(sliced) # Output: [2, 3, 4]
BB. sorted()
The sorted() function returns a new sorted list from the elements of any iterable.
example = sorted([3, 1, 2])
print(example) # Output: [1, 2, 3]
BC. staticmethod()
The staticmethod() function transforms a method into a static method.
class MyClass:
@staticmethod
def my_method():
return "Hello"
print(MyClass.my_method()) # Output: Hello
BD. str()
The str() function converts an object to its string representation.
example = str(100)
print(example) # Output: '100'
BE. sum()
The sum() function adds the items of an iterable.
example = sum([1, 2, 3])
print(example) # Output: 6
BF. tuple()
The tuple() function creates a tuple from an iterable.
example = tuple([1, 2, 3])
print(example) # Output: (1, 2, 3)
BG. type()
The type() function returns the type of an object.
example = type(100)
print(example) # Output:
BH. vars()
The vars() function returns the __dict__ attribute of an object.
class MyClass:
def __init__(self):
self.x = 10
obj = MyClass()
print
Leave a comment