Skip to content

Commit 7632a7f

Browse files
committed
Enhance documentation by adding examples and relevant links for built-in functions
1 parent 8971be4 commit 7632a7f

File tree

12 files changed

+252
-139
lines changed

12 files changed

+252
-139
lines changed

docs/builtin/abs.md

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,37 @@ Python abs() built-in function
2020

2121
The `abs()` function in Python is a built-in function that returns the absolute value of a number. It can handle integers, floating-point numbers, and even complex numbers (returning their magnitude). This function is useful when you need to ensure a value is positive, regardless of its original sign.
2222

23+
### Examples
24+
2325
```python
26+
# For integers
2427
>>> abs(-1)
2528
# 1
2629
>>> abs(0)
2730
# 0
28-
>>> abs(1)
29-
# 1
30-
>>> abs(3.14)
31+
32+
# For floats
33+
>>> abs(-3.14)
3134
# 3.14
32-
>>> abs(3 + 2j)
33-
# 3.6055512754639896
34-
>>> abs(0x10)
35+
36+
# For complex numbers (returns magnitude)
37+
>>> abs(3 + 4j)
38+
# 5.0
39+
40+
# For other number systems
41+
>>> abs(0x10) # Hexadecimal
3542
# 16
36-
>>> abs(0b10)
43+
>>> abs(0b10) # Binary
3744
# 2
38-
>>> abs(0o20)
45+
>>> abs(0o20) # Octal
3946
# 16
4047
```
4148

4249
## Relevant links
4350

44-
- <router-link :to="'/builtin/float'">float()</router-link>
45-
- <router-link :to="'/builtin/int'">`int()`</router-link>
46-
- <router-link :to="'/builtin/complex'">complex()</router-link>
51+
- <router-link to="/blog/python-data-types">Blog: Python Data Types</router-link>
52+
- <router-link to="/builtin/pow">pow()</router-link>
53+
- <router-link to="/builtin/round">round()</router-link>
54+
- <router-link to="/builtin/int">int()</router-link>
55+
- <router-link to="/builtin/float">float()</router-link>
56+
- <router-link to="/builtin/complex">complex()</router-link>

docs/builtin/aiter.md

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,51 @@ Python aiter() built-in function
1818

1919
## Introduction
2020

21-
The `aiter()` function in Python is a built-in function that returns an asynchronous iterator for an asynchronous iterable. This is the asynchronous equivalent of the `iter()` function and is essential when working with asynchronous loops (`async for`). It allows you to iterate over objects that produce values asynchronously.
21+
The `aiter()` function returns an asynchronous iterator from an asynchronous iterable object. It's the asynchronous equivalent of the `iter()` function.
22+
23+
You'll typically use `aiter()` implicitly when using an `async for` loop, but you can call it directly if you need to work with the iterator manually, often in conjunction with `anext()`.
2224

2325
## Example
2426

27+
Here's an example of an asynchronous iterable. The `async for` loop automatically calls `aiter()` on the `AsyncCounter` object to get an iterator.
28+
2529
```python
26-
>>> async def aitersync(iterable):
27-
... results = []
28-
... async for x in aiter(iterable):
29-
... results.append(x)
30-
... return iter(results)
30+
import asyncio
31+
32+
class AsyncCounter:
33+
def __init__(self, stop):
34+
self.stop = stop
35+
self.current = 0
36+
37+
def __aiter__(self):
38+
return self
39+
40+
async def __anext__(self):
41+
if self.current < self.stop:
42+
await asyncio.sleep(0.1)
43+
value = self.current
44+
self.current += 1
45+
return value
46+
else:
47+
raise StopAsyncIteration
48+
49+
async def main():
50+
async for number in AsyncCounter(3):
51+
print(number)
52+
53+
# To run this in a real environment:
54+
# asyncio.run(main())
55+
#
56+
# Output:
57+
# 0
58+
# 1
59+
# 2
3160
```
3261

3362
## Relevant links
3463

35-
- <router-link :to="'/builtin/iter'">iter()</router-link>
64+
- <router-link to="/cheatsheet/control-flow">Cheatsheet: Control Flow (async for)</router-link>
65+
- <router-link to="/cheatsheet/functions">Cheatsheet: Functions (async def)</router-link>
66+
- <router-link to="/modules/itertools-module">Module: itertools</router-link>
67+
- <router-link to="/builtin/iter">iter()</router-link>
68+
- <router-link to="/builtin/next">next()</router-link>

docs/builtin/all.md

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,31 @@ The `all()` function in Python is a built-in function that checks if all element
2323
## Examples
2424

2525
```python
26-
>>> all([True, True, True])
26+
# All values are truthy
27+
>>> all([1, 2, 3])
2728
# True
2829

29-
>>> all((0, True, False))
30+
# Contains a falsy value (0)
31+
>>> all([1, 0, 3])
3032
# False
3133

32-
>>> all({1, 1, 1})
34+
# Contains a falsy value (empty string)
35+
>>> all(['a', '', 'c'])
36+
# False
37+
38+
# An empty iterable is considered True
39+
>>> all([])
3340
# True
3441
```
3542

