computer learning

Python (Programming Language)

Python (Programming Language)

Python (Programming Language) Disambig gray RTL.svg This article is about the Python programming language. For other uses, see Python.


Python has an active community, and it also has a lot of special purpose software libraries developed by people from that community. For example, there is the Baygame library that provides a set of functions for game programming. can also handle many types of databases like MySQL and so on.

supports several programming styles, namely object routing, side routing and functional programming.

has an open model for development, based on the Python programming community and supported by the Python Software Foundation. Which preserves the language definition in the reference implementation of C Python.

Etymology Python (Programming Language)

The name Python derives from British comedy group

Rossum being influenced by watching their shows while working on language development. Monty Python occasionally appears in the Python code and culture. For example, spam and white are the more common meta-structural variants in the Python literature rather than the traditional fo and bar variant. Also notes that the official Python documentation refers to the short scenes provided by Monty Python’s band.

anything related to this language. For example, Python implementations and libraries such as Baygame link SDL to Python, and BayQuete and BayGTK link both Cute and GTK to Python.

Brief History Python (Programming Language)

. They wanted it to be able to handle exceptional cases and interact with the amoeba operating system. In contrast, implementation of the idea did not begin until December 1989.

Van Rossum took sole responsibility for managing the project, as lead developer, until July 12, 2018, when he declared his “permanent leave” of his responsibilities as Python’s benevolent dictator for life, a title bestowed upon him by the Python community. To reflect his long-term commitment as the main decision maker for the project. He now co-leads as a member of the five-person Steering Board.

on October 16, 2000 with several major new features, including a loop select, garbage collector, and Unicode support.

The third version of Python

(Python 3.0) was

released on December 3, 2008. It was a major revision of the language but it is not fully backward compatible. Its main features are: reverse loading on Python 2.6.x and 2.7.x release threads. Python 3 versions include the 2to3 function, which automatically (at least partially) translates Python 2 code to Python 3.

pushed back to 2020 for fear that a large set of instructions

could not be ported.

In January 2019, the active Python kernel elected developers Brett Cannon, Nick Coughlan, Barry Warsaw, Carol Feiling and Van Rossum to form the five members of a “steering board” to lead the project.

Advantages of Python Python

is a relatively easy programming language compared to Java and C++, in addition to being a multi-paradigm programming language (multi-paradigm programming). It fully supports object-oriented programming and structured programming, and Python supports functional and side-oriented programming (including via meta-programming and meta-objects “especially methods”). It can also support many other thought patterns via extensions, including contract design and logic programming.

Support for functional programming

In addition, the design of Python provides some support for functional programming in the Lisp tradition. Python has functions (functions) filter, reduce, map. It also includes list, dictionaries, combinations, and generator expressions. The standard library contains two modules (itertools and functools) that implement functional tools borrowed from Haskell and the standard ML.

Easy to Learn

language to get started with. Python has unusually easy syntax, as previously mentioned. it is strives to build simpler and less cluttered grammars while giving developers a choice in their programming methodology. In contrast to Perl’s motto “there is more than one way to do it”, Python takes the motto “and there must be – and preferably – only one clear way to do it” as a design philosophy. “Describing something as ‘smart’ is not considered a

compliment in Python culture,” wrote Alex Martelli, a fellow at the Python Software Foundation and author of the book “Python”.

Free and

Open Source Python is an example of free open source software. In simple terms, you can freely distribute copies of this software, read the source code, make some changes to it and use parts of it in new free software. Free software is based on the principle of a knowledge-sharing community.

A high-level programming language

When you write programs in Python, you don’t have to worry about minute-level details like the memory management your program uses. Python uses a dynamic (software) type system and a combination of a loop selector, a garbage collector, and a (cyclic) selector to manage memory. It also features dynamic name resolution (delayed binding), which associates method and variable names during program execution.


Due to its open source software nature, it works across many platforms. Everything written in Python can run on any of these platforms without requiring any changes at all if it is precise enough to avoid any system-specific features.

Macintosh, Solaris, OS/2, Android. And many other operating systems.


Python supports procedural programming as well as object-oriented programming. Python is a very powerful language and also simplistic to do object-oriented programming, especially when compared to languages ​​such as C++ or Java.


Python is designed to be highly extensible. This built-in module has made it particularly popular as a way to add programmable interfaces to existing applications. Van Rossum’s vision of a small base language with a large, compiled (compiled) standard library easily expandable took advantage of the errors of IBC, which took the opposite approach.

philosophy of Python

in the Zen Python document (Pip 20), includes a statement such :

beautiful is better than ugly.

Explicit is better than implicit.

Simple is better than complex.

Compound is better than complex.

Readability is important.

Python developers strive to avoid premature optimization, and reject patches for non-critical parts of the reference C Python implementation that would provide marginal speed increases at the expense of clarity. When speed is important, a Python programmer can move time-sensitive functions to extension models written in languages ​​such as C, or by using pipi, a real-time (compiler) compiler. Also available is Python, which translates Python script to C script and makes direct C-level API calls to the Python interpreter.

One of the important goals of Python developers is to keep it fun to use.

Syntax (Language Syntax) and Semantics

Python was designed as a highly readable, visually organized language; They usually use commonly used English words while other programming languages ​​use punctuation. In contrast to many other languages, Python does not use curly braces { } to delimit code blocks, and the semicolon ; After the code statements are optional. Python has much fewer syntactic exceptions and special cases than C or Pascal.

Indent Python (Programming Language)

Python uses indents with spaces instead of curly braces { } or keywords to delimit code blocks. The increase in offset comes after some statements; Decreased offset marks the end of the current block. Thus, the visual structure of the program accurately represents the semantic structure of the program.

Statements and Flow Control Python (Programming Language)

Python statements include (among other things):

an equals statement (it uses the equal sign =) that works differently than other imperative programming languages ​​and this basic mechanism involving the “nature of

Python’s versions of variables” enlightens the way for many other language features . Take an example of equality in C: x = 2, which translates to “The name of the variable written x receives a copy of the scalar value 2”. Copy the (right) value into a custom storage location so that the (left) variable name is the symbolic address. The memory allocated to the variable is large enough (potentially too large) for the defined type. In the simplest case of a Python assignment, if we do the same operation in Python

(i.e. x = 2), it translates to “(generic) x receives a reference to a separate, dynamically assigned object whose scalar type (int integer) is 2”. This is called object binding. Since the name’s storage location does not contain the indicated value, it is not appropriate to call

Python (Programming Language) common value for multiple names, for example, x = 2; y = 2; z = 2 results in allocating storage space (at most) to three names and one scalar object, to which all three names are associated. Since the name is a generic reference holder, it is not reasonable to associate a static datatype with it. Thus there is dynamic writing.

Python (Programming Language) The statement for for , which iterates over an iterable object in a loop, captures each element in a local variable for use by the attached code set.

The Python (Programming Language) try statement, which allows handling and handling of exceptions raised in the attached code block except for clauses; It also ensures that the cleanup code in the last block of code always runs no matter how the block exits.

Python (Programming Language) The class statement, which implements a set of code and associates its local namespace with a class, for use in object-oriented programming.

The statement defines def, which defines a function or method.

The with with statement, from Python 2.5 released in September 2006 , which attaches a block of code inside a context manager (eg, getting a lock before running a block of code and releasing the lock afterward, or opening a file and then closing it), and allowing a resource acquisition that is Initialization-like behavior (RAII) and replaces the idiomatic try/finally statement.

The phrase break means to get out of the loop.

The continue statement overrides the current iteration and continues to the next element.

The pass statement, which acts as a “no process”, works. Used when there is a syntactically need to create an empty block of code.

Python (Programming Language) The yield statement returns a value from the generated function. Starting with Python 2.5, yield is also practical.

There are three ways to use the import statement:

import <module name> [as <alias>]

from <module name> import *

from <module name> import <definition 1> [as <alias 1>], <definition 2> [as <alias 2>],….

Python does not support tail-calling or (first-degree continuity) private optimization, as Guido van Rossum declared that it would never. However, helper-like functionality is better supported in version 2.5, through the extension of the Python generators. Prior to version 2.5, generators were lazy repeaters, in which information was passed from one direction outside the generator. Starting with Python 2.5, it is now possible to pass information back to the generator function, and starting with Python 3.3, it is also possible to pass information through multiple levels of the stack.

Expressions Python (Programming Language)

of some Python expressions are similar to languages ​​such as C and Java, while others are not:

subtraction, and multiplication in Python are similar to those in Java and C, but the division behavior is different. There are two types of division in Python: whole division (or integer division) // and comma division a (for a float)/. Python also added the ** operator for the exponent.

Starting in Python 3.5, I introduced a new operator, @, for the purpose of using it by libraries like numpy to multiply arrays.

It assigns values ​​to variables as part of a larger expression.

In Python == compares numbers by value (-0.0==0.0 is true), in contrast in Java it compares numerically by value or objects by reference address. (It is possible in Java to compare value to objects via the equals() method. In Python, comparisons can be restricted, for example a <= b <= c.

Python uses the words and, or, not for Boolean operations instead of the symbols &&, ||, ! Used in java and c language.

Python has a type of expression under the term inclusion list. Python 2.4 extended the list of realizations with a more comprehensive expression ie the expression generator.

x : y from other languages).

Python distinguishes between lists and tuples. Lists are written as [1, 2, 3], are mutable, and cannot be used as dictionaries keys (dictionary keys are immutable in Python). As for the enumerations: (1, 2, 3), they are immutable and therefore can be used as dictionaries keys, provided that all the elements of the enumeration are immutable. The + operator can be used to concatenate two polynomials, which do not directly modify their contents,he statement expects that an iterable object on the right side of the equal sign will produce the same number of values ​​as the writable expressions when iterated through it, and will iterate through it, with each of the corresponding expression’s producing values ​​set to the left.

Python has the % operator for “formatting a string”. This function is similar to the format for the printf string in C, for example; . Python 3.6 has added the following “f-strings”: blah = “blah”; eggs = 2; f’spam={blah} eggs={eggs}’.

Python has a variety of string literals:

strings delimited by single or double quotes. In contrast to Unix shell, Perl and B-influenced languages, single quotes and double quotes work the same. Both types of slashes (\) use an escape symbol. String completion is now available in Python 3.6 as a “literal formatted string”.

Text strings with triple quotation marks, which begin and end with a string of three single or double quotation marks. It may span multiple lines and act like the hare documentation found in shell, perl, and ruby.

The raw string configuration, denoted by the string literal prefix with the letter r. It does not explain the escape sequence. Hence, raw strings are useful when literal backslashes are common, such as regular expressions and Windows-style paths. Compared to «quote-@» (@-quoting) in C-Sharp.

Python quote has array slicing index expressions in lists, denoted as [key] a[key], [start:stop]a[start:stop] or [start:stop]a[ start:stop:step]. Indexes are zero-based, and negative indexes are bound to the end. Slices take items from the start index through the stop index, but do not include it. A third slide parameter, called a step or stage, allows items to be skipped and reversed. You might omit slice indexes, for example, it returns a[:]copy of the entire list. Each element in the slide is a shallow copy.

This leads to duplication of some functions. For example:

Include a list versus a loop for for-loops.

Conditional statements versus if blocks.

The eval() vs. exec() attached functions (in B Python 2, exec is a program statement) the first for expressions, the second for code statements.

Statements cannot be part of an expression, so list, other inclusions, or lambda expressions (all considered expressions) cannot contain statements.

Back to top button