Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita10f944

Browse files
committed
Enhance documentation for built-in functions
- Updated examples and explanations for the following built-in functions: - bin() - complex() - delattr() - dict() - divmod() - globals() - hasattr() - hash() - id() - __import__() - isinstance() - iter() - locals() - max() - memoryview() - min() - next() - object() - oct() - ord() - pow() - property() - repr() - reversed() - round() - set() - setattr() - sorted() - sum() - super() - tuple() - vars() - zip()- Added new examples and clarified usage for better understanding.
1 parentc98ec74 commita10f944

34 files changed

+673
-206
lines changed

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ dist-ssr
2323
.vite-ssg-temp
2424
coverage
2525
.env
26+
.github/prompts/custom.prompt.md

‎docs/builtin/bin.md‎

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,20 @@ Python bin() built-in function
1616
</base-disclaimer-content>
1717
</base-disclaimer>
1818

19-
##Examples
19+
The`bin()` function converts an integer into its binary representation. The resulting string is prefixed with "0b" to indicate that it's a binary number.
2020

21-
```python
22-
>>>bin(1)
23-
# '0b1'
24-
25-
>>>bin(10)
26-
# '0b1010'
21+
###Examples
2722

28-
>>>bin(100)
29-
# '0b1100100'
23+
Here are a few examples of how to use`bin()`:
3024

31-
>>>bin(1000)
32-
# '0b1111101000'
25+
```python
26+
# Convert integers to binary
27+
print(bin(2))# Output: 0b10
28+
print(bin(7))# Output: 0b111
29+
30+
# The original examples
31+
print(bin(1))# Output: 0b1
32+
print(bin(10))# Output: 0b1010
33+
print(bin(100))# Output: 0b1100100
34+
print(bin(1000))# Output: 0b1111101000
3335
```
34-
35-
<!-- remove this tag to start editing this page-->
36-
<empty-section />
37-
<!-- remove this tag to start editing this page-->

‎docs/builtin/complex.md‎

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,24 @@ Python complex() built-in function
1616
</base-disclaimer-content>
1717
</base-disclaimer>
1818

19-
##Examples
19+
The`complex()` function creates a complex number. It can take a real and an imaginary part as arguments. If only one argument is provided, it's considered the real part, and the imaginary part is zero.
20+
21+
###Examples
22+
23+
**Creating complex numbers:**
24+
25+
```python
26+
# With real and imaginary parts
27+
print(complex(3,4))# Output: (3+4j)
28+
29+
# With only a real part
30+
print(complex(5))# Output: (5+0j)
31+
32+
# From a string
33+
print(complex("2+3j"))# Output: (2+3j)
34+
```
35+
36+
Here is the original example:
2037

2138
```python
2239
>>>complex(1)
@@ -28,7 +45,3 @@ Python complex() built-in function
2845
>>>complex('100')
2946
# (100+0j)
3047
```
31-
32-
<!-- remove this tag to start editing this page-->
33-
<empty-section />
34-
<!-- remove this tag to start editing this page-->

‎docs/builtin/delattr.md‎

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,37 @@ Python delattr() built-in function
1616
</base-disclaimer-content>
1717
</base-disclaimer>
1818

19+
The`delattr()` function is used to delete an attribute from an object. It takes two arguments: the object and the name of the attribute to delete (as a string). It's the counterpart to`setattr()`.
20+
21+
###Examples
22+
23+
**Deleting an attribute from an object:**
24+
1925
```python
2026
classPerson:
2127
def__init__(self,name,age):
2228
self.name= name
2329
self.age= age
2430

25-
>>> person= Person("John",30)
26-
>>>delattr(person,'age')
27-
>>> person.__dict__
28-
# {'name': 'John'}
31+
person= Person("John",30)
32+
print(person.__dict__)# Output: {'name': 'John', 'age': 30}
2933