3643
## Relevant links
3744

38-
- <router-link :to="'/builtin/any'">any()</router-link>
39-
- <router-link :to="'/cheatsheet/control-flow'">Control Flow</router-link>
40-
- <router-link :to="'/cheatsheet/comprehensions'">Comprehensions</router-link>
41-
- <router-link :to="'/blog/python-data-types'">Python Data Types</router-link>
45+
- <router-link to="/cheatsheet/control-flow">Cheatsheet: Control Flow</router-link>
46+
- <router-link to="/cheatsheet/comprehensions">Cheatsheet: Comprehensions</router-link>
47+
- <router-link to="/blog/python-data-types">Blog: Python Data Types</router-link>
48+
- <router-link to="/builtin/any">any()</router-link>
49+
- <router-link to="/builtin/bool">bool()</router-link>
50+
- <router-link to="/builtin/list">list()</router-link>
51+
- <router-link to="/builtin/tuple">tuple()</router-link>
52+
- <router-link to="/builtin/set">set()</router-link>
53+
- <router-link to="/builtin/dict">dict()</router-link>

docs/builtin/any.md

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,27 @@ The `any()` function in Python is a built-in function that checks if at least on
2323
## Examples
2424

2525
```python
26-
>>> any([False, False, False])
26+
# All values are falsy
27+
>>> any([0, '', False])
2728
# False
2829

29-
>>> any((0, True, False))
30+
# Contains one truthy value (2)
31+
>>> any([0, 2, False])
3032
# True
3133

32-
>>> any({0, 0, 0})
34+
# An empty iterable is considered False
35+
>>> any([])
3336
# False
3437
```
3538

3639
## Relevant links
3740

38-
- <router-link :to="'/builtin/all'">all()</router-link>
39-
- <router-link :to="'/cheatsheet/control-flow'">Control Flow</router-link>
40-
- <router-link :to="'/cheatsheet/comprehensions'">Comprehensions</router-link>
41-
- <router-link :to="'/blog/python-data-types'">Python Data Types</router-link>
41+
- <router-link to="/cheatsheet/control-flow">Cheatsheet: Control Flow</router-link>
42+
- <router-link to="/cheatsheet/comprehensions">Cheatsheet: Comprehensions</router-link>
43+
- <router-link to="/blog/python-data-types">Blog: Python Data Types</router-link>
44+
- <router-link to="/builtin/all">all()</router-link>
45+
- <router-link to="/builtin/bool">bool()</router-link>
46+
- <router-link to="/builtin/list">list()</router-link>
47+
- <router-link to="/builtin/tuple">tuple()</router-link>
48+
- <router-link to="/builtin/set">set()</router-link>
49+
- <router-link to="/builtin/dict">dict()</router-link>

docs/builtin/ascii.md

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,29 @@ The `ascii()` function in Python is a built-in function that returns a string co
2323
## Examples
2424

2525
```python
26+
# For an ASCII character, it's the same as repr()
2627
>>> ascii('A')
27-
# 'A'
28+
# "'A'"
2829

30+
# For a non-ASCII character, it gets escaped
2931
>>> ascii('ë')
30-
# '\xeb'
32+
# "'\\xeb'"
3133

34+
# For comparison, repr() would not escape it
35+
>>> repr('ë')
36+
# "'ë'"
37+
38+
# It works on iterables too
3239
>>> ascii(['A', 'ë'])
33-
# ['A', '\xeb']
40+
# "['A', '\\xeb']"
3441
```
3542

3643
## Relevant links
3744

38-
- <router-link :to="'/builtin/repr'">repr()</router-link>
39-
- <router-link :to="'/builtin/str'">str()</router-link>
40-
- <router-link :to="'/builtin/chr'">chr()</router-link>
41-
- <router-link :to="'/builtin/ord'">ord()</router-link>
42-
- <router-link :to="'/cheatsheet/manipulating-strings'">Manipulating Strings</router-link>
45+
- <router-link to="/cheatsheet/manipulating-strings">Cheatsheet: Manipulating Strings</router-link>
46+
- <router-link to="/blog/python-data-types">Blog: Python Data Types</router-link>
47+
- <router-link to="/builtin/repr">repr()</router-link>
48+
- <router-link to="/builtin/str">str()</router-link>
49+
- <router-link to="/builtin/bytes">bytes()</router-link>
50+
- <router-link to="/builtin/chr">chr()</router-link>
51+
- <router-link to="/builtin/ord">ord()</router-link>

docs/builtin/bin.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ print(bin(1000)) # Output: 0b1111101000
3636

3737
## Relevant links
3838

