Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(168403)

Side by Side Diff: Doc/library/functions.rst

Issue 26506: hex() documentation: mention "%x" % int
Patch Set: Created 3 years, 8 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 .. XXX document all delegations to __special__ methods 1 .. XXX document all delegations to __special__ methods
2 .. _built-in-funcs: 2 .. _built-in-funcs:
3 3
4 Built-in Functions 4 Built-in Functions
5 ================== 5 ==================
6 6
7 The Python interpreter has a number of functions and types built into it that 7 The Python interpreter has a number of functions and types built into it that
8 are always available. They are listed here in alphabetical order. 8 are always available. They are listed here in alphabetical order.
9 9
10 =================== ================= ================== ================ == ================== 10 =================== ================= ================== ================ == ==================
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
73 .. function:: ascii(object) 73 .. function:: ascii(object)
74 74
75 As :func:`repr`, return a string containing a printable representation of an 75 As :func:`repr`, return a string containing a printable representation of an
76 object, but escape the non-ASCII characters in the string returned by 76 object, but escape the non-ASCII characters in the string returned by
77 :func:`repr` using ``\x``, ``\u`` or ``\U`` escapes. This generates a string 77 :func:`repr` using ``\x``, ``\u`` or ``\U`` escapes. This generates a string
78 similar to that returned by :func:`repr` in Python 2. 78 similar to that returned by :func:`repr` in Python 2.
79 79
80 80
81 .. function:: bin(x) 81 .. function:: bin(x)
82 82
83 Convert an integer number to a binary string. The result is a valid Python 83 Convert an integer number to a binary string prefixed with "0b". The result i s
84 expression. If *x* is not a Python :class:`int` object, it has to define an 84 a valid Python expression. If *x* is not a Python :class:`int` object, it has
85 :meth:`__index__` method that returns an integer. 85 to define an :meth:`__index__` method that returns an integer. Some examples:
86
87 >>> bin(12)
88 '0b1100'
89 >>> bin(-10)
90 '-0b1010'
91
92 If prefix "0b" is desired or not, you can use either of the following ways. S ee
mdk 2016/12/05 21:22:48 I'm not a big fan of "is desired or not", why not
93 also :func:`format` for more information.
94
95 >>> num = 14
96 >>> format(num, '#b'), format(num, 'b')
97 ('0b1110', '1110')
98 >>> f'{num:#b}', f'{num:b}'
99 ('0b1110', '1110')
86 100
87 101
88 .. class:: bool([x]) 102 .. class:: bool([x])
89 103
90 Return a Boolean value, i.e. one of ``True`` or ``False``. *x* is converted 104 Return a Boolean value, i.e. one of ``True`` or ``False``. *x* is converted
91 using the standard :ref:`truth testing procedure <truth>`. If *x* is false 105 using the standard :ref:`truth testing procedure <truth>`. If *x* is false
92 or omitted, this returns ``False``; otherwise it returns ``True``. The 106 or omitted, this returns ``False``; otherwise it returns ``True``. The
93 :class:`bool` class is a subclass of :class:`int` (see :ref:`typesnumeric`). 107 :class:`bool` class is a subclass of :class:`int` (see :ref:`typesnumeric`).
94 It cannot be subclassed further. Its only instances are ``False`` and 108 It cannot be subclassed further. Its only instances are ``False`` and
95 ``True`` (see :ref:`bltin-boolean-values`). 109 ``True`` (see :ref:`bltin-boolean-values`).
(...skipping 523 matching lines...) Expand 10 before | Expand all | Expand 10 after
619 633
620 This function is added to the built-in namespace by the :mod:`site` module. 634 This function is added to the built-in namespace by the :mod:`site` module.
621 635
622 .. versionchanged:: 3.4 636 .. versionchanged:: 3.4
623 Changes to :mod:`pydoc` and :mod:`inspect` mean that the reported 637 Changes to :mod:`pydoc` and :mod:`inspect` mean that the reported
624 signatures for callables are now more comprehensive and consistent. 638 signatures for callables are now more comprehensive and consistent.
625 639
626 640
627 .. function:: hex(x) 641 .. function:: hex(x)
628 642
629 Convert an integer number to a lowercase hexadecimal string 643 Convert an integer number to a lowercase hexadecimal string prefixed with "0x ".
630 prefixed with "0x", for example: 644 If x is not a Python :class:`int` object, it has to define an __index__() met hod
645 that returns an integer. Some examples:
631 646
632 >>> hex(255) 647 >>> hex(255)
633 '0xff' 648 '0xff'
634 >>> hex(-42) 649 >>> hex(-42)
635 '-0x2a' 650 '-0x2a'
636 651
637 If x is not a Python :class:`int` object, it has to define an __index__() 652 If you want to convert an integer number to uppercase or lowercase hexadecima l
638 method that returns an integer. 653 string either with prefix or not, you can use either of the following ways. S ee
654 also :func:`format` for more information.
655
656 >>> num=255
657 >>> '%#x' % num, '%x' % num, '%X' % num
658 ('0xff', 'ff', 'FF')
659 >>> format(num, '#x'), format(num, 'x'), format(num, 'X')
660 ('0xff', 'ff', 'FF')
661 >>> f'{num:#x}', f'{num:x}', f'{num:X}'
662 ('0xff', 'ff', 'FF')
639 663
640 See also :func:`int` for converting a hexadecimal string to an 664 See also :func:`int` for converting a hexadecimal string to an
641 integer using a base of 16. 665 integer using a base of 16.
642 666
643 .. note:: 667 .. note::
644 668
645 To obtain a hexadecimal string representation for a float, use the 669 To obtain a hexadecimal string representation for a float, use the
646 :meth:`float.hex` method. 670 :meth:`float.hex` method.
647 671
648 672
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after
858 function does not accept any arguments. 882 function does not accept any arguments.
859 883
860 .. note:: 884 .. note::
861 885
862 :class:`object` does *not* have a :attr:`~object.__dict__`, so you can't 886 :class:`object` does *not* have a :attr:`~object.__dict__`, so you can't
863 assign arbitrary attributes to an instance of the :class:`object` class. 887 assign arbitrary attributes to an instance of the :class:`object` class.
864 888
865 889
866 .. function:: oct(x) 890 .. function:: oct(x)
867 891
868 Convert an integer number to an octal string. The result is a valid Python 892 Convert an integer number to an octal string prefixed with "0o". The result is
869 expression. If *x* is not a Python :class:`int` object, it has to define an 893 a valid Python expression. If *x* is not a Python :class:`int` object, it has to
870 :meth:`__index__` method that returns an integer. 894 define an :meth:`__index__` method that returns an integer. For example:
895
896 >>> oct(10)
897 '0o12'
898 >>> oct(-56)
899 '-0o70'
900
901 If you want to convert an integer number to octal string either with prefix " 0o"
902 or not, you can use either of the following ways. See also :func:`format` for
903 more information.
904
905 >>> num = 10
906 >>> '%#o' % num, '%o' % num
907 ('0o12', '12')
908 >>> format(num, '#o'), format(num, 'o')
909 ('0o12', '12')
910 >>> f'{num:#o}', f'{num:o}'
911 ('0o12', '12')
871 912
872 913
873 .. index:: 914 .. index::
874 single: file object; open() built-in function 915 single: file object; open() built-in function
875 916
876 .. function:: open(file, mode='r', buffering=-1, encoding=None, errors=None, new line=None, closefd=True, opener=None) 917 .. function:: open(file, mode='r', buffering=-1, encoding=None, errors=None, new line=None, closefd=True, opener=None)
877 918
878 Open *file* and return a corresponding :term:`file object`. If the file 919 Open *file* and return a corresponding :term:`file object`. If the file
879 cannot be opened, an :exc:`OSError` is raised. 920 cannot be opened, an :exc:`OSError` is raised.
880 921
(...skipping 705 matching lines...) Expand 10 before | Expand all | Expand 10 after
1586 .. versionchanged:: 3.3 1627 .. versionchanged:: 3.3
1587 Negative values for *level* are no longer supported (which also changes 1628 Negative values for *level* are no longer supported (which also changes
1588 the default value to 0). 1629 the default value to 0).
1589 1630
1590 1631
1591 .. rubric:: Footnotes 1632 .. rubric:: Footnotes
1592 1633
1593 .. [#] Note that the parser only accepts the Unix-style end of line convention. 1634 .. [#] Note that the parser only accepts the Unix-style end of line convention.
1594 If you are reading the code from a file, make sure to use newline conversion 1635 If you are reading the code from a file, make sure to use newline conversion
1595 mode to convert Windows or Mac-style newlines. 1636 mode to convert Windows or Mac-style newlines.
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+