34+
delattr(person,'age')
35+
print(person.__dict__)# Output: {'name': 'John'}
36+
```
37+
38+
**Trying to delete a non-existent attribute:**
39+
40+
```python
3041
classCar:
3142
def__init__(self,make,model):
3243
self.make= make
3344
self.model= model
3445

35-
>>>car= Car("Toyota","Corolla")
36-
>>>try:
37-
...delattr(car,'year')
38-
...exceptAttributeErroras e:
39-
...print(f"Error:{e}")
46+
car= Car("Toyota","Corolla")
47+
try:
48+
delattr(car,'year')
49+
exceptAttributeErroras e:
50+
print(f"Error:{e}")
4051
# Error: 'Car' object has no attribute 'year'
4152
```
42-
<!-- remove this tag to start editing this page-->
43-
<empty-section />
44-
<!-- remove this tag to start editing this page-->

‎docs/builtin/dict.md‎

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,38 @@ Python dict() built-in function
1616
</base-disclaimer-content>
1717
</base-disclaimer>
1818

19-
##Examples
19+
The`dict()` constructor creates a new dictionary. A dictionary is a collection of key-value pairs.
20+
21+
You can create an empty dictionary, or create a dictionary from keyword arguments or from an iterable of key-value pairs.
22+
23+
###Examples
24+
25+
**Create an empty dictionary:**
26+
27+
```python
28+
my_dict=dict()
29+
print(my_dict)# Output: {}
30+
```
31+
32+
**Create a dictionary with keyword arguments:**
33+
34+
```python
35+
my_dict=dict(name="John",age=30)
36+
print(my_dict)# Output: {'name': 'John', 'age': 30}
37+
```
38+
39+
**Create a dictionary from a list of tuples:**
40+
41+
```python
42+
my_list= [('name','Jane'), ('age',25)]
43+
my_dict=dict(my_list)
44+
print(my_dict)# Output: {'name': 'Jane', 'age': 25}
45+
```
46+
47+
2048

2149
```python
2250
>>> a=dict()
2351
>>>type(a)
2452
# <class 'dict'>
2553
```
26-
27-
<!-- remove this tag to start editing this page-->
28-
<empty-section />
29-
<!-- remove this tag to start editing this page-->

‎docs/builtin/divmod.md‎

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,18 @@ Python divmod() built-in function
1616
</base-disclaimer-content>
1717
</base-disclaimer>
1818

19-
##Examples
19+
The`divmod()` function takes two numbers as arguments and returns a tuple containing the quotient and the remainder of their integer division. It's a convenient way to get both results in a single call.
20+
21+
###Examples
2022

2123
```python
22-
>>>divmod(2,2)
23-
# (1, 0)
24-
>>>divmod(10,2)
25-
# (5, 0)
26-
>>>divmod(7,2)
27-
# (3, 1)
28-
```
24+
# Get quotient and remainder
25+
quotient, remainder=divmod(10,3)
26+
print(quotient)# Output: 3
27+
print(remainder)# Output: 1
2928

30-
<!-- remove this tag to start editing this page-->
31-
<empty-section />
32-
<!-- remove this tag to start editing this page-->
29+
# Original examples
30+
print(divmod(2,2))# Output: (1, 0)
31+
print(divmod(10,2))# Output: (5, 0)
32+
print(divmod(7,2))# Output: (3, 1)
33+
```

‎docs/builtin/globals.md‎

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,34 @@ Python globals() built-in function
1616
</base-disclaimer-content>
1717
</base-disclaimer>
1818

