Built-In-Functions

Sun 29 June 2025
#tuples
t = (1, 2, 3)
print(len(t))
3
print(max((4, 7, 2)))
7
print(min((4, 7, 2)))
2
print(sum((10, 20, 30)))
60
print(any((0, 0, 1)))
True
print(all((1, 1, 1)))
True
print(tuple([1, 2, 3]))
(1, 2, 3)
print(sorted((5, 1, 3)))
[1, 3, 5]
print((1, 2, 1, 3).count(1))
2
print((5, 2, 3).index(2))
1
print(type((1, 2)))
<class 'tuple'>
a = (1, 2)
b = (3, 4)
print(list(zip(a, b)))
[(1, 3), (2, 4)]
print(tuple(reversed((1, 2, 3))))
(3, 2, 1)
for i, v in enumerate(('a', 'b')):
    print(i, v)
0 a
1 b
print(3 in (1, 2, 3))
True
print(4 not in (1, 2, 3))
True
print(tuple(map(str, (1, 2, 3))))
('1', '2', '3')
print(tuple(filter(lambda x: x > 1, (0, 1, 2, 3))))
(2, 3)
print(isinstance((1, 2), tuple))
True
print(id((1, 2)))
1883265549120
#sets
print(len({1, 2, 3}))
3
s = {1, 2}
s.add(3)
print(s)
{1, 2, 3}
s.remove(2)
print(s)
{1, 3}
s.discard(10)
print(s)
{1, 3}
print(s.pop())
1
s.clear()
print(s)
set()
print(set([1, 2, 3]))
{1, 2, 3}
print({1, 2}.union({2, 3}))
{1, 2, 3}
print({1, 2}.intersection({2, 3}))
{2}
print({1, 2, 3}.difference({2}))
{1, 3}
print({1, 2}.symmetric_difference({2, 3}))
{1, 3}
print({1}.issubset({1, 2}))
True
print({1, 2}.issuperset({1}))
True
print({1, 2}.isdisjoint({3}))
True
a = {1, 2}
b = a.copy()
print(b)
{1, 2}
f = frozenset([1, 2, 3])
print(f)
frozenset({1, 2, 3})
print(sorted({3, 1, 2}))
[1, 2, 3]
print(2 in {1, 2, 3})
True
print(4 not in {1, 2, 3})
True
print(sum({1, 2, 3}))
6
#strings
print(len("hello"))
5
print("abc".upper())
ABC
print("ABC".lower())
abc
print("hello".capitalize())
Hello
print("hello world".title())
Hello World
print("HeLLo".swapcase())
hEllO
print("  hi  ".strip())
hi
print("hi  ".rstrip())
hi
print("hello hello".rfind("l"))
9
print("hello".replace("l", "x"))
hexxo
print("a,b,c".split(","))
['a', 'b', 'c']
print("a b c".rsplit())
['a', 'b', 'c']
print("-".join(["a", "b", "c"]))
a-b-c
print("banana".count("a"))
3
print("abc".isalpha())
True
print("123".isdigit())
True
print("123".isnumeric())
True
print("abc123".isalnum())
True
print("abc".islower())
True
print("ABC".isupper())
True
print("python".startswith("py"))
True
print("main.py".endswith(".py"))
True
print("abc".encode())
b'abc'
print("Hello {}".format("World"))
Hello World
print("{x} + {y}".format_map({'x': 1, 'y': 2}))
1 + 2
print("a=b".partition("="))
('a', '=', 'b')
print("a=b=c".rpartition("="))
('a=b', '=', 'c')
print("a\tb".expandtabs(4))
a   b
print(" ".isspace())
True
print("Hello World".istitle())
True
print("42".zfill(5))
00042
print("hi".ljust(5, '-'))
hi---
print("hi".rjust(5, '-'))
---hi
print("hi".center(6, '*'))
**hi**
print("HELLO".casefold())
hello
table = str.maketrans("ae", "12")
print("apple".translate(table))
1ppl2
print(str.maketrans("abc", "123"))
{97: 49, 98: 50, 99: 51}
#dictionary
d = {"a": 1, "b": 2}
print(len(d))
2
print(d.keys())
dict_keys(['a', 'b'])
print(d.values())
dict_values([1, 2])
print(d.items())
dict_items([('a', 1), ('b', 2)])
print(d.get("a"))
1
print(d.popitem())
('b', 2)
d.update({"c": 3})
print(d)
{'c': 3}
d.clear()
print(d)
{}
print(dict.fromkeys(['x', 'y'], 0))
{'x': 0, 'y': 0}
d = {'a': 1}
print(d.setdefault('b', 2))
2
print("a" in {'a': 1})
True
print("x" not in {'a': 1})
True
d = {'a': 1}
del d['a']
print(d)
{}
print(dict(name="John", age=25))
{'name': 'John', 'age': 25}
a = {'x': 10}
b = a.copy()
print(b)
{'x': 10}
print(dict(zip(['a', 'b'], [1, 2])))
{'a': 1, 'b': 2}
print(sorted({'b': 2, 'a': 1}))
['a', 'b']
print(max({'x': 1, 'y': 3}))
y
d = {'a': 4, 'b': 1}
print(min(d.values()))
1
#files
with open("test.txt", "w") as f:
    f.write("Hello")
with open("test.txt", "r") as f:
    print(f.read())
Hello
with open("test.txt", "r") as f:
    print(f.readline())
Hello
with open("test.txt", "r") as f:
    print(f.readlines())
['Hello']
lines = ["Line1\n", "Line2\n"]
with open("test2.txt", "w") as f:
    f.writelines(lines)
with open("test2.txt", "r") as f:
    print(f.tell())
0
with open("test2.txt", "r") as f:
    f.seek(0)
    print(f.read(5))
Line1
f = open("test.txt", "r")
f.close()
with open("test.txt", "r") as f:
    pass
with open("test.txt", "r") as f:
    print(f.mode)
r
with open("test.txt", "r") as f:
    print(f.name)
test.txt
with open("test.txt", "r") as f:
    print(f.readable())
True
with open("test.txt", "r") as f:
    print(f.readable())
True
with open("test.txt", "r") as f:
    print(f.writable())
False
with open("test.txt", "r") as f:
    print(f.seekable())
True
with open("test.txt", "a") as f:
    f.truncate(5)
with open("test.txt", "r") as f:
    print(f.encoding)
cp1252
with open("test.txt", "w") as f:
    f.write("flush")
    f.flush()
with open("test.txt", "rb") as f:
    print(f.read())
b'flush'
with open("test.txt", "r", encoding="utf-8") as f:
    print(f.read())
flush
try:
    with open("nofile.txt", "r") as f:
        print(f.read())
except FileNotFoundError:
    print("File not found!")
File not found!


Score: 120

Category: basics