TOCPython Draft for Information Only
ContentPython Literal Constant
Python Literal ConstantIn general, a python literal is used as its literal value in Python script literally. A literal is always a constant because the value of a Python literal cannot be changed. Hence, all Python literals are referred to as Python literal constants.Types of Python Literal ConstantLiterals are notations for constant values of some built-in Python types.![]() Scalar LiteralsA scalar literal is a literal that is considered as a single value.literal ::= stringliteral | bytesliteral | integer | floatnumber | imagnumber String LiteralsA string literal is a sequence of characters from any source characters.stringliteral::=[stringprefix](shortstring | longstring) stringprefix::="r" | "u" | "R" | "U" | "f" | "F" | "fr" | "Fr" | "fR" | "FR" | "rf" | "rF" | "Rf" | "RF" shortstring::="'" shortstringitem* "'" | '"' shortstringitem* '"' longstring::="'''" longstringitem* "'''" | '"""' longstringitem* '"""' shortstringitem::=shortstringchar | stringescapeseq longstringitem::=longstringchar | stringescapeseq shortstringchar::=<any source character except "\" or newline or the quote> longstringchar::=<any source character except "\"> stringescapeseq::="\" <any source character> Unicode LiterialsAn unicode string literal is only used to restore support for the unicode legacy literal. Python reads program text as Unicode code points; the encoding of a source file can be given by an encoding declaration and defaults to UTF-8.Bytes LiteralsA bytes literal is a sequences of integers with each integer representing one single byte having ASCII value between 0 and 255 of a byte literal. However, bytes with a numeric value of 128 or greater must be expressed with escapes.bytesliteral::=bytesprefix(shortbytes | longbytes) bytesprefix::="b" | "B" | "br" | "Br" | "bR" | "BR" | "rb" | "rB" | "Rb" | "RB" shortbytes::="'" shortbytesitem* "'" | '"' shortbytesitem* '"' longbytes::="'''" longbytesitem* "'''" | '"""' longbytesitem* '"""' shortbytesitem::=shortbyteschar | bytesescapeseq longbytesitem::=longbyteschar | bytesescapeseq shortbyteschar::=<any ASCII character except "\" or newline or the quote> longbyteschar::=<any ASCII character except "\"> bytesescapeseq::="\" <any ASCII character> Bytearray LiteralsSince bytes literal is an immutable array of bytes (PyString), bytearray literal is designed as a mutable counterpart to bytes literal. In other words, bytearray literal is a mutable array of bytes (PyBytes).Numeric LiteralsThe numeric literals used in Python are integers, floating point numbers, and imaginary numbers. There are no complex literals. Complex numbers can be formed by adding a real number and an imaginary number together.Integer LiteralsAn integer literal is a sequence of characters for representing an integer. integer::=decinteger | bininteger | octinteger | hexinteger decinteger::=nonzerodigit (["_"] digit)* | "0"+ (["_"] "0")* bininteger::="0" ("b" | "B") (["_"] bindigit)+ octinteger::="0" ("o" | "O") (["_"] octdigit)+ hexinteger::="0" ("x" | "X") (["_"] hexdigit)+ nonzerodigit::="1"..."9" digit::="0"..."9" bindigit::="0" | "1" octdigit::="0"..."7" hexdigit::=digit | "a"..."f" | "A"..."F"Long Integer LiteralsInteger literal in Python 3 is already long integer literals. The suffixFloating Point LiteralsA floating point literal is a sequence of characters for representing an floating point number with an integer part, a fractional part, and an exponent part. floatnumber::=pointfloat | exponentfloat pointfloat::=[digitpart] fraction | digitpart "." exponentfloat::=(digitpart | pointfloat) exponent digitpart::=digit (["_"] digit)* fraction::="." digitpart exponent::=("e" | "E") ["+" | "-"] digitpartImaginary LiteralsAn imaginary literal is a sequence of characters for representing the imaginary part of complex number. imagnumber::=(floatnumber | digitpart) ("j" | "J")Special LiteralsSpecial literals are reserved keywords that are used as literal constants.Boolean LiteralsA Boolean literal can have any of the two values: PythonNone LiteralsLiteral CollectionsLiteral collections is a collection of literals featured by the form of collections. The typical literal collections are list literal, tuple literal, dictionary literal, set literal, and frozenset literal.List LiteralsA list literal is a container used to contain a variable length sequence of Python objects. Python objects in the list are ordered, changeable, and allowing duplicate members. A list is enclosed by a pair of square brackets. e.g. [], [5,6,8].Tuple LiteralsA tuple literal is a container used to contain a fixed length sequence of Python objects. Python objects in the tuple are ordered, unchangeable, and allowing duplicate members. A tuple is enclosed by a pair of parentheses. e.g. (), (5,6,7).Dictionary LiteralsA dictionary literal is a container used to contain a variable length sequence of Python object pairs. Python object pairs in the dictionary are unordered, changeable, and not allowing duplicate members. The Python object pair of a dictionary is the key and value of the dictionary. A dictionary is enclosed by a pair of curly braces. e.g. {}, {'x':5}.Set LiteralsA set literal is a container used to contain a variable length sequence of Python objects. Python objects in the set are unordered, changeable, and not allowing duplicate members. A set is also enclosed by a pair of curly braces. e.g. {85,6,7}. But elements of a set are single independent Python objects instead of a pair of Python objects of the form key-value pair. Beside an empty set can only be created by function set() without any argument or with no element, since empty curly braces will create an empty dictionary in Python.Frozenset LiteralsA frozenset literal is a container used to contain a variable length sequence of Python objects. Python objects in the frozenset are unordered, unchangeable, and not allowing duplicate members. A frozenset is always displayed as an immutable copy of an ordinaryl set. e.g. frozenset({85,6,7}). And a frozenset is always created by using function frozenset() to convert a collection into frozenset.Source and Reference
©sideway ID: 201200031 Last Updated: 12/31/2020 Revision: 0 Latest Updated Links
![]() ![]() ![]() ![]() ![]() |
![]() Home 5 Business Management HBR 3 Information Recreation Hobbies 8 Culture Chinese 1097 English 339 Travel 18 Reference 79 Computer Hardware 254 Software Application 213 Digitization 37 Latex 52 Manim 205 KB 1 Numeric 19 Programming Web 289 Unicode 504 HTML 66 CSS 65 SVG 46 ASP.NET 270 OS 431 DeskTop 7 Python 72 Knowledge Mathematics Formulas 8 Set 1 Logic 1 Algebra 84 Number Theory 206 Trigonometry 31 Geometry 34 Calculus 67 Engineering Tables 8 Mechanical Rigid Bodies Statics 92 Dynamics 37 Fluid 5 Control Acoustics 19 Natural Sciences Matter 1 Electric 27 Biology 1 |
Copyright © 2000-2025 Sideway . All rights reserved Disclaimers last modified on 06 September 2019