Python Built-in Functions

Fifteen functions that are always there, no import needed โ€” I/O, type conversion, math, number-base conversion, iterables, introspection, and documentation lookup.

Overview

This session covers Python's built-in functions โ€” functions that are always available without needing to import anything. These are fundamental utilities used constantly in everyday Python programming: I/O, type conversion, math operations, and introspection (inspecting objects/functions).

Key Concepts

Detailed Explanation

1. print()

Prints output to the console (stdout by default).

print("Hello world")

Output:

Hello world

๐Ÿ“Œ Full signature (from help('print')):

print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)

Optional keyword arguments:

ArgumentMeaningDefault
fileA file-like stream to write tosys.stdout
sepString inserted between multiple values' ' (space)
endString appended after the last value'\n' (newline)
flushWhether to forcibly flush the streamFalse
๐Ÿ’ก Tip: Use sep and end to customize how multiple print statements or values are formatted, e.g. print("a", "b", sep="-") โ†’ a-b.

2. input()

Reads a line of text typed by the user and returns it as a string.

input("Enter your name")

Interaction:

Enter your nameNitish

Output:

'Nitish'

Important Points

3. type()

Returns the data type/class of an object.

a = True
type(a)

Output:

bool
๐Ÿ’ก Useful for debugging โ€” quickly check what kind of object a variable is.

4. int() and other type conversion functions

int() converts a value (like a float or string) into an integer.

int(5.6)
# float
# str
# list
# tuple

Output:

5

Important Points

โš ๏ธ Screenshot text unclear โ€” the comments (# float, # str, # list, # tuple) appear to just be a note-to-self listing related conversion functions, not executable code.

5. abs()

Returns the absolute value (removes the negative sign) of a number.

abs(-4)

Output:

4

6. pow()

Returns the value of a number raised to a power (like **, but as a function).

pow(2, 3)

Output:

8
pow(2, -3)

Output:

0.125

Important Points

7. min() / max()

Return the smallest or largest item in an iterable.

max([2, 1, 3, 0])

Output: 3

min([2, 1, 3, 0])

Output: 0

min("kolkata")

Output: 'a'

Important Points

8. round()

Rounds a number to the nearest value, optionally to a specified number of decimal places.

c = 22/7
round(c, 2)

Output: 3.14

round(c)

Output: 3

Important Points

9. divmod()

Returns a tuple containing the quotient and remainder of a division, in one call.

divmod(5, 2)

Output:

(2, 1)
๐Ÿ’ก Equivalent to (5 // 2, 5 % 2) but computed in a single, efficient call. Useful when you need both quotient and remainder together.

10. bin() / oct() / hex()

Convert an integer into its binary, octal, or hexadecimal string representation.

hex(4)

Output: '0x4'

oct(4)

Output: '0o4'

Important Points

11. id()

Returns the unique memory identity (address) of an object โ€” an integer that is unique for the object's lifetime.

a = 3
id(a)

Output:

140721404847984

Important Points

12. ord()

Returns the Unicode/ASCII code point (integer) of a single character.

ord('A')

Output:

65
๐Ÿ’ก The reverse function is chr(), which converts a code point back into a character (not shown in screenshots, but a natural pairing to remember).

13. len()

Returns the number of items in a sequence or collection (list, string, tuple, dict, etc.).

len([1, 2, 3])

Output:

3

14. sum()

Returns the sum of all items in an iterable, plus an optional starting value.

sum()  # (being typed in the notebook โ€” incomplete example)

๐Ÿ“Œ Full signature (from help('sum')):

sum(iterable, /, start=0)

Behavior:

Example (inferred usage pattern):

sum([1, 2, 3])        # โ†’ 6
sum([1, 2, 3], 10)     # โ†’ 16 (start=10)

15. help()

Displays the documentation (docstring) for a function, module, or object โ€” extremely useful for quickly checking a function's signature and behavior without leaving the notebook.

help('print')

Output (partial):

Help on built-in function print in module builtins:

print(...)
    print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)

    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file:  a file-like object (stream); defaults to the current sys.stdout.
    sep:   string inserted between values, default a space.
    end:   string appended after the last value, default a newline.
    flush: whether to forcibly flush the stream.
help('sum')

Output (partial):

Help on built-in function sum in module builtins:

sum(iterable, /, start=0)
    Return the sum of a 'start' value (default: 0) plus an iterable of numbers

    When the iterable is empty, return the start value.
    This function is intended specifically for use with numeric values and may
    reject non-numeric types.
๐Ÿ’ก Tip: help() can be called with either the function name as a string (help('print')) or the function object itself (help(print)) โ€” both work.

Commands / Syntax Cheat Sheet

print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
input("prompt")                # always returns a string
type(obj)                      # returns the class/type of obj
int(x)                         # converts x to int (truncates floats)
abs(x)                         # absolute value
pow(base, exp)                 # exponentiation (like base ** exp)
min(iterable)                  # smallest item
max(iterable)                  # largest item
round(number, ndigits=None)    # rounds; ndigits omitted โ†’ nearest int
divmod(a, b)                   # returns (a // b, a % b) as a tuple
bin(x)                         # binary string, prefixed '0b'
oct(x)                         # octal string, prefixed '0o'
hex(x)                         # hex string, prefixed '0x'
id(obj)                        # unique memory identity of obj
ord(char)                      # Unicode code point of a single character
len(sequence)                  # number of items
sum(iterable, start=0)         # sum of items + start value
help(obj_or_name)              # shows documentation

Things to Remember

Quick Revision

Python has many built-in functions that require no imports: