DEV Community

Cover image for ๐Ÿ Python Built-in Functions - Ultimate Cheat Sheet
AK
AK

Posted on

1 1 1

๐Ÿ Python Built-in Functions - Ultimate Cheat Sheet

Python's most commonly used built-in functions, organized by alphabet. Each function is explained with a brief description, an example, and some fun ๐ŸŽ‰ emojis to make it easier and more enjoyable to learn.


๐Ÿ”ค A

abs() โ€“ Absolute value

Returns the absolute value of a number

abs(-7)  # 7
Enter fullscreen mode Exit fullscreen mode

all() โ€“ Checks if all items are True

Returns True if all elements in an iterable are true

all([1, 2, 3])  # True
Enter fullscreen mode Exit fullscreen mode

any() โ€“ Any item True?

Returns True if at least one element is True

any([False, 0, 5])  # True
Enter fullscreen mode Exit fullscreen mode

ascii() โ€“ String representation

Returns a string containing a printable representation

ascii("cafรฉ")  # "'caf\\xe9'"
Enter fullscreen mode Exit fullscreen mode

๐Ÿงฎ B

bin() โ€“ Binary representation

Returns binary version of a number

bin(5)  # '0b101'
Enter fullscreen mode Exit fullscreen mode

bool() โ€“ Boolean value

Converts a value to Boolean (True or False)

bool(0)  # False
Enter fullscreen mode Exit fullscreen mode

bytearray() โ€“ Mutable byte sequence

Creates a mutable array of bytes

bytearray('hello', 'utf-8')  # bytearray(b'hello')
Enter fullscreen mode Exit fullscreen mode

bytes() โ€“ Immutable byte sequence

Creates an immutable bytes object

bytes('hello', 'utf-8')  # b'hello'
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“ž C

chr() โ€“ Character from ASCII

Returns the character corresponding to an ASCII code

chr(65)  # 'A'
Enter fullscreen mode Exit fullscreen mode

complex() โ€“ Complex numbers

Creates a complex number

complex(2, 3)  # (2+3j)
Enter fullscreen mode Exit fullscreen mode

๐Ÿ—‘๏ธ D

delattr() โ€“ Delete attribute

Deletes an attribute from an object

class Car:
    color = "red"
delattr(Car, "color")
Enter fullscreen mode Exit fullscreen mode

dict() โ€“ Dictionary

Creates a dictionary

dict(name="Alice", age=25)  # {'name': 'Alice', 'age': 25}
Enter fullscreen mode Exit fullscreen mode

dir() โ€“ List attributes

Returns list of attributes of an object

dir([])  # Shows list methods like append(), sort(), etc.
Enter fullscreen mode Exit fullscreen mode

divmod() โ€“ Division & Modulus

Returns quotient and remainder

divmod(10, 3)  # (3, 1)
Enter fullscreen mode Exit fullscreen mode

๐Ÿ” E

enumerate() โ€“ Index + Value

Returns index and value pairs

for i, val in enumerate(['a','b','c']): print(i, val)
Enter fullscreen mode Exit fullscreen mode

eval() โ€“ Evaluate expression

Evaluates a string as Python code

eval("2 + 3")  # 5
Enter fullscreen mode Exit fullscreen mode

exec() โ€“ Execute code

Executes a block of Python code

exec("x = 5\nprint(x)")  # 5
Enter fullscreen mode Exit fullscreen mode

๐Ÿงน F

filter() โ€“ Filter items

Filters iterable using a function

list(filter(lambda x: x > 3, [1,2,3,4,5]))  # [4,5]
Enter fullscreen mode Exit fullscreen mode

float() โ€“ Float conversion

Converts a value to float

float("3.14")  # 3.14
Enter fullscreen mode Exit fullscreen mode

format() โ€“ Format values

Formats a string

"{} {}".format("Hello", "World")  # 'Hello World'
Enter fullscreen mode Exit fullscreen mode

frozenset() โ€“ Immutable set

Creates an immutable set

frozenset([1,2,3])  # frozenset({1,2,3})
Enter fullscreen mode Exit fullscreen mode

๐Ÿ’ก G

getattr() โ€“ Get attribute

Returns the value of a named attribute

class Dog: name = "Buddy"
getattr(Dog, "name")  # 'Buddy'
Enter fullscreen mode Exit fullscreen mode

globals() โ€“ Global variables

Returns dictionary of global variables

globals()
Enter fullscreen mode Exit fullscreen mode

๐Ÿ” H

hasattr() โ€“ Check attribute

Returns True if object has that attribute

hasattr(str, "upper")  # True
Enter fullscreen mode Exit fullscreen mode

hash() โ€“ Hash value

Returns hash of an object

hash("hello")  # Some integer
Enter fullscreen mode Exit fullscreen mode

help() โ€“ Documentation

Shows help documentation

help(list)
Enter fullscreen mode Exit fullscreen mode

hex() โ€“ Hexadecimal

Returns hexadecimal representation

hex(255)  # '0xff'
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”ข I

id() โ€“ Identity

Returns memory address of object

id("hello")
Enter fullscreen mode Exit fullscreen mode

input() โ€“ User input

Takes input from user

name = input("Enter name: ")
Enter fullscreen mode Exit fullscreen mode

int() โ€“ Integer conversion

Converts to integer

int("123")  # 123
Enter fullscreen mode Exit fullscreen mode

isinstance() โ€“ Type check

Returns True if object is instance of class

isinstance(5, int)  # True
Enter fullscreen mode Exit fullscreen mode

issubclass() โ€“ Inheritance check

Returns True if class is subclass

issubclass(bool, int)  # True
Enter fullscreen mode Exit fullscreen mode

iter() โ€“ Iterator

Returns iterator object

it = iter([1,2,3])
next(it)  # 1
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“ L

len() โ€“ Length

Returns length of object

len("hello")  # 5
Enter fullscreen mode Exit fullscreen mode

list() โ€“ List

Converts to list

list((1,2,3))  # [1,2,3]
Enter fullscreen mode Exit fullscreen mode

locals() โ€“ Local variables

Returns local variable dict

locals()
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”„ M

map() โ€“ Apply function

Applies function to all items

list(map(lambda x: x.upper(), ["a"]))  # ['A']
Enter fullscreen mode Exit fullscreen mode

max() โ€“ Maximum

Returns maximum value

max([1,2,3])  # 3
Enter fullscreen mode Exit fullscreen mode

min() โ€“ Minimum

Returns minimum value

min([1,2,3])  # 1
Enter fullscreen mode Exit fullscreen mode

memoryview() โ€“ Memory view

Access internal data without copying

mv = memoryview(b'Hello')
Enter fullscreen mode Exit fullscreen mode

โญ๏ธ N

next() โ€“ Next item

Returns next item from iterator

it = iter([1,2])
next(it)  # 1
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“ฆ O

object() โ€“ Base class

Returns a new featureless object

obj = object()
Enter fullscreen mode Exit fullscreen mode

oct() โ€“ Octal

Returns octal representation

oct(8)  # '0o10'
Enter fullscreen mode Exit fullscreen mode

open() โ€“ File opener

Opens a file

with open("file.txt") as f: content = f.read()
Enter fullscreen mode Exit fullscreen mode

ord() โ€“ ASCII code

Returns ASCII code for a character

ord('A')  # 65
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”ฅ P

pow() โ€“ Power

Raises a number to a power

pow(2, 3)  # 8
Enter fullscreen mode Exit fullscreen mode

print() โ€“ Output

Prints output to console

print("Hello!")
Enter fullscreen mode Exit fullscreen mode

property() โ€“ Property

Used in classes to create managed attributes

class Circle:
    def __init__(self, radius):
        self._radius = radius

    @property
    def radius(self):
        return self._radius
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”„ R

range() โ€“ Range of numbers

Generates sequence of numbers

list(range(1, 5))  # [1,2,3,4]
Enter fullscreen mode Exit fullscreen mode

repr() โ€“ Representation

Returns string representation

repr("hello")  # "'hello'"
Enter fullscreen mode Exit fullscreen mode

reversed() โ€“ Reverse iterator

Returns reversed iterator

list(reversed([1,2,3]))  # [3,2,1]
Enter fullscreen mode Exit fullscreen mode

round() โ€“ Round number

Rounds a number to n digits

round(3.1415, 2)  # 3.14
Enter fullscreen mode Exit fullscreen mode

๐Ÿ› ๏ธ S

set() โ€“ Set

Creates a set

set([1,2,2])  # {1,2}
Enter fullscreen mode Exit fullscreen mode

setattr() โ€“ Set attribute

Sets an attribute on an object

class Car: pass
setattr(Car, "color", "blue")
Enter fullscreen mode Exit fullscreen mode

slice() โ€“ Slice object

Represents slicing

s = slice(1, 4)
[0,1,2,3,4][s]  # [1,2,3]
Enter fullscreen mode Exit fullscreen mode

sorted() โ€“ Sort

Returns sorted list

sorted([3,1,2])  # [1,2,3]
Enter fullscreen mode Exit fullscreen mode

staticmethod() โ€“ Static method

Marks a method as static

class Math:
    @staticmethod
    def add(a, b): return a + b
Enter fullscreen mode Exit fullscreen mode

str() โ€“ String

Converts to string

str(123)  # '123'
Enter fullscreen mode Exit fullscreen mode

sum() โ€“ Summation

Adds all items

sum([1,2,3])  # 6
Enter fullscreen mode Exit fullscreen mode

super() โ€“ Parent class

Calls parent class method

class Child(Parent):
    def __init__(self):
        super().__init__()
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“ฆ T

tuple() โ€“ Tuple

Converts to tuple

tuple([1,2])  # (1,2)
Enter fullscreen mode Exit fullscreen mode

type() โ€“ Type

Returns type of object

type(5)  # <class 'int'>
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“ฅ V

vars() โ€“ Object attributes

Returns __dict__ of an object

class Person: pass
p = Person()
vars(p)  # {}
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“ฆ Z

zip() โ€“ Pair items

Pairs items from multiple iterables

list(zip([1,2], ['a','b']))  # [(1,'a'), (2,'b')]
Enter fullscreen mode Exit fullscreen mode

๐Ÿง  Special

__import__() โ€“ Import module

Used by import system internally

math = __import__('math')  # Same as import math
Enter fullscreen mode Exit fullscreen mode

๐Ÿš€ Summary Table

Function Use
abs(), pow(), round() Math operations
str(), int(), float() Type conversion
list(), dict(), set() Data structure creation
map(), filter(), zip() Functional tools
len(), max(), min() Size/value checks
dir(), help(), type() Debugging & introspection

Google AI Education track image

Build Apps with Google AI Studio ๐Ÿงฑ

This track will guide you through Google AI Studio's new "Build apps with Gemini" feature, where you can turn a simple text prompt into a fully functional, deployed web application in minutes.

Read more โ†’

Top comments (1)

Collapse
 
sawyerwolfe profile image
Sawyer Wolfe โ€ข

Awesome cheat sheet! Iโ€™d love to see a similar guide covering Pythonโ€™s built-in exceptions and common error handling tips next.

Google AI Education track image

Work through these 3 parts to earn the exclusive Google AI Studio Builder badge!

This track will guide you through Google AI Studio's new "Build apps with Gemini" feature, where you can turn a simple text prompt into a fully functional, deployed web application in minutes.

Read more โ†’