0% found this document useful (0 votes)
0 views4 pages

Python Built in Functions Examples

The document provides a comprehensive overview of Python's built-in functions along with examples for each function. It covers various data types and operations, such as converting types, manipulating collections, and performing mathematical computations. Each function is illustrated with a code snippet demonstrating its usage and expected output.

Uploaded by

avaneesh6808
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views4 pages

Python Built in Functions Examples

The document provides a comprehensive overview of Python's built-in functions along with examples for each function. It covers various data types and operations, such as converting types, manipulating collections, and performing mathematical computations. Each function is illustrated with a code snippet demonstrating its usage and expected output.

Uploaded by

avaneesh6808
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Python Built-in Functions with Examples

int()
int("10") # => 10
float()
float("3.14") # => 3.14
complex()
complex(2, 3) # => (2+3j)
bool()
bool(0) # => False
str()
str(123) # => "123"
list()
list("abc") # => ['a', 'b', 'c']
tuple()
tuple([1, 2]) # => (1, 2)
set()
set([1, 2, 2]) # => {1, 2}
frozenset()
frozenset([1, 2]) # => frozenset({1, 2})
dict()
dict([('a', 1), ('b', 2)]) # => {'a': 1, 'b': 2}
bytes()
bytes("abc", "utf-8") # => b'abc'
bytearray()
bytearray("abc", "utf-8") # => bytearray(b'abc')
memoryview()
memoryview(b"abc")[0] # => 97
len()
len("hello") # => 5
range()
list(range(3)) # => [0, 1, 2]
enumerate()
list(enumerate(['a', 'b'])) # => [(0, 'a'), (1, 'b')]
zip()
list(zip([1, 2], ['a', 'b'])) # => [(1, 'a'), (2, 'b')]
map()
list(map(str.upper, ["a", "b"])) # => ['A', 'B']
filter()
list(filter(lambda x: x > 1, [1, 2, 3])) # => [2, 3]
reversed()
list(reversed([1, 2, 3])) # => [3, 2, 1]
sorted()
sorted([3, 1, 2]) # => [1, 2, 3]
all()
all([True, True]) # => True
any()
any([False, True]) # => True
abs()
abs(-5) # => 5
round()
round(3.1415, 2) # => 3.14
pow()
pow(2, 3) # => 8
divmod()
divmod(10, 3) # => (3, 1)
sum()
sum([1, 2, 3]) # => 6
max()
max([1, 5, 3]) # => 5
min()
min([1, 5, 3]) # => 1
type()
type(123) # => <class 'int'>
id()
id("hello") # => memory address
isinstance()
isinstance(123, int) # => True
issubclass()
issubclass(bool, int) # => True
callable()
callable(print) # => True
dir()
dir([]) # => list of list methods
vars()
class X: pass
vars(X()) # => {}
help()
help(str) # => opens help doc for str
globals()
globals()["x"] = 10 # => {"x": 10, ...}
locals()
def test(): a = 5; return locals() # => {"a": 5}
eval()
eval("2+2") # => 4
exec()
exec("x = 5") # => x is created
compile()
code = compile("print('hi')", "", "exec"); exec(code) # => hi
print()
print("Hello") # => Hello
open()
open("file.txt", "w") # => opens file for writing
getattr()
class X: a = 1
getattr(X, "a") # => 1
setattr()
class X: pass
setattr(X, "a", 100)
hasattr()
class X: a = 1
hasattr(X, "a") # => True
delattr()
class X: a = 1
delattr(X, "a")
property()
class X:
def get(self): return 5
a = property(get)
staticmethod()
class X:
@staticmethod
def f(): return 1
classmethod()
class X:
@classmethod
def f(cls): return cls
super()
class A: def greet(self): return "Hi"
class B(A): def greet(self): return super().greet()
object()
obj = object()
format()
format(255, "x") # => "ff"
hash()
hash("abc")
bin()
bin(5) # => "0b101"
oct()
oct(8) # => "0o10"
hex()
hex(255) # => "0xff"
ascii()
ascii("é") # => "'\xe9'"
ord()
ord("A") # => 65
chr()
chr(65) # => "A"
repr()
repr("hello") # => "'hello'"
__import__()
__import__("math").sqrt(9) # => 3.0

You might also like