Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Python (programming language)
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Typing=== [[File:Python 3.13 Standrd Type Hierarchy-en.svg|thumb|The standard type hierarchy in Python 3]] Python uses [[duck typing]], and it has typed objects but untyped variable names. Type constraints are not checked at definition time; rather, operations on an object may fail at usage time, indicating that the object is not of an appropriate type. Despite being [[dynamically typed]], Python is [[strongly typed]], forbidding operations that are poorly defined (e.g., adding a number and a string) rather than quietly attempting to interpret them. Python allows programmers to define their own types using [[class (computer science)|classes]], most often for [[object-oriented programming]]. New [[object (computer science)|instances]] of classes are constructed by calling the class, for example, {{code|lang=python|code=SpamClass()}} or {{code|lang=python|code=EggsClass()}}); the classes are instances of the [[metaclass]] <code>type</code> (which is an instance of itself), thereby allowing metaprogramming and [[Reflective programming|reflection]]. Before version 3.0, Python had two kinds of classes, both using the same syntax: ''old-style'' and ''new-style''.<ref name="classy"/> Current Python versions support the semantics of only the new style. Python supports [[optional typing|optional type annotations]].<ref name="type_hint-PEP"/><ref>{{Cite web |title=PEP 484 β Type Hints {{!}} peps.python.org |url=https://peps.python.org/pep-0484/ |access-date=2023-11-29 |website=peps.python.org |archive-date=27 November 2023 |archive-url=https://web.archive.org/web/20231127205023/https://peps.python.org/pep-0484/ |url-status=live}}</ref> These annotations are not enforced by the language, but may be used by external tools such as '''mypy''' to catch errors.<ref>{{cite web |title=typing β Support for type hints |url=https://docs.python.org/3/library/typing.html |website=Python documentation |publisher=Python Software Foundation |access-date=22 December 2023 |archive-date=21 February 2020 |archive-url=https://web.archive.org/web/20200221184042/https://docs.python.org/3/library/typing.html |url-status=live}}</ref><ref>{{cite web |url=http://mypy-lang.org/ |title=mypy β Optional Static Typing for Python |access-date=28 January 2017 |archive-date=6 June 2020 |archive-url=https://web.archive.org/web/20200606192012/http://mypy-lang.org/ |url-status=live}}</ref> Mypy also supports a Python compiler called mypyc, which leverages type annotations for optimization.<ref>{{cite web |title=Introduction |url=https://mypyc.readthedocs.io/en/latest/introduction.html |website=mypyc.readthedocs.io |access-date=22 December 2023 |archive-date=22 December 2023 |archive-url=https://web.archive.org/web/20231222000457/https://mypyc.readthedocs.io/en/latest/introduction.html |url-status=live}}</ref> {|class="wikitable" |+ Summary of Python 3's built-in types |- ! Type ! [[immutable object|Mutability]] ! Description ! Syntax examples |- | <code>bool</code> | immutable | [[Boolean value]] | {{code|lang=python|True}}<br />{{code|lang=python|False}} |- | <code>bytearray</code> | mutable | Sequence of [[byte]]s | {{code|lang=python|bytearray(b'Some ASCII')}}<br />{{code|lang=python|bytearray(b"Some ASCII")}}<br />{{code|lang=python|bytearray([119, 105, 107, 105])}} |- | <code>bytes</code> | immutable | Sequence of bytes | {{code|lang=python|b'Some ASCII'}}<br />{{code|lang=python|b"Some ASCII"}}<br />{{code|lang=python|bytes([119, 105, 107, 105])}} |- | <code>complex</code> | immutable | [[Complex number]] with real and imaginary parts | {{code|lang=python|3+2.7j}}<br />{{code|lang=python|3 + 2.7j}} |- | <code>dict</code> | mutable | [[Associative array]] (or dictionary) of key and value pairs; can contain mixed types (keys and values); keys must be a hashable type | {{code|lang=python|{'key1': 1.0, 3: False} }}<br />{{code|lang=python| {} }} |- | <code>types.EllipsisType</code> | immutable | An [[Ellipsis (programming operator)|ellipsis]] placeholder to be used as an index in [[NumPy]] arrays | {{code|lang=python|...}}<br />{{code|lang=python|Ellipsis}} |- | <code>float</code> | immutable | [[Double-precision]] [[floating-point number]]. The precision is machine-dependent, but in practice it is generally implemented as a 64-bit [[IEEE 754]] number with 53 bits of precision.<ref>{{Cite web |title=15. Floating Point Arithmetic: Issues and Limitations β Python 3.8.3 documentation |url=https://docs.python.org/3.8/tutorial/floatingpoint.html#representation-error |access-date=6 June 2020 |website=docs.python.org |quote=Almost all machines today (November 2000) use IEEE-754 floating point arithmetic, and almost all platforms map Python floats to IEEE-754 "double precision". |archive-date=6 June 2020 |archive-url=https://web.archive.org/web/20200606113842/https://docs.python.org/3.8/tutorial/floatingpoint.html#representation-error |url-status=live }}</ref> | {{code|lang=python|1.33333}} |- | <code>frozenset</code> | immutable | Unordered [[set (computer science)|set]], contains no duplicates; can contain mixed types, if hashable | {{nobr|{{code|lang=python|frozenset([4.0, 'string', True])}}}} |- | <code>int</code> | immutable | [[Integer (computer science)|Integer]] of unlimited magnitude<ref name="pep0237"/> | {{code|lang=python|42}} |- | <code>list</code> | mutable | [[list (computer science)|List]], can contain mixed types | {{code|lang=python|[4.0, 'string', True]}}<br />{{code|lang=python|[]}} |- | <code>types.NoneType</code> | immutable | An object representing the absence of a value, often called [[null pointer|null]] in other languages | {{code|lang=python|None}} |- | <code>types.NotImplementedType</code> | immutable | A placeholder that can be returned from [[Operator overloading|overloaded operators]] to indicate unsupported operand types. | {{code|lang=python|NotImplemented}} |- | <code>range</code> | immutable | An ''immutable sequence'' of numbers, commonly used for iterating a specific number of times in <code>for</code> loops<ref>{{cite web |title=Built-in Types |url=https://docs.python.org/3/library/stdtypes.html#typesseq-range |access-date=3 October 2019 |archive-date=14 June 2020 |archive-url=https://web.archive.org/web/20200614194325/https://docs.python.org/3/library/stdtypes.html#typesseq-range |url-status=live}}</ref> | {{code|lang=python|range(β1, 10)}}<br />{{code|lang=python|range(10, β5, β2)}} |- | <code>set</code> | mutable | Unordered [[set (computer science)|set]], contains no duplicates; can contain mixed types, if hashable | {{code|lang=python| {4.0, 'string', True} }}<br />{{code|lang=python|set()}} |- | <code>str</code> | immutable | A [[character string]]: sequence of Unicode codepoints | {{code|lang=python|'Wikipedia'}}<br />{{code|lang=python|"Wikipedia"}}<syntaxhighlight lang="python">"""Spanning multiple lines"""</syntaxhighlight><syntaxhighlight lang="python"> Spanning multiple lines </syntaxhighlight> |- | <code>tuple</code> | immutable | [[Tuple]], can contain mixed types | {{code|lang=python|(4.0, 'string', True)}}<br />{{code|lang=python|('single element',)}}<br />{{code|lang=python|()}} |}
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Python (programming language)
(section)
Add topic