Sideway
output.to from Sideway
Draft for Information Only

Content

Python Literal Constant
 Types of Python Literal Constant
  Scalar Literals
   String Literals
    Unicode Literials
   Bytes Literals
    Bytearray Literals
   Numeric Literals
    Integer Literals
    Floating Point Literals
    Imaginary Literals
   Special Literals
    Boolean Literals
    None Literals
  Literal Collections
   List Literals
   Tuple Literals
   Dictionary Literals
   Set Literals
    Frozenset Literals
 Source and Reference

Python Literal Constant

In 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 Constant

Literals are notations for constant values of some built-in Python types.
image

Scalar Literals

A scalar literal is a literal that is considered as a single value.
literal ::=  stringliteral | bytesliteral
             | integer | floatnumber | imagnumber

String Literals

A 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 Literials
An 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 Literals

A 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 Literals
Since 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 Literals

The 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 Literals
An 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 Literals
Integer literal in Python 3 is already long integer literals. The suffix l or L used to specify a long integer literal is no longer supported.
Floating Point Literals
A 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") ["+" | "-"] digitpart
Imaginary Literals
An imaginary literal is a sequence of characters for representing the imaginary part of complex number. imagnumber::=(floatnumber | digitpart) ("j" | "J")

Special Literals

Special literals are reserved keywords that are used as literal constants.
Boolean Literals
A Boolean literal can have any of the two values: Python True or Python False. True and False are simply built-in named values for the Boolean objects. True and False are defined as Boolean logic True and False.
None Literals
None is a special literal to name the Python None type class. None is simply a built-in name for the Python None object. None is defined as a null value, no value at all, or the object has not been created.

Literal Collections

Literal 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 Literals

A 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 Literals

A 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 Literals

A 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 Literals

A 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 Literals
A 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 LinksValid XHTML 1.0 Transitional Valid CSS!Nu Html Checker Firefox53 Chromena IExplorerna
IMAGE

Home 5

Business

Management

HBR 3

Information

Recreation

Hobbies 8

Culture

Chinese 1097

English 339

Reference 79

Computer

Hardware 249

Software

Application 213

Digitization 32

Latex 52

Manim 205

KB 1

Numeric 19

Programming

Web 289

Unicode 504

HTML 66

CSS 65

SVG 46

ASP.NET 270

OS 429

DeskTop 7

Python 72

Knowledge

Mathematics

Formulas 8

Algebra 84

Number Theory 206

Trigonometry 31

Geometry 34

Coordinate Geometry 2

Calculus 67

Complex Analysis 21

Engineering

Tables 8

Mechanical

Mechanics 1

Rigid Bodies

Statics 92

Dynamics 37

Fluid 5

Fluid Kinematics 5

Control

Process Control 1

Acoustics 19

FiniteElement 2

Natural Sciences

Matter 1

Electric 27

Biology 1

Geography 1


Copyright © 2000-2024 Sideway . All rights reserved Disclaimers last modified on 06 September 2019