Built-in Functions

Python comes with a rich set of built-in functions that you can use right out of the box without needing to install or import anything. These functions make programming faster and easier by providing ready-made solutions for common tasks.

1. Input and Output Functions

Function Description
print() Prints output to the screen.
input() Takes user input as a string.

Example :

Python
Copy
name = input("Enter your name: ")
# Takes input from user

print("Hello, Python!") 
# Output: Hello, Python!

2. Type Conversion Functions

Function Description
int() Converts a value to an integer.
float() Converts a value to a floating-point number.
str() Converts a value to a string.
bool() Converts a value to a boolean.
complex() Converts to a complex number.
list() Converts to a list.
tuple() Converts to a tuple.
set() Converts to a set.
frozenset() Converts to an immutable set.
dict() Converts to a dictionary.
bytes() Converts to a bytes object.
bytearray() Converts to a mutable byte array.
memoryview() Creates a memory view object.

Example :

Python
Copy
int("10")
# Output: 10

float("3.14")
# Output: 3.14

str(123)
# Output: '123'

bool(0)
# Output: False

complex(1, 2)
# Output: (1+2j)

list("abc")
# Output: ['a', 'b', 'c']

tuple([1, 2, 3])
# Output: (1, 2, 3)

set([1, 2, 2])
# Output: {1, 2}

frozenset([1, 2, 3])
# Output: frozenset({1, 2, 3})

dict(a=1, b=2)
# Output: {'a': 1, 'b': 2}

bytes(5)
# Output: b'\x00\x00\x00\x00\x00'

bytearray(3)
# Output: bytearray(b'\x00\x00\x00')

memoryview(b'abc')

3. String Functions

Function Description
ord() Converts a character to its Unicode code.
chr() Converts a Unicode code to its character.
ascii() Returns a readable version of a string.
repr() Returns a string representation of an object.

Example :

Python
Copy
ord('A')
# Output: 65

chr(65)
# Output: 'A'

ascii('🐍')
# Output: "'\U0001f40d'"

repr(3.14)
# Output: '3.14'

4. Math Functions

Function Description
abs() Returns the absolute value.
round() Rounds a number to the nearest integer.
pow() Returns x raised to the power y.
divmod() Returns quotient and remainder as a tuple.
min() Returns the smallest value.
max() Returns the largest value.
sum() Returns the sum of all items.

Example :

Python
Copy
abs(-10)
# Output: 10

round(3.14)
# Output: 3

pow(2, 3)
# Output: 8

divmod(10, 3)
# Output: (3, 1)

min(1, 2, 3)
# Output: 1

max(1, 2, 3)
# Output: 3

sum([1, 2, 3])
# Output: 6

5. Iterable and Sequence Functions

Function Description
len() Returns the number of items.
sorted() Returns a sorted list.
reversed() Returns a reversed iterator.
enumerate() Adds an index to an iterable.
all() Returns True if all elements are true.
any() Returns True if any element is true.
range() Generates a sequence of numbers.
zip() Combines iterables into pairs.
map() Applies a function to each item.
filter() Filters items based on a condition.

Example:

Python
Copy
len("Python")
# Output: 6

sorted([3, 1, 2])
# Output: [1, 2, 3]

list(reversed([1, 2, 3]))
# Output: [3, 2, 1]

list(enumerate("abc"))
# Output: [(0, 'a'), (1, 'b'), (2, 'c')]

all([True, False])
# Output: False

any([True, False])
# Output: True

list(range(3))
# Output: [0, 1, 2]

list(zip("abc", [1, 2, 3]))
# Output: [('a', 1), ('b', 2), ('c', 3)]

list(map(str.upper, "abc"))
# Output: ['A', 'B', 'C']

list(filter(lambda x: x > 1, [0, 1, 2]))
# Output: [2]

6. Object and Attribute Functions

Function Description
type() Returns the type of an object.
id() Returns the memory address of an object.
isinstance() Checks if an object is of a type.
issubclass() Checks if a class is a subclass.
getattr() Gets the value of an object's attribute.
setattr() Sets the value of an object's attribute.
delattr() Deletes an attribute.
hasattr() Checks if an object has an attribute.
callable() Checks if an object is callable.
dir() Returns all attributes and methods of an object.
vars() Returns __dict__ attribute of an object.

Example:

Python
Copy
type(123)
# Output: <class 'int'>

id(123)
# Output: 140714374146296

isinstance(123, int)
#Output: True

issubclass(bool, int)
#Output: True

getattr(obj, "attr")

setattr(obj, "attr", value)

delattr(obj, "attr")

hasattr(obj, "attr")
#Output: True

callable(len)
#Output: True

dir([])

vars(obj)

7. Memory and Data Representation Functions

Function Description
bin() Converts to binary.
oct() Converts to octal.
hex() Converts to hexadecimal.
hash() Returns the hash value of an object.

Example

Python
Copy
bin(5)
# Output: '0b101'

oct(8)
# Output: '0o10'

hex(255)
# Output: '0xff'

hash("Python")
# Output: -4053829851421555031

8. File and System Functions

Function Description
open() Opens a file.
compile() Compiles source code to bytecode.
exec() Executes dynamically compiled Python code.
eval() Evaluates a Python expression.

Example

Python
Copy
file = open("example.txt", "r")

code = compile("print('Hello')", "", "exec")

exec("print('Hello')")

eval("2 + 2") Output: 4

9. Debugging and Help Functions

Function Description
help() Displays documentation for an object.
globals() Returns a dictionary of global variables.
locals() Returns a dictionary of local variables.
exit() Exits Python.

Example:

Python
Copy
help(print)

globals()

locals()

exit()

10. Class and Object Functions

Function Description
staticmethod() Creates a static method.
classmethod() Creates a class method.
property() Creates a property object.
super() Refers to the parent class.

Example:

Python
Copy
staticmethod(func)

classmethod(func)

property(fget, fset, fdel)

super().__init__()