Sideway
output.to from Sideway
Draft for Information Only

Content

Python Built-In Types
 Numeric Types
  Integer
  Floating Point Number
  Complex Number
 Sequence Types
  Lists
  Tuples
  Ranges
  Text Sequence Type
  Binary Sequence Types
   Bytes Objects
   Bytearray Objects
   Memory Views
 Set Types
 Mapping Types
  Dictionary view objects
 Context Manager Types
 Other Built-in Types
 Special Attributes
 Source and Reference

Python Built-In Types

The principal Python standard built-in types used by Python interpreter are: numerics, sequences, mappings, classes, instances, and exceptions.
Some collection classes are mutable. The methods that add, subtract, or rearrange their members in place, and don’t return a specific item, never return the collection instance itself but None.

Some operations are supported by several object types; in particular, practically all objects can be compared for equality, tested for truth value, and converted to a string (with the repr() function or the slightly different str() function). The latter function is implicitly used when an object is written by the print() function.

Numeric Types

The Python numeric types are integers, floating point numbers, and complex numbers. Besides, booleans are implemented as a subtype of integers.

Integer

Integers int have unlimited precision. The constructor int() can be used to create a integer. Integer literals can also be used to create integers. For example,
  • Decimal integer: decinteger e.g. 1234567890
  • Binary integer: bininteger e.g. 0b10,0B10
  • Octal integer: octinteger e.g. 0o1234567, 0O1234567
  • Hexadecimal integer: hexinteger e.g. 0x1234567890ABCDEF, 0X1234567890ABCDEF

Floating Point Number

Floating point numbers are usually implemented using double in C. Information about the precision and internal representation of Python floating point numbers can be obtained in sys.float_info. The standard library also includes the additional numeric types fractions.Fraction for rationals, and decimal.Decimal for floating-point numbers with user-definable precision. The constructor float() can also be used to create a integer. Floating point literals can also be used to create floating point number. For example,
  • Decimal point floating point number: pointfloat e.g. 123456.7890
  • Exponent floating point number: exponentfloat e.g. 7e10,7.2E10

Complex Number

Complex numbers have a real and imaginary part. The real part of a complex number can be extracted by z.real and the imaginary part of the complex number can be extracted by z.imag. The constructor complex() can be used to create a integer. The real part of a complex number can be created by the floating point numeral or integer numeral. While the imaginary part of the complex number can be created by the imaginary numeral which is similar to the real part but a specifier j or J is appended to the numeric literal. A complex number with a zero real part can be created by a imaginary literal only. For example, 1.3+0.8j, 5.6+8.4J

Sequence Types

The three basic sequence types are lists, tuples, and ranges. Besides, booleans are implemented as a subtype of integers. Besides, binary data and text strings are other typical sequence types.

Lists

Lists are mutable sequences, typically used to store collections of homogeneous items.
class list([iterable])
Lists may be constructed by:
  • Using a pair of square brackets to denote the empty list: []
  • Using square brackets, separating items with commas: [a], [a, b, c]
  • Using a list comprehension: [x for x in iterable]
  • Using the type constructor: list() or list(iterable)

Tuples

tuples are immutable sequences, typically used to store collections of heterogeneous data. Tuples are also used for cases where an immutable sequence of homogeneous data is needed.
class tuple([iterable])
Tuples may be constructed by:
  • Using a pair of parentheses to denote the empty tuple: ()
  • Using a trailing comma for a singleton tuple: a, or (a,)
  • Separating items with commas: a, b, c or (a, b, c)
  • Using the tuple() built-in: tuple() or tuple(iterable)
In fact, a tuple is constructed by the comma, not the parentheses. The parentheses are optional and used as tuple separators, except in the empty tuple case, or when they are needed to avoid syntactic ambiguity.

Ranges

The range type represents an immutable sequence of numbers and is commonly used for looping a specific number of times in for loops.
class range(stop)
class range(start, stop[, step])
The arguments to the range constructor must be integers (either built-in int or any object that implements the __index__ special method). If the step argument is omitted, it defaults to 1. If the start argument is omitted, it defaults to 0. If step is zero, ValueError is raised.
    For a positive step, the contents of a range r are determined by the formula r[i] = start + step*i where i >= 0 and r[i] < stop.

