-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
1429 lines (1429 loc) · 66.7 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<script type="module" src="index.js"></script>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Float32Array Example</title>
<style>
.highlight {
background-color: rgba(
255,
255,
0,
1
); /* Fully opaque yellow by default */
}
</style>
</head>
<body>
<div>
<label for="searchText">Search:</label>
<input type="text" id="searchText" />
<button id="searchButton">Search</button>
</div>
<div id="text-contents">
<p>
<b>Python</b> is a high-level, general-purpose programming language. Its
design philosophy emphasizes code readability with the use of
significant indentation.
</p>
<p>
Python is dynamically typed and garbage-collected. It supports multiple
programming paradigms, including structured (particularly procedural),
object-oriented and functional programming. It is often described as a
"batteries included" language due to its comprehensive standard library.
</p>
<p>
Guido van Rossum began working on Python in the late 1980s as a
successor to the ABC programming language and first released it in 1991
as Python 0.9.0. Python 2.0 was released in 2000. Python 3.0, released
in 2008, was a major revision not completely backward-compatible with
earlier versions. Python 2.7.18, released in 2020, was the last release
of Python 2.
</p>
<p>
Python consistently ranks as one of the most popular programming
languages, and has gained widespread use in the machine learning
community.
</p>
<h2>History</h2>
<p>
Python was conceived in the late 1980s by Guido van Rossum at Centrum
Wiskunde & Informatica (CWI) in the Netherlands as a successor to
the ABC programming language, which was inspired by SETL, capable of
exception handling and interfacing with the Amoeba operating system. Its
implementation began in December 1989. Van Rossum shouldered sole
responsibility for the project, as the lead developer, until 12 July
2018, when he announced his "permanent vacation" from his
responsibilities as Python's "benevolent dictator for life", a title the
Python community bestowed upon him to reflect his long-term commitment
as the project's chief decision-maker. In January 2019, active Python
core developers elected a five-member Steering Council to lead the
project.
</p>
<p>
Python 2.0 was released on 16 October 2000, with many major new features
such as list comprehensions, cycle-detecting garbage collection,
reference counting, and Unicode support. Python 3.0, released on 3
December 2008, with many of its major features backported to Python
2.6.x and 2.7.x. Releases of Python 3 include the
<code>2to3</code> utility, which automates the translation of Python 2
code to Python 3.
</p>
<p>
Python 2.7's end-of-life was initially set for 2015, then postponed to
2020 out of concern that a large body of existing code could not easily
be forward-ported to Python 3. No further security patches or other
improvements will be released for it. Currently only 3.8 and later are
supported (2023 security issues were fixed in e.g. 3.7.17, the final
3.7.x release).
</p>
<p>
In 2021 (and again twice in 2022), security updates were expedited,
since all Python versions were insecure (including 2.7) because of
security issues leading to possible remote code execution and web-cache
poisoning. In 2022, Python 3.10.4 and 3.9.12 were expedited and 3.8.13,
because of many security issues. When Python 3.9.13 was released in May
2022, it was announced that the 3.9 series (joining the older series 3.8
and 3.7) would only receive security fixes in the future. On 7 September
2022, four new releases were made due to a potential denial-of-service
attack: 3.10.7, 3.9.14, 3.8.14, and 3.7.14.
</p>
<p>
As of October 2023, Python 3.12 is the stable release, and 3.12 and 3.11
are the only versions with active (as opposed to just security) support.
Notable changes in 3.11 from 3.10 include increased program execution
speed and improved error reporting.
</p>
<p>
Python 3.12 adds syntax (and in fact every Python since at least 3.5
adds some syntax) to the language, the new (soft) keyword
<code>type</code> (recent releases have added a lot of typing support
e.g. new type union operator in 3.10), and 3.11 for exception handling,
and 3.10 the <code>match</code> and <code>case</code> (soft) keywords,
for structural pattern matching statements. Python 3.12 also drops
outdated modules and functionality, and future versions will too, see
below in Development section.
</p>
<p>
Python 3.11 claims to be between 10 and 60% faster than Python 3.10, and
Python 3.12 adds another 5% on top of that. It also has improved error
messages, and many other changes.
</p>
<p>
Since 27 June 2023, Python 3.8 is the oldest supported version of Python
(albeit in the 'security support' phase), due to Python 3.7 reaching
end-of-life.
</p>
<h2>Design philosophy and features</h2>
<p>
Python is a multi-paradigm programming language. Object-oriented
programming and structured programming are fully supported, and many of
their features support functional programming and aspect-oriented
programming (including metaprogramming and metaobjects). Many other
paradigms are supported via extensions, including design by contract and
logic programming.
</p>
<p>
Python uses dynamic typing and a combination of reference counting and a
cycle-detecting garbage collector for memory management. It uses dynamic
name resolution (late binding), which binds method and variable names
during program execution.
</p>
<p>
Its design offers some support for functional programming in the Lisp
tradition. It has <code>filter</code>,<code>map</code>and<code
>reduce</code
>
functions; list comprehensions, dictionaries, sets, and generator
expressions. The standard library has two modules (<code
>itertools</code
>
and <code>functools</code>) that implement functional tools borrowed
from Haskell and Standard ML.
</p>
<p>
Its core philosophy is summarized in the document
<i>The Zen of Python</i> (<i>PEP 20</i>), which includes aphorisms such
as:
</p>
<ul>
<li>Beautiful is better than ugly.</li>
<li>Explicit is better than implicit.</li>
<li>Simple is better than complex.</li>
<li>Complex is better than complicated.</li>
<li>Readability counts.</li>
</ul>
<p>
Rather than building all of its functionality into its core, Python was
designed to be highly extensible via modules. This compact modularity
has made it particularly popular as a means of adding programmable
interfaces to existing applications. Van Rossum's vision of a small core
language with a large standard library and easily extensible interpreter
stemmed from his frustrations with ABC, which espoused the opposite
approach.
</p>
<p>
Python strives for a simpler, less-cluttered syntax and grammar while
giving developers a choice in their coding methodology. In contrast to
Perl's "there is more than one way to do it" motto, Python embraces a
"there should be one—and preferably only one—obvious way to do it"
philosophy. Alex Martelli, a Fellow at the Python Software Foundation
and Python book author, wrote: "To describe something as 'clever' is
<i>not</i> considered a compliment in the Python culture."
</p>
<p>
Python's developers strive to avoid premature optimization and reject
patches to non-critical parts of the CPython reference implementation
that would offer marginal increases in speed at the cost of clarity.
Execution speed can be improved by moving speed-critical functions to
extension modules written in languages such as C, or by using a
just-in-time compiler like PyPy. It is also possible to cross-compile to
other languages, but it either doesn't provide the full speed-up that
might be expected, since Python is a very dynamic language, or a
restricted subset of Python is compiled, and possibly semantics are
slightly changed. Python's developers aim for it to be fun to use. This
is reflected in its name—a tribute to the British comedy group Monty
Python—and in occasionally playful approaches to tutorials and reference
materials, such as the use of the terms "spam" and "eggs" (a reference
to a Monty Python sketch) in examples, instead of the often-used "foo"
and "bar".
</p>
<p>
A common neologism in the Python community is <i>pythonic</i>, which has
a wide range of meanings related to program style. "Pythonic" code may
use Python idioms well, be natural or show fluency in the language, or
conform with Python's minimalist philosophy and emphasis on readability.
Code that is difficult to understand or reads like a rough transcription
from another programming language is called <i>unpythonic</i>.
</p>
<h2>Syntax and semantics</h2>
<link
rel="mw-deduplicated-inline-style"
href="mw-data:TemplateStyles:r1033289096"
/>
<p>
Python is meant to be an easily readable language. Its formatting is
visually uncluttered and often uses English keywords where other
languages use punctuation. Unlike many other languages, it does not use
curly brackets to delimit blocks, and semicolons after statements are
allowed but rarely used. It has fewer syntactic exceptions and special
cases than C or Pascal.
</p>
<h3>Indentation</h3>
<link
rel="mw-deduplicated-inline-style"
href="mw-data:TemplateStyles:r1033289096"
/>
<p>
Python uses whitespace indentation, rather than curly brackets or
keywords, to delimit blocks. An increase in indentation comes after
certain statements; a decrease in indentation signifies the end of the
current block. Thus, the program's visual structure accurately
represents its semantic structure. This feature is sometimes termed the
off-side rule. Some other languages use indentation this way; but in
most, indentation has no semantic meaning. The recommended indent size
is four spaces.
</p>
<h3>Statements and control flow</h3>
<p>Python's statements include:</p>
<ul>
<li>
The assignment statement, using a single equals sign <code>=</code>
</li>
<li>
The <code>if</code> statement, which conditionally executes a block of
code, along with <code>else</code> and <code>elif</code> (a
contraction of else-if)
</li>
<li>
The <code>for</code> statement, which iterates over an iterable
object, capturing each element to a local variable for use by the
attached block
</li>
<li>
The <code>while</code> statement, which executes a block of code as
long as its condition is true
</li>
<li>
The <code>try</code> statement, which allows exceptions raised in its
attached code block to be caught and handled by
<code>except</code> clauses (or new syntax <code>except*</code> in
Python 3.11 for exception groups); it also ensures that clean-up code
in a <code>finally</code> block is always run regardless of how the
block exits
</li>
<li>
The <code>raise</code> statement, used to raise a specified exception
or re-raise a caught exception
</li>
<li>
The <code>class</code> statement, which executes a block of code and
attaches its local namespace to a class, for use in object-oriented
programming
</li>
<li>
The <code>def</code> statement, which defines a function or method
</li>
<li>
The <code>with</code> statement, which encloses a code block within a
context manager (for example, acquiring a lock before it is run, then
releasing the lock; or opening and closing a file), allowing
resource-acquisition-is-initialization (RAII)-like behavior and
replacing a common try/finally idiom
</li>
<li>The <code>break</code> statement, which exits a loop</li>
<li>
The <code>continue</code> statement, which skips the rest of the
current iteration and continues with the next
</li>
<li>
The <code>del</code> statement, which removes a variable—deleting the
reference from the name to the value, and producing an error if the
variable is referred to before it is redefined
</li>
<li>
The <code>pass</code> statement, serving as a NOP, syntactically
needed to create an empty code block
</li>
<li>
The <code>assert</code> statement, used in debugging to check for
conditions that should apply
</li>
<li>
The <code>yield</code> statement, which returns a value from a
generator function (and also an operator); used to implement
coroutines
</li>
<li>
The <code>return</code> statement, used to return a value from a
function
</li>
<li>
The <code>import</code> and <code>from</code> statements, used to
import modules whose functions or variables can be used in the current
program
</li>
</ul>
<p>
The assignment statement (<code>=</code>) binds a name as a reference to
a separate, dynamically allocated object. Variables may subsequently be
rebound at any time to any object. In Python, a variable name is a
generic reference holder without a fixed data type; however, it always
refers to
<i>some</i> object with a type. This is called dynamic typing—in
contrast to statically-typed languages, where each variable may contain
only a value of a certain type.
</p>
<p>
Python does not support tail call optimization or first-class
continuations, and, according to Van Rossum, it never will. However,
better support for coroutine-like functionality is provided by extending
Python's generators. Before 2.5, generators were lazy iterators; data
was passed unidirectionally out of the generator. From Python 2.5 on, it
is possible to pass data back into a generator function; and from
version 3.3, it can be passed through multiple stack levels.
</p>
<h3>Expressions</h3>
<p>Python's expressions include:</p>
<ul>
<li>
The <code>+</code>, <code>-</code>, and <code>*</code> operators for
mathematical addition, subtraction, and multiplication are similar to
other languages, but the behavior of division differs. There are two
types of divisions in Python: floor division (or integer division)
<code>//</code> and floating-point<code>/</code>division. Python uses
the <code>**</code> operator for exponentiation.
</li>
<li>
Python uses the <code>+</code> operator for string concatenation.
Python uses the <code>*</code> operator for duplicating a string a
specified number of times.
</li>
<li>
The <code>@</code> infix operator. It is intended to be used by
libraries such as NumPy for matrix multiplication.
</li>
<li>
The syntax <code>:=</code>, called the "walrus operator", was
introduced in Python 3.8. It assigns values to variables as part of a
larger expression.
</li>
<li>
In Python, <code>==</code> compares by value. Python's
<code>is</code> operator may be used to compare object identities
(comparison by reference), and comparisons may be chained—for example,
<code
class="mw-highlight mw-highlight-lang-python mw-content-ltr"
id=""
style=""
dir="ltr"
><span>a</span> <span><=</span> <span>b</span>
<span><=</span> <span>c</span></code
>.
</li>
<li>
Python uses <code>and</code>, <code>or</code>, and <code>not</code> as
Boolean operators.
</li>
<li>
Python has a type of expression called a <i>list comprehension</i>, as
well as a more general expression called a
<i>generator expression</i>.
</li>
<li>
Anonymous functions are implemented using lambda expressions; however,
there may be only one expression in each body.
</li>
<li>
Conditional expressions are written as
<code
class="mw-highlight mw-highlight-lang-python mw-content-ltr"
id=""
style=""
dir="ltr"
><span>x</span> <span>if</span> <span>c</span> <span>else</span>
<span>y</span></code
>
(different in order of operands from the
<code>c ? x : y</code> operator common to many other languages).
</li>
<li>
Python makes a distinction between lists and tuples. Lists are written
as
<code
class="mw-highlight mw-highlight-lang-python mw-content-ltr"
id=""
style=""
dir="ltr"
><span>[</span><span>1</span><span>,</span> <span>2</span
><span>,</span> <span>3</span><span>]</span></code
>, are mutable, and cannot be used as the keys of dictionaries
(dictionary keys must be immutable in Python). Tuples, written as
<code
class="mw-highlight mw-highlight-lang-python mw-content-ltr"
id=""
style=""
dir="ltr"
><span>(</span><span>1</span><span>,</span> <span>2</span
><span>,</span> <span>3</span><span>)</span></code
>, are immutable and thus can be used as keys of dictionaries,
provided all of the tuple's elements are immutable. The
<code>+</code> operator can be used to concatenate two tuples, which
does not directly modify their contents, but produces a new tuple
containing the elements of both. Thus, given the variable
<code>t</code> initially equal to
<code
class="mw-highlight mw-highlight-lang-python mw-content-ltr"
id=""
style=""
dir="ltr"
><span>(</span><span>1</span><span>,</span> <span>2</span
><span>,</span> <span>3</span><span>)</span></code
>, executing
<code
class="mw-highlight mw-highlight-lang-python mw-content-ltr"
id=""
style=""
dir="ltr"
><span>t</span> <span>=</span> <span>t</span> <span>+</span>
<span>(</span><span>4</span><span>,</span> <span>5</span
><span>)</span></code
>
first evaluates
<code
class="mw-highlight mw-highlight-lang-python mw-content-ltr"
id=""
style=""
dir="ltr"
><span>t</span> <span>+</span> <span>(</span><span>4</span
><span>,</span> <span>5</span><span>)</span></code
>, which yields
<code
class="mw-highlight mw-highlight-lang-python mw-content-ltr"
id=""
style=""
dir="ltr"
><span>(</span><span>1</span><span>,</span> <span>2</span
><span>,</span> <span>3</span><span>,</span> <span>4</span
><span>,</span> <span>5</span><span>)</span></code
>, which is then assigned back to <code>t</code>—thereby effectively
"modifying the contents" of <code>t</code> while conforming to the
immutable nature of tuple objects. Parentheses are optional for tuples
in unambiguous contexts.
</li>
<li>
Python features <i>sequence unpacking</i> where multiple expressions,
each evaluating to anything that can be assigned (to a variable,
writable property, etc.) are associated in an identical manner to that
forming tuple literals—and, as a whole, are put on the left-hand side
of the equal sign in an assignment statement. The statement expects an
<i>iterable</i> object on the right-hand side of the equal sign that
produces the same number of values as the provided writable
expressions; when iterated through them, it assigns each of the
produced values to the corresponding expression on the left.
</li>
<li>
Python has a "string format" operator <code>%</code> that functions
analogously to <code>printf</code> format strings in C—e.g.
<code
class="mw-highlight mw-highlight-lang-python mw-content-ltr"
id=""
style=""
dir="ltr"
><span>"spam=</span><span>%s</span><span> eggs=</span><span>%d</span
><span>"</span> <span>%</span> <span>(</span><span>"blah"</span
><span>,</span> <span>2</span><span>)</span></code
>
evaluates to <code>"spam=blah eggs=2"</code>. In Python 2.6+ and 3+,
this was supplemented by the <code>format()</code> method of the
<code>str</code> class, e.g.
<code
class="mw-highlight mw-highlight-lang-python mw-content-ltr"
id=""
style=""
dir="ltr"
><span>"spam=</span><span>{0}</span><span> eggs=</span
><span>{1}</span><span>"</span><span>.</span><span>format</span
><span>(</span><span>"blah"</span><span>,</span> <span>2</span
><span>)</span></code
>. Python 3.6 added "f-strings":
<code
class="mw-highlight mw-highlight-lang-python mw-content-ltr"
id=""
style=""
dir="ltr"
><span>spam</span> <span>=</span> <span>"blah"</span><span>;</span>
<span>eggs</span> <span>=</span> <span>2</span><span>;</span>
<span>f</span><span>'spam=</span><span>{</span><span>spam</span
><span>}</span><span> eggs=</span><span>{</span><span>eggs</span
><span>}</span><span>'</span></code
>.
</li>
<li>
Strings in Python can be concatenated by "adding" them (with the same
operator as for adding integers and floats), e.g.
<code
class="mw-highlight mw-highlight-lang-python mw-content-ltr"
id=""
style=""
dir="ltr"
><span>"spam"</span> <span>+</span> <span>"eggs"</span></code
>
returns <code>"spameggs"</code>. If strings contain numbers, they are
added as strings rather than integers, e.g.
<code
class="mw-highlight mw-highlight-lang-python mw-content-ltr"
id=""
style=""
dir="ltr"
><span>"2"</span> <span>+</span> <span>"2"</span></code
>
returns <code>"22"</code>.
</li>
<li>
Python has various string literals:
<ul>
<li>
Delimited by single or double quote marks; unlike in Unix shells,
Perl, and Perl-influenced languages, single and double quote marks
work the same. Both use the backslash (<code>\</code>) as an
escape character. String interpolation became available in Python
3.6 as "formatted string literals".
</li>
<li>
Triple-quoted (beginning and ending with three single or double
quote marks), which may span multiple lines and function like here
documents in shells, Perl, and Ruby.
</li>
<li>
Raw string varieties, denoted by prefixing the string literal with
<code>r</code>. Escape sequences are not interpreted; hence raw
strings are useful where literal backslashes are common, such as
regular expressions and Windows-style paths. (Compare
"<code>@</code>-quoting" in C#.)
</li>
</ul>
</li>
<li>
Python has array index and array slicing expressions in lists, denoted
as <code>a[key]</code>,
<code
class="mw-highlight mw-highlight-lang-python mw-content-ltr"
id=""
style=""
dir="ltr"
><span>a</span><span>[</span><span>start</span><span>:</span
><span>stop</span><span>]</span></code
>
or
<code
class="mw-highlight mw-highlight-lang-python mw-content-ltr"
id=""
style=""
dir="ltr"
><span>a</span><span>[</span><span>start</span><span>:</span
><span>stop</span><span>:</span><span>step</span
><span>]</span></code
>. Indexes are zero-based, and negative indexes are relative to the
end. Slices take elements from the <i>start</i> index up to, but not
including, the <i>stop</i> index. The third slice parameter called
<i>step</i> or <i>stride</i>, allows elements to be skipped and
reversed. Slice indexes may be omitted—for example,
<code
class="mw-highlight mw-highlight-lang-python mw-content-ltr"
id=""
style=""
dir="ltr"
><span>a</span><span>[:]</span></code
>
returns a copy of the entire list. Each element of a slice is a
shallow copy.
</li>
</ul>
<p>
In Python, a distinction between expressions and statements is rigidly
enforced, in contrast to languages such as Common Lisp, Scheme, or Ruby.
This leads to duplicating some functionality. For example:
</p>
<ul>
<li>List comprehensions vs. <code>for</code>-loops</li>
<li>Conditional expressions vs. <code>if</code> blocks</li>
<li>
The <code>eval()</code> vs. <code>exec()</code> built-in functions (in
Python 2, <code>exec</code> is a statement); the former is for
expressions, the latter is for statements
</li>
</ul>
<p>
Statements cannot be a part of an expression—so list and other
comprehensions or lambda expressions, all being expressions, cannot
contain statements. A particular case is that an assignment statement
such as
<code
class="mw-highlight mw-highlight-lang-python mw-content-ltr"
id=""
style=""
dir="ltr"
><span>a</span> <span>=</span> <span>1</span></code
>
cannot form part of the conditional expression of a conditional
statement. This has the advantage of avoiding a classic C error of
mistaking an assignment operator <code>=</code> for an equality operator
<code>==</code> in conditions:
<code
class="mw-highlight mw-highlight-lang-c mw-content-ltr"
id=""
style=""
dir="ltr"
><span>if</span><span> </span><span>(</span><span>c</span
><span> </span><span>=</span><span> </span><span>1</span><span>)</span
><span> </span><span>{</span><span> </span><span>...</span
><span> </span><span>}</span></code
>
is syntactically valid (but probably unintended) C code, but
<code
class="mw-highlight mw-highlight-lang-python mw-content-ltr"
id=""
style=""
dir="ltr"
><span>if</span> <span>c</span> <span>=</span> <span>1</span
><span>:</span> <span>...</span></code
>
causes a syntax error in Python.
</p>
<h3>Methods</h3>
<p>
Methods on objects are functions attached to the object's class; the
syntax
<code
class="mw-highlight mw-highlight-lang-python mw-content-ltr"
id=""
style=""
dir="ltr"
><span>instance</span><span>.</span><span>method</span><span>(</span
><span>argument</span><span>)</span></code
>
is, for normal methods and functions, syntactic sugar for
<code
class="mw-highlight mw-highlight-lang-python mw-content-ltr"
id=""
style=""
dir="ltr"
><span>Class</span><span>.</span><span>method</span><span>(</span
><span>instance</span><span>,</span> <span>argument</span
><span>)</span></code
>. Python methods have an explicit <code>self</code> parameter to access
instance data, in contrast to the implicit self (or <code>this</code>)
in some other object-oriented programming languages (e.g., C++, Java,
Objective-C, Ruby). Python also provides methods, often called
<i>dunder methods</i> (due to their names beginning and ending with
double-underscores), to allow user-defined classes to modify how they
are handled by native operations including length, comparison, in
arithmetic operations and type conversion.
</p>
<h3>Typing</h3>
<p>
Python uses duck typing and has typed objects but untyped variable
names. Type constraints are not checked at compile time; rather,
operations on an object may fail, signifying that it is not of a
suitable type. Despite being dynamically typed, Python is strongly
typed, forbidding operations that are not well-defined (for example,
adding a number to a string) rather than silently attempting to make
sense of them.
</p>
<p>
Python allows programmers to define their own types using classes, most
often used for object-oriented programming. New instances of classes are
constructed by calling the class (for example,
<code
class="mw-highlight mw-highlight-lang-python mw-content-ltr"
id=""
style=""
dir="ltr"
><span>SpamClass</span><span>()</span></code
>
or
<code
class="mw-highlight mw-highlight-lang-python mw-content-ltr"
id=""
style=""
dir="ltr"
><span>EggsClass</span><span>()</span></code
>), and the classes are instances of the metaclass
<code>type</code> (itself an instance of itself), allowing
metaprogramming and reflection.
</p>
<p>
Before version 3.0, Python had two kinds of classes (both using the same
syntax): <i>old-style</i> and <i>new-style</i>, current Python versions
only support the semantics new style.
</p>
<p>
Python supports optional type annotations. These annotations are not
enforced by the language, but may be used by external tools such as mypy
to catch errors. Mypy also supports a Python compiler called mypyc,
which leverages type annotations for optimization.
</p>
<h3>Arithmetic operations</h3>
<p>
Python has the usual symbols for arithmetic operators (<code>+</code>,
<code>-</code>, <code>*</code>, <code>/</code>), the floor division
operator <code>//</code> and the modulo operation <code>%</code> (where
the remainder can be negative, e.g. <code>4 % -3 == -2</code>). It also
has <code>**</code> for exponentiation, e.g.
<code>5**3 == 125</code> and <code>9**0.5 == 3.0</code>, and a
matrix‑multiplication operator <code>@</code> . These operators work
like in traditional math; with the same precedence rules, the operators
infix (<code>+</code> and <code>-</code> can also be unary to represent
positive and negative numbers respectively).
</p>
<p>
The division between integers produces floating-point results. The
behavior of division has changed significantly over time:
</p>
<ul>
<li>
Current Python (i.e. since 3.0) changed <code>/</code> to always be
floating-point division, e.g.
<code
class="nowrap mw-highlight mw-highlight-lang-python mw-content-ltr"
id=""
style=""
dir="ltr"
><span>5</span><span>/</span><span>2</span> <span>==</span>
<span>2.5</span></code
>.
</li>
<li>
The floor division <code>//</code> operator was introduced. So
<code>7//3 == 2</code>, <code>-7//3 == -3</code>,
<code>7.5//3 == 2.0</code> and <code>-7.5//3 == -3.0</code>. Adding
<code
class="nowrap mw-highlight mw-highlight-lang-python2 mw-content-ltr"
id=""
style=""
dir="ltr"
><span>from</span> <span>__future__</span> <span>import</span>
<span>division</span></code
>
causes a module used in Python 2.7 to use Python 3.0 rules for
division (see above).
</li>
</ul>
<p>
In Python terms, <code>/</code> is <i>true division</i> (or simply
<i>division</i>), and <code>//</code> is <i>floor division.</i>
<code>/</code> before version 3.0 is <i>classic division</i>.
</p>
<p>
Rounding towards negative infinity, though different from most
languages, adds consistency. For instance, it means that the equation
<code
class="nowrap mw-highlight mw-highlight-lang-python mw-content-ltr"
id=""
style=""
dir="ltr"
><span>(</span><span>a</span> <span>+</span> <span>b</span
><span>)</span><span>//</span><span>b</span> <span>==</span>
<span>a</span><span>//</span><span>b</span> <span>+</span>
<span>1</span></code
>
is always true. It also means that the equation
<code
class="nowrap mw-highlight mw-highlight-lang-python mw-content-ltr"
id=""
style=""
dir="ltr"
><span>b</span><span>*</span><span>(</span><span>a</span
><span>//</span><span>b</span><span>)</span> <span>+</span>
<span>a</span><span>%</span><span>b</span> <span>==</span>
<span>a</span></code
>
is valid for both positive and negative values of <code>a</code>.
However, maintaining the validity of this equation means that while the
result of <code>a%b</code> is, as expected, in the half-open interval
[0, <i>b</i>), where <code>b</code> is a positive integer, it has to lie
in the interval (<i>b</i>, 0] when <code>b</code> is negative.
</p>
<p>
Python provides a <code>round</code> function for rounding a float to
the nearest integer. For tie-breaking, Python 3 uses round to even:
<code>round(1.5)</code> and <code>round(2.5)</code> both produce
<code>2</code>. Versions before 3 used round-away-from-zero:
<code>round(0.5)</code> is <code>1.0</code>, <code>round(-0.5)</code> is
<code>−1.0</code>.
</p>
<p>
Python allows Boolean expressions with multiple equality relations in a
manner that is consistent with general use in mathematics. For example,
the expression <code>a < b < c</code> tests whether
<code>a</code> is less than <code>b</code> and <code>b</code> is less
than <code>c</code>. C-derived languages interpret this expression
differently: in C, the expression would first evaluate
<code>a < b</code>, resulting in 0 or 1, and that result would then
be compared with <code>c</code>.
</p>
<p>
Python uses arbitrary-precision arithmetic for all integer operations.
The
<code>Decimal</code> type/class in the <code>decimal</code> module
provides decimal floating-point numbers to a pre-defined arbitrary
precision and several rounding modes. The <code>Fraction</code> class in
the <code>fractions</code> module provides arbitrary precision for
rational numbers.
</p>
<p>
Due to Python's extensive mathematics library, and the third-party
library NumPy that further extends the native capabilities, it is
frequently used as a scientific scripting language to aid in problems
such as numerical data processing and manipulation.
</p>
<h2>Programming examples</h2>
<p>Hello world program:</p>
<p>Program to calculate the factorial of a positive integer:</p>
<h2>Libraries</h2>
<p>
Python's large standard library provides tools suited to many tasks and
is commonly cited as one of its greatest strengths. For Internet-facing
applications, many standard formats and protocols such as MIME and HTTP
are supported. It includes modules for creating graphical user
interfaces, connecting to relational databases, generating pseudorandom
numbers, arithmetic with arbitrary-precision decimals, manipulating
regular expressions, and unit testing.
</p>
<p>
Some parts of the standard library are covered by specifications—for
example, the Web Server Gateway Interface (WSGI) implementation
<code>wsgiref</code> follows PEP 333—but most are specified by their
code, internal documentation, and test suites. However, because most of
the standard library is cross-platform Python code, only a few modules
need altering or rewriting for variant implementations.
</p>
<p>
As of 14 November 2022, the Python Package Index (PyPI), the official
repository for third-party Python software, contains over 415,000
packages with a wide range of functionality, including:
</p>
<h2>Development environments</h2>
<link
rel="mw-deduplicated-inline-style"
href="mw-data:TemplateStyles:r1033289096"
/>
<p>
Most Python implementations (including CPython) include a
read–eval–print loop (REPL), permitting them to function as a command
line interpreter for which users enter statements sequentially and
receive results immediately.
</p>
<p>
Python also comes with an Integrated development environment (IDE)
called IDLE, which is more beginner-oriented.
</p>
<p>
Other shells, including IDLE and IPython, add further abilities such as
improved auto-completion, session state retention, and syntax
highlighting.
</p>
<p>
As well as standard desktop integrated development environments, there
are web browser-based IDEs, including SageMath, for developing science-
and math-related programs; PythonAnywhere, a browser-based IDE and
hosting environment; and Canopy IDE, a commercial IDE emphasizing
scientific computing.
</p>
<h2>Implementations</h2>
<link
rel="mw-deduplicated-inline-style"
href="mw-data:TemplateStyles:r1033289096"
/>
<h3>Reference implementation</h3>
<p>
CPython is the reference implementation of Python. It is written in C,
meeting the C89 standard (Python 3.11 uses C11) with several select C99
features. CPython includes its own C extensions, but third-party
extensions are not limited to older C versions—e.g. they can be
implemented with C11 or C++.) It compiles Python programs into an
intermediate bytecode which is then executed by its virtual machine.
CPython is distributed with a large standard library written in a
mixture of C and native Python, and is available for many platforms,
including Windows (starting with Python 3.9, the Python installer
deliberately fails to install on Windows 7 and 8; Windows XP was
supported until Python 3.5) and most modern Unix-like systems, including
macOS (and Apple M1 Macs, since Python 3.9.1, with experimental
installer) and unofficial support for e.g. VMS. Platform portability was
one of its earliest priorities. (During Python 1 and 2 development, even
OS/2 and Solaris were supported, but support has since been dropped for
many platforms.)
</p>
<h3>Other implementations</h3>
<ul>
<li>
PyPy is a fast, compliant interpreter of Python 2.7 and 3.8. Its
just-in-time compiler often brings a significant speed improvement
over CPython but some libraries written in C cannot be used with it.
</li>
<li>
Stackless Python is a significant fork of CPython that implements
microthreads; it does not use the call stack in the same way, thus
allowing massively concurrent programs. PyPy also has a stackless
version.
</li>
<li>
MicroPython and CircuitPython are Python 3 variants optimized for
microcontrollers, including Lego Mindstorms EV3.
</li>
<li>
Pyston is a variant of the Python runtime that uses just-in-time
compilation to speed up the execution of Python programs.
</li>
<li>
Cinder is a performance-oriented fork of CPython 3.8 that contains a
number of optimizations including bytecode inline caching, eager
evaluation of coroutines, a method-at-a-time JIT, and an experimental
bytecode compiler.
</li>
</ul>
<h3>Unsupported implementations</h3>
<p>
Other just-in-time Python compilers have been developed, but are now
unsupported:
</p>
<ul>
<li>
Google began a project named Unladen Swallow in 2009, with the aim of
speeding up the Python interpreter fivefold by using the LLVM, and of
improving its multithreading ability to scale to thousands of cores,
while ordinary implementations suffer from the global interpreter
lock.
</li>
<li>
Psyco is a discontinued just-in-time specializing compiler that
integrates with CPython and transforms bytecode to machine code at
runtime. The emitted code is specialized for certain data types and is
faster than the standard Python code. Psyco does not support Python
2.7 or later.
</li>
<li>
PyS60 was a Python 2 interpreter for Series 60 mobile phones released
by Nokia in 2005. It implemented many of the modules from the standard
library and some additional modules for integrating with the Symbian
operating system. The Nokia N900 also supports Python with GTK widget
libraries, enabling programs to be written and run on the target
device.
</li>
</ul>
<h3>Cross-compilers to other languages</h3>
<p>
There are several compilers/transpilers to high-level object languages,
with either unrestricted Python, a restricted subset of Python, or a
language similar to Python as the source language:
</p>
<ul>
<li>
Brython, Transcrypt and Pyjs (latest release in 2012) compile Python
to JavaScript.
</li>
<li>
Codon compiles a subset of statically typed Python to machine code
(via LLVM) and supports native multithreading.
</li>
<li>
Cython compiles (a superset of) Python to C. The resulting code is
also usable with Python via direct C-level API calls into the Python
interpreter.
</li>
<li>
PyJL compiles/transpiles a subset of Python to "human-readable,
maintainable, and high-performance Julia source code". Despite
claiming high performance, no tool can claim to do that for
<i>arbitrary</i> Python code; i.e. it's known not possible to compile
to a faster language or machine code. Unless semantics of Python are
changed, but in many cases speedup is possible with few or no changes
in the Python code. The faster Julia source code can then be used from
Python, or compiled to machine code, and based that way.
</li>
<li>Nuitka compiles Python into C.</li>
<li>Numba uses LLVM to compile a subset of Python to machine code.</li>
<li>Pythran compiles a subset of Python 3 to C++ (C++11).</li>
<li>
RPython can be compiled to C, and is used to build the PyPy
interpreter of Python.
</li>
<li>
The Python → 11l → C++ transpiler compiles a subset of Python 3 to C++
(C++17).
</li>
</ul>
<p>Specialized:</p>
<ul>