39-
- <router-link :to="'/builtin/int'">int()</router-link>
40-
- <router-link :to="'/builtin/hex'">hex()</router-link>
41-
- <router-link :to="'/builtin/oct'">oct()</router-link>
39+
- <router-link to="/blog/python-data-types">Blog: Python Data Types</router-link>
40+
- <router-link to="/cheatsheet/string-formatting">Cheatsheet: String Formatting</router-link>
41+
- <router-link to="/builtin/int">int()</router-link>
42+
- <router-link to="/builtin/hex">hex()</router-link>
43+
- <router-link to="/builtin/oct">oct()</router-link>
44+
- <router-link to="/builtin/format">format()</router-link>

docs/builtin/bool.md

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,30 +22,55 @@ The `bool()` function in Python is a built-in function that converts a value to
2222

2323
## Examples
2424

25+
### Falsy Values
26+
These values are considered `False`:
27+
2528
```python
29+
>>> bool(False)
30+
# False
31+
>>> bool(None)
32+
# False
2633
>>> bool(0)
2734
# False
35+
>>> bool(0.0)
36+
# False
37+
>>> bool('') # empty string
38+
# False
39+
>>> bool([]) # empty list
40+
# False
41+
>>> bool({}) # empty dict
42+
# False
43+
>>> bool(set()) # empty set
44+
# False
45+
```
46+
47+
### Truthy Values
48+
Most other values are considered `True`:
2849

50+
```python
51+
>>> bool(True)
52+
# True
2953
>>> bool(1)
3054
# True
31-
32-
>>> bool(2)
55+
>>> bool(-1)
3356
# True
34-
35-
>>> bool('3')
57+
>>> bool('hello')
3658
# True
37-
38-
>>> bool(False)
39-
# False
40-
41-
>>> bool(True)
59+
>>> bool([1, 2])
60+
# True
61+
>>> bool({'a': 1})
4262
# True
4363
```
4464

4565
## Relevant links
4666

47-
- <router-link :to="'/builtin/int'">int()</router-link>
48-
- <router-link :to="'/builtin/all'">all()</router-link>
49-
- <router-link :to="'/builtin/any'">any()</router-link>
50-
- <router-link :to="'/cheatsheet/control-flow'">Control Flow</router-link>
51-
- <router-link :to="'/blog/python-data-types'">Python Data Types</router-link>
67+
- <router-link to="/cheatsheet/control-flow">Cheatsheet: Control Flow</router-link>
68+
- <router-link to="/blog/python-data-types">Blog: Python Data Types</router-link>
69+
- <router-link to="/builtin/all">all()</router-link>
70+
- <router-link to="/builtin/any">any()</router-link>
71+
- <router-link to="/builtin/int">int()</router-link>
72+
- <router-link to="/builtin/list">list()</router-link>
73+
- <router-link to="/builtin/tuple">tuple()</router-link>
74+
- <router-link to="/builtin/set">set()</router-link>
75+
- <router-link to="/builtin/dict">dict()</router-link>
76+
- <router-link to="/builtin/str">str()</router-link>

docs/builtin/breakpoint.md

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,34 @@ The `breakpoint()` function in Python, introduced in Python 3.7, provides an eas
2424

2525
## Example
2626

27+
Here's how you can use `breakpoint()` to pause execution and inspect variables:
28+
2729
```python
28-
>>> # Create a loop over 5 integers
29-
>>> for i in range(5):
30-
... # Stream i to stdout
31-
... print(i)
32-
... # Create breakpoint at # 3
33-
... if i == 3:
34-
... breakpoint()
35-
...
36-
# 0
37-
# 1
38-
# 2
39-
# 3
40-
# > c:\users\user\path\to\your\project\example.py(24)<module>()
41-
# -> for i in range(5):
42-
# (Pdb)
30+
def calculate_sum(a, b):
31+
result = a + b
32+
# We want to inspect the 'result' before returning
33+
breakpoint()
34+
return result
35+
36+
# When you run this, the debugger will start right after 'result' is calculated
37+
# You can then type 'result' in the (Pdb) prompt to see its value
38+
# To continue execution, type 'c' or 'continue'
39+
final_sum = calculate_sum(10, 20)
40+
print(final_sum)
41+
42+
# (Pdb) result
43+
# 30
44+
# (Pdb) c
45+
# 30
4346
```
4447

4548
## Relevant links
4649

47-
- <router-link :to="'/cheatsheet/debugging'">Debugging</router-link>
48-
- <router-link :to="'/builtin/print'">print()</router-link>
49-
- <router-link :to="'/cheatsheet/control-flow'">Control Flow</router-link>
50+
- <router-link to="/cheatsheet/debugging">Cheatsheet: Debugging</router-link>
51+
- <router-link to="/cheatsheet/control-flow">Cheatsheet: Control Flow</router-link>
52+
- <router-link to="/builtin/print">print()</router-link>
53+
- <router-link to="/builtin/input">input()</router-link>
54+
- <router-link to="/builtin/eval">eval()</router-link>
55+
- <router-link to="/builtin/exec">exec()</router-link>
56+
- <router-link to="/builtin/globals">globals()</router-link>
57+
- <router-link to="/builtin/locals">locals()</router-link>

0 commit comments

Comments
 (0)