For a negative step, the contents of the range are still determined by the formula r[i] = start + step*i, but the constraints are i >= 0 and r[i] > stop.

A range object will be empty if r[0] does not meet the value constraint. Ranges do support negative indices, but these are interpreted as indexing from the end of the sequence determined by the positive indices.

Ranges containing absolute values larger than sys.maxsize are permitted but some features (such as len()) may raise OverflowError.

Text Sequence Type

Strings are implemented as text sequence type, str. Textual data in Python is handled with str objects, or strings. Strings are immutable sequences of Unicode code points. String literals may be constructed by:
  • Single quotes: 'allows embedded "double" quotes'
  • Double quotes: "allows embedded 'single' quotes".
  • Triple quoted: '''Three single quotes''', """Three double quotes"""
Triple quoted strings may span multiple lines - all associated whitespace will be included in the string literal.

String literals that are part of a single expression and have only whitespace between them will be implicitly converted to a single string literal. That is, ("spam " "eggs") == "spam eggs".
    Strings may also be created from other objects using the str constructor.

Since there is no separate “character” type, indexing a string produces strings of length 1. That is, for a non-empty string s, s[0] == s[0:1].

There is also no mutable string type, but str.join() or io.StringIO can be used to efficiently construct strings from multiple fragments.
 class str(object='')
class str(object=b'', encoding='utf-8', errors='strict')

Binary Sequence Types

Binary datas are implemented as binary sequence types, bytes, bytearray, memoryview.
The core built-in types for manipulating binary data are bytes and bytearray. They are supported by memoryview which uses the buffer protocol to access the memory of other binary objects without needing to make a copy.

The array module supports efficient storage of basic data types like 32-bit integers and IEEE754 double-precision floating values.

Bytes Objects

bytes objects are immutable sequences of single bytes. Since many major binary protocols are based on the ASCII text encoding, bytes objects offer several methods that are only valid when working with ASCII compatible data and are closely related to string objects in a variety of other ways.
class bytes([source[, encoding[, errors]]])
Firstly, the syntax for bytes literals is largely the same as that for string literals, except that a b prefix is added:
  • Single quotes: b'still allows embedded "double" quotes'
  • Double quotes: b"still allows embedded 'single' quotes"
  • Triple quoted: b'''3 single quotes''', b"""3 double quotes"""
Only ASCII characters are permitted in bytes literals (regardless of the declared source code encoding). Any binary values over 127 must be entered into bytes literals using the appropriate escape sequence.
    Bytes objects can be created by
  • A zero-filled bytes object of a specified length: bytes(10)
  • From an iterable of integers: bytes(range(20))
  • Copying existing binary data via the buffer protocol: bytes(obj)

Bytearray Objects

bytearray objects are a mutable counterpart to bytes objects.
class bytearray([source[, encoding[, errors]]])
There is no dedicated literal syntax for bytearray objects, instead they are always created by calling the constructor:
  • Creating an empty instance: bytearray()
  • Creating a zero-filled instance with a given length: bytearray(10)
  • From an iterable of integers: bytearray(range(20))
  • Copying existing binary data via the buffer protocol: bytearray(b'Hi!')
Since bytearray objects are sequences of integers (akin to a list), for a bytearray object b, b[0] will be an integer, while b[0:1] will be a bytearray object of length 1. (This contrasts with text strings, where both indexing and slicing will produce a string of length 1)

The representation of bytearray objects uses the bytes literal format (bytearray(b'...')) since it is often more useful than e.g. bytearray([46, 46, 46]). You can always convert a bytearray object into a list of integers using list(b).

Memory Views

memoryview objects allow Python code to access the internal data of an object that supports the buffer protocol without copying.
class memoryview(obj)
Create a memoryview that references obj. obj must support the buffer protocol. Built-in objects that support the buffer protocol include bytes and bytearray.

A memoryview has the notion of an element, which is the atomic memory unit handled by the originating object obj. For many simple types such as bytes and bytearray, an element is a single byte, but other types such as array.array may have bigger elements.

Set Types

Set types are set,frozenset.
    A set object is an unordered collection of distinct hashable objects. Common uses include membership testing, removing duplicates from a sequence, and computing mathematical operations such as intersection, union, difference, and symmetric difference. (For other containers see the built-in dict, list, and tuple classes, and the collections module.)

Like other collections, sets support x in set, len(set), and for x in set. Being an unordered collection, sets do not record element position or order of insertion. Accordingly, sets do not support indexing, slicing, or other sequence-like behavior.

There are currently two built-in set types, set and frozenset. The set type is mutable — the contents can be changed using methods like add() and remove(). Since it is mutable, it has no hash value and cannot be used as either a dictionary key or as an element of another set. The frozenset type is immutable and hashable — its contents cannot be altered after it is created; it can therefore be used as a dictionary key or as an element of another set.

Non-empty sets (not frozensets) can be created by placing a comma-separated list of elements within braces, for example: {'jack', 'sjoerd'}, in addition to the set constructor.

The constructors for both classes work the same:
class set([iterable])
class frozenset([iterable])
Return a new set or frozenset object whose elements are taken from iterable. The elements of a set must be hashable. To represent sets of sets, the inner sets must be frozenset objects. If iterable is not specified, a new empty set is returned.

Mapping Types

Mapping types is dict.
    A mapping object maps hashable values to arbitrary objects. Mappings are mutable objects. There is currently only one standard mapping type, the dictionary. (For other containers see the built-in list, set, and tuple classes, and the collections module.)

A dictionary’s keys are almost arbitrary values. Values that are not hashable, that is, values containing lists, dictionaries or other mutable types (that are compared by value rather than by object identity) may not be used as keys. Numeric types used for keys obey the normal rules for numeric comparison: if two numbers compare equal (such as 1 and 1.0) then they can be used interchangeably to index the same dictionary entry. (Note however, that since computers store floating-point numbers as approximations it is usually unwise to use them as dictionary keys.)

Dictionaries can be created by placing a comma-separated list of key: value pairs within braces, for example: {'jack': 4098, 'sjoerd': 4127} or {4098: 'jack', 4127: 'sjoerd'}, or by the dict constructor.
  • class dict(**kwarg)
  • class dict(mapping, **kwarg)
  • class dict(iterable, **kwarg)
Return a new dictionary initialized from an optional positional argument and a possibly empty set of keyword arguments.

If no positional argument is given, an empty dictionary is created. If a positional argument is given and it is a mapping object, a dictionary is created with the same key-value pairs as the mapping object. Otherwise, the positional argument must be an iterable object. Each item in the iterable must itself be an iterable with exactly two objects. The first object of each item becomes a key in the new dictionary, and the second object the corresponding value. If a key occurs more than once, the last value for that key becomes the corresponding value in the new dictionary.

If keyword arguments are given, the keyword arguments and their values are added to the dictionary created from the positional argument. If a key being added is already present, the value from the keyword argument replaces the value from the positional argument.

Dictionary view objects

The objects returned by dict.keys(), dict.values() and dict.items() are view objects. They provide a dynamic view on the dictionary’s entries, which means that when the dictionary changes, the view reflects these changes.

Context Manager Types

Python’s with statement supports the concept of a runtime context defined by a context manager.

Other Built-in Types

The interpreter supports several other kinds of objects. 
  • Modules
  • Classes and Class Instances
  • Functions
  • Methods
  • Code Objects
  • Type Objects
  • The Null Object
  • The Ellipsis Object
  • The NotImplemented Object
  • Boolean Values
  • Internal Objects

Special Attributes

  • object.__dict__
  • instance.__class__
  • class.__bases__
  • definition.__name__
  • class.__mro__
  • class.mro()
  • class.__subclasses__()

Source and Reference


©sideway

ID: 200702402 Last Updated: 24/7/2020 Revision: 0

IMAGE

Home 5

Business

Management

HBR 3

Information

Recreation

Hobbies 8

Culture

Chinese 1097

English 337

Reference 67

Computer

Hardware 151

Software

Application 202

Digitization 25

Latex 10

Manim 159

Numeric 19

Programming

Web 285

Unicode 495

HTML 65

CSS 59

ASP.NET 194

OS 391

DeskTop 7

Python 46

Knowledge

Mathematics

Formulas 8

Algebra 29

Number Theory 206

Trigonometry 18

Geometry 18

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

Electric 27

Biology 1

Geography 1


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