19-
<!-- remove this tag to start editing this page-->
20-
<empty-section />
21-
<!-- remove this tag to start editing this page-->
19+
The`globals()` function in Python returns a dictionary representing the current global symbol table. This includes all global variables, functions, and other objects in the current scope.
20+
21+
It can be useful for inspecting the global namespace or for dynamically accessing global variables by their string names.
22+
23+
###Examples
24+
25+
```python
26+
# Define a global variable
27+
global_var="I am global"
28+
29+
defmy_function():
30+
# Access global variables using globals()
31+
global_dict=globals()
32+
print(global_dict["global_var"])# Output: I am global
33+
34+
# Modify a global variable
35+
global_dict["global_var"]="Modified global"
36+
37+
my_function()
38+
print(global_var)# Output: Modified global
39+
```
40+
41+
You can also use`globals()` to create new global variables from within a function:
42+
43+
```python
44+
defcreate_global():
45+
globals()["new_global"]="This was created dynamically"
46+
47+
create_global()
48+
print(new_global)# Output: This was created dynamically
49+
```

‎docs/builtin/hasattr.md‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,18 @@ Python hasattr() built-in function
1616
</base-disclaimer-content>
1717
</base-disclaimer>
1818

19-
<!-- remove this tag to start editing this page-->
20-
<empty-section />
21-
<!-- remove this tag to start editing this page-->
19+
The`hasattr()` function checks if an object has a given attribute. It takes the object and the attribute name (as a string) as arguments and returns`True` if the attribute exists, and`False` otherwise.
20+
21+
###Example
22+
23+
```python
24+
classPerson:
25+
name="John"
26+
age=30
27+
28+
p= Person()
29+
30+
print(hasattr(p,'name'))# Output: True
31+
print(hasattr(p,'age'))# Output: True
32+
print(hasattr(p,'email'))# Output: False
33+
```

‎docs/builtin/hash.md‎

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,27 @@ Python hash() built-in function
1616
</base-disclaimer-content>
1717
</base-disclaimer>
1818

19-
##Examples
19+
The`hash()` function returns an integer representing the hash value of an object. This is primarily used by dictionaries to quickly look up keys.
20+
21+
Only "hashable" objects can be passed to`hash()`. An object is hashable if it has a hash value that never changes during its lifetime. All of Python's built-in immutable types (like strings, numbers, and tuples) are hashable, while mutable containers (like lists and dictionaries) are not.
22+
23+
###Examples
2024

2125
```python
22-
>>>hash(1)
23-
# 1
24-
>>>hash('1')
25-
# -3658718886659147670
26-
>>>hash('10')
27-
# 5216539490891759533
28-
```
26+
# Hash of an integer is the integer itself
27+
print(hash(1))# Output: 1
28+
print(hash(1.0))# Output: 1 (1 and 1.0 are equal)
2929

30-
<!-- remove this tag to start editing this page-->
31-
<empty-section />
32-
<!-- remove this tag to start editing this page-->
30+
# Hash of a string (output varies)
31+
print(hash('hello'))
32+
33+
# Hash of a tuple (output varies)
34+
print(hash((1,2,3)))
35+
36+
# Trying to hash a list will raise a TypeError
37+
try:
38+
hash([1,2,3])
39+
exceptTypeErroras e:
40+
print(e)
41+
# Output: unhashable type: 'list'
42+
```

‎docs/builtin/id.md‎

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,21 @@ Python id() built-in function
1616
</base-disclaimer-content>
1717
</base-disclaimer>
1818

19-
##Examples
19+
The`id()` function returns a unique integer that identifies an object in memory. This ID is guaranteed to be unique for the lifetime of the object. It's essentially the memory address of the object.
20+
21+
###Examples
2022

2123
```python
22-
>>>id(1)
23-
# 9788960
24-
>>>id('1')
25-
# 140269689726000
26-
>>>id([1,2])
27-
# 140269672924928
28-
```
24+
x=10
25+
y=10
26+
z=20
2927

30-
<!-- remove this tag to start editing this page-->
31-
<empty-section />
32-
<!-- remove this tag to start editing this page-->
28+
print(id(x))# Output might be something like 4331368528
29+
print(id(y))# Output will be the same as id(x) because Python caches small integers
30+
print(id(z))# Output will be different
31+
32+
# Original examples
33+
print(id(1))
34+
print(id('1'))
35+
print(id([1,2]))
36+
```

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp