Lesson 01: Introduction to Python

This lesson begins with a history of the creation of Python and continues with a brief description and description of its features; In the description of the features, an attempt has been made to provide some non-Python explanations for a better understanding of the topics.

There is also talk of Python performance and why there are currently two different branches of the language. The purpose of this lesson is to create an unambiguous background of Python! Hoping it will be useful and can encourage you to learn this language 😉 .

✔ Level: Basic

History

Story [1] Python starts from late 1982, when Mr. Rossum after the university as a programmer ABC Language Development team at CWI in Amsterdam is. ABC is an interpreted programming language for educational purposes that began in the late 1970s under a project at the center. Four or five years later, when the ABC language project did not succeed, he left and joined the Amoeba OS development team . This is important because it was while working on the operating system that Mr. Rossum came up with the idea of ​​creating a new scripting language.

In December 1989, the Christmas holiday is an opportunity to Mr. Rossum’s idea of fun its own. After that, of course, he spends a lot of free time developing a project he called “Python.” In this way, his experience working on the ABC language as well as his studies on the Modula-3 language have played a major role, in fact he created a personal version of his favorite parts of the ABC. He created a simple virtual machine, a simple parser, and a simple runtime system; A language with syntax (syntax Syntax) of the recess (Indentation) for Blakbndy uses and the types of data dictionary, lists, strings, and numbers were supported and against ABC extensible.

A year later, Mr. Rosum uses Python in the Amoeba project; The desired result, as well as the feedback among his colleagues, led him to make many initial optimizations in Python. This trend continued until finally, in the second month of 1991, it released the language with version 0.9.0 and under a similar license to MIT through one of the “news groups” (Usenet) called alt.sources , which It is also highly welcomed.

The origin of Mr. Rossom’s choice of the name Python, contrary to popular belief or what can be seen from its logo, is not related to a specific type of snake !; In fact, the name comes from a British comedy series called ” Monty Python’s Flying Circus “, which aired mainly on BBC One from 1969 to 1974.

Short descriptive

Simplicity and readability are the hallmarks of the Python programming language, so simple that even children can learn it, and power alongside that simplicity and readability is the miracle of Python. From the point of view of any programmer, Python programs are a collection of beautiful code, without any confusion or complexity. Instead of engaging the programmer in understanding its structure, it becomes a powerful tool for accomplishing everything a programmer thinks, if you did not make any mistakes in executing your Python code first; Do not be surprised! And if you did, be happy! This language is very lovable 😍 . ilovepython #

Guido van Rossum

The Python programming language, as mentioned, was created in 1991 by Mr. Rosum and is under his direct supervision. Mr. Rossum was born in 1956 and is the first holder of the BDFL (Eternal Benevolent Dictator) title , and currently (2015) works for Dropbox. (personal page)

The development of Python has always been associated with the development and popularity of other open source languages ​​such as Tcl, Perl, and Ruby, which has greatly contributed to its development.

Python has a very large and dynamic user community, which is a very important factor in the development of any programming language. This community exchanges what is learned every year by holding various conferences around the world. One of the most common of these conferences is PyCon .

This language has been used by many companies or organizations and has been used in many applications, including: reddit, Pinterest, Instagram (social networks) / Google (search engine and YouTube service). ) / Yahoo (Map Service) / IBM, Intel, Cisco, Seagate (Hardware Testing) / Dropbox (Cloud Hosting Service) / EVE Online (Online Games) / NASA, Los Alamos (Science Affairs) / iRobot (Robotics) / Pixar , Industrial Light and Magic, Walt Disney (animation making) / BitTorrent (peer-to-peer file sharing) / CIA (website) / PayPal (online money transfer system) / Mercurial (version control system) / ESRI (GIS mapping) And many others [4] pointed out. (Python in stackshare)

An important point about the Python programming language is its types of implementation. So far, various implementations have been developed for this language with different purposes, including Jython (specific to Java Virtual Machine), IronPython (specific to Microsoft .NET framework), PyPy (for faster execution), Stackless (for execution). Better multi-threaded programs) and of course CPython mentioned.

CPython is the standard and core implementation of the Python language, which is developed using the C programming language. With CPython, you can use the C and C ++ language libraries in addition to the Python library.

Note that Python is the name of a programming language, and CPython (or other Python implementations) is the program by which the source code of a program can be executed in Python. In other words, CPython is a processor for the Python language.

Features

Python is a free, open source, and more complete free-to- use programming language currently licensed (since version 2.1 and later) by the PSF Python Software Foundation . The Python license is compatible with the GPL3 license , however it allows the programmer to generate a Closed Source program.

The Python language is also part of the LAMP software package. This package includes: Linux ~ Operating System / Apache ~ Web Server / MySQL or MariaDB ~ Database / Python or Perl or PHP. Programming Language.

The language was open source long before even Eric Raymond and Bruce Perens spoke Open Source.

Python is a general-purpose programming language, meaning that it can be used to develop a wide variety of applications in a variety of software fields; Including in the development of web applications and applications with “graphical user interface” ( GUI ) capabilities.

Python is a programming language with a large set of pre-built and portable capabilities known as its standard library. In addition to this library, you can find a very wide range of Python Third-Party Libraries Also benefited. Third-party libraries provide useful tools in many areas such as: Scientific programming ~ NumPy, SciPy / natural language processing ~ NLTK / Serial port communication ~ PySerial / game creation ~ PyGame, Pyglet, PyOpenGL, PySoy / artificial intelligence ~ PyBrain / website creation ~ Django, Flask, Bottle, CherryPy / image processing ~ PIL / Pillow, PythonMagick / graphical user interface ~ PyGtk, PyQt, WxPython / working with rdflib ~ pdf / encryption documents ~ PyCrypto, pyOpenSSL and many more … They are being developed and provided to Python programmers as their number and variety increase.

Python is a Dynamic programming language and has automatic memory management capabilities. This language has similarities to TCL, Perl, Ruby, PHP or other dynamic programming languages. In this type of languages, unlike static languages ​​such as C ++, C and Java, there is no need to explicitly define the type of variables (Variables) and also the type of variable can be changed during the program. In dynamic languages, variables do not have a type of their own, and the decision to assign a type is made at “RunTime”.

Unlike C language, Python allocates and releases memory automatically. Python uses two methods to free up memory: “Reference Counting” and ” Garbage Collection ” . (Of course, don’t forget to talk about CPython implementation)

Although Python is a dynamic language, it is also a “Strongly Typed” language. In these types of languages ​​(such as Java) there is no sudden and automatic change of data type (or object); For example, in Python, if an addition is made between a numeric data type 3and a non-numeric data type “7”, an exception TypeErroris reported. In contrast, there are “Weak Typed” languages; In these languages ​​(such as Perl) the type is changed automatically depending on the situation. In this case, programming languages ​​have different behaviors, ‌ For example, the result of the phrase in Perl language is converted (Convert) Convert Automatic non-numeric data type to number ( ), the value is calculated.”7″ + 3″7″710

tip

An exception is actually a situation or error (Error) that is predicted in the program and its occurrence is reported as a protest. On the other hand, the program for each protest also defines how to deal wit

Python is a high-level programming language such as C, Ruby, PHP, and Java that allows it to be programmed easily and quickly, unlike low-level programming languages ​​such as Assembly. Does not involve specific issues at this level such as working with registers, memory addresses, etc.

tip

According to a common classification [9] , programming languages ​​can be categorized into two levels in relation to the involvement of the programmer with the concepts of hardware or the degree of abstraction of the programmer from the hardware: 1. Bottom ~ Car Language and Assembly 2. Top ~ Java, C ++, C etc …

Attention

The abstraction rate of all high-level programming languages ​​is not the same; For example, the Python abstraction rate is higher than Java and the Java abstraction level is much higher than C:

Machine <Assembly <C <C ++ <Java <Python

 

tip

The only language that can be understood and executed for each machine (or computer) is its specific machine language, and all programs written in any level of abstraction must be translated into this language before execution. Obviously, the need for this additional processing reduces the speed of the program. Translates to assembly code by the assembler ( Assembler ) and the language compiler ( Compiler ) or interpreter ( Interpreter ) is done.

Python is a multi-paradigm programming language and does not force the programmer to follow a specific pattern. This language supports patterns: Imperative or Procedural , Functional and Object-Oriented ; Of course, as mentioned, Python can not be considered purely belonging to any of these patterns.

Aside from supporting object-oriented programming templates, the Python structure is completely object-oriented, and everything in this language is an object; We will talk more about this in the future.

Python (implementation of CPython) is known as an interpreted language. With a general definition in these types of languages, the job of reading the program source code, translating it into machine language, and finally executing it is done by an interpreter. In Python, translating and executing source code can be summarized in two consecutive steps:

1- Compile source code to bytecode

2- Interpreting bytecode in machine language and executing it

In this way, the source code of the program is first translated into an intermediate language close to the machine language called bytecode and then the resulting bytecode is sent to the virtual machine – as the Python execution engine – and is interpreted and executed by it.

Attention

Although Python (CPython implementation) is known as an interpreted language, it uses a compiler at runtime to translate source code into bytecode.

Difference between compiler and interpreter

In general, short and simple:

A compiler (in compiled languages) reads all the lines of the program’s source code together and then creates a new, source-independent file called the “Object Code” or “Executable” translation file. Ends the language of the machine. The program from the compiler has the ability to run several times on the destination computer, without the need to re-compile, but it is not portable and the end-user will not be able to access the source code of the program and change or edit it. But an interpreter (in interpreted languages) reads the source code of the program line by line and translates each line separately into machine language and then executes it immediately. In this way, when one line is running, the next line is being translated into machine language at the same time. Programs written in interpreted programming languages ​​need to be re-interpreted each time they run on the target computer.This difference in performance, although it slows down the execution of interpreted programs compared to compiled programs, but reading the code line by line in the interpreter, in addition to better error detection, allows any user to edit and change the source code even when running the program. Give; These languages ​​have received a lot of attention when there is a constant need to add different features to the program.

Attention

The Python virtual machine (CPython VM) is an iterative loop that reads bytecode commands one by one, interprets them in machine language, and executes them immediately.

Python is a portable programming language. It can be said that the source code of a program in this language can be run on any computer. Python bytecode is similar to machine language, except that it is not specific to any particular machine (or computer). In contrast, when the source code of a program is translated into machine language by the compiler, the result is a file that is only applicable to machines for which this code has been translated (according to the processor architecture ), but Python bytecode with the help of its virtual machine. It has the ability to run on any machine.

tip

Each machine has its own machine language, and therefore normally running a program on machines with different architectures sometimes requires changing the source code of the program and re-translating it by a compiler tailored to the architecture of that particular machine.

A virtual machine is a package of software that can be installed on a variety of machines and its task is to create a separate machine (on the destination machine) to execute codes (bytecodes) independently. This type of virtual machine “virtual machine processing” said.

Accordingly, the source code of a Python program can now be run on all major operating systems without any changes.

Python is an extensible programming language. Code written with Python can be added to other source code of this language, and libraries and code written with other programming languages ​​can also be used among Python code (such as C and C ++ in CPython implementation or Java in Jython implementation).

Python has a clean grammar and readability above the code. Readability is emphasized in Python design and along with object-orientation is one of the most important features of a code for reuse. Python as the language of ABC method “indent” by the empty spaces to Blakbndy uses. In Python, it is not necessary to use semicolons ;and some parentheses, and of course the presence of data types – more accurately “object types” – is varied, and Python dynamics reduce coding volume and save development time. Compared to languages ​​like C ++, C and even Java. Research shows that what a Python programmer can do in two months, two C ++ programmers will not be able to complete in a year!

Python is a case sensitive language. In these types of languages ​​(such as C, C ++, Java, Perl, etc.) there is a difference between lowercase and uppercase letters such as a and A. With this in mind, the Python interpreter distinguishes between the words True, true, and TRUE.

Efficiency

People who learn Python will often have concerns about the performance of programs developed with this language, especially when compared to Java. However, Python is an interpreted language and runs slower than compiled languages ​​such as Will have C and Java. High performance is usually equal to high speed, but judging performance is not so easy!

In each project, depending on what we want, we have to choose. If the basis of performance for a project is high-speed execution (such as system applications), ‌ C language is always the best choice, but if the basis is high-speed development and resource savings, including cost, Python is the right choice. Sometimes the best performance is achieved by using a combination of different languages.

In many applications, Python speed is perfectly acceptable, the presence of bytecode increases the speed of subsequent executions of the program, and there are many programming tips to improve the execution speed in Python, which will be gradually mentioned in this book. It should not be forgotten that one of the most important performance factors is having an optimal algorithm, and of course writing modules in C also significantly increases the performance of Python – Python is slow if used incorrectly – however in the normal case if there are a thousand Python is the best choice for them, speed will not be one of them, and you should use other languages ​​to develop programs where speed is very important.

It should be noted that all of this talk is about implementing CPython in Python, and other implementations have been developed in Python for high-speed execution (such as PyPy ).

Versions

Two branches of Python are now available in parallel: 2x and 3x versions.

python-2-and-python-3

Historically, Sustainable Version 2.0 will be released on October 16, 2000, followed by version 1.6; Python continued to evolve until December 2008, when version 3.0 of Python, also known as Python 3000 or Py3K, took the approach of breaking “compatibility with versions.” Previous ( Backward Compatibility ) is published. In other words, the newer (3x) version of Python will not be able to execute the source code based on the previous version’s interpreter. (That this has been and is very controversial in the Python community!)

Apparently, Mr. Rosum had long ago felt the need to make a series of changes in the structure and syntax of this language. Perhaps the first sign of the need for change in Python can be seen in his talk at the 2002 Oriole Open Source Conference (OSCON) entitled ” Python Regrets ” In any case, the Python development team seeks to address this need by preserving Python’s past and adhering to its philosophy of simplicity; Selects the latter, discarding old features and replacing them with new ones.

With the release of a new version, the development of the old version should normally be stopped, but since the infrastructure of large companies depends on Python (such as Google) and upgrading the version for them will take at least a lot of time, many small and large programs and libraries by Python community users have been developed for a variety of small and large needs, all of which seem unlikely to be compatible with the new version of Python, and most importantly Python programmers themselves who, after years, now have to synthesize yesterday’s work and sometimes even To do with different libraries and modules; The Python development team, in order to fill the gap between yesterday and today Python, or to pave the way for the migration to the new version of Python, ‌ in addition to trying in advance to bring the new features and syntax of version 3.0 to version 2.6 (which was released two months earlier) Port does not end the development of the old version and releases another version, 2.7, on July 3, 2010, almost a year after the release of version 3.1, along with many of its new features. Makes.

According to the document PEP 404 , never officially released a version number 2.8 will not be a long-term support version 2.7, an older version of Python will be the end point. Initially, this version was supposed to be supported for five years (bug fixes), but a few months ago, this time was increased to ten years, until 2020.

Stable version of Python with ABC format; As in 3.4.2, it is numbered and published. The number A is the major part of the version number and increases when there are really big changes in the Python language. The number B indicates the minor part of the version number and will increase with a series of important changes to the Python language. The Python version number is usually shown as AB only, because the C number only increases (from zero) with a possible bug, which does not include all Python versions.

Which version? The 3x version is suitable for starting learning and educational purposes, and if necessary, you can get acquainted with the old version only by studying the comparison between these two versions. Also, if you are planning to develop desktop applications (ie when you are not limited to the version installed on the server) or when you do not need libraries that are not yet (early 2015) compatible with the 3x version (such as Twisted) in creating your application; ‌ Using the 3x version is very good. However, efforts are being made to cover both versions of this book, far from confusion.

Leave a Reply

Your email address will not be published. Required fields are marked *