ComputersProgramming

Python - what is it? High-level programming language

Python is a universal high-level language that can be extended and built-in. It, for example, is included in the suite of applications as a tool for writing macros. This makes Python a reasonable choice for many programming tasks, large and not very, and not so successful for a small number of computing tasks.

Where is it better to use?

The Python language is ideal for projects requiring rapid development. It supports several programming paradigms, which is good for programs that require flexibility. And the presence of multiple packages and modules ensures versatility and saves time.

Guido van Rossum - the creator of Python, affectionately awarded the community title of "magnanimous lifelong dictator." In the late 1980s, Guido liked the characteristics of some programming languages, but none of them had all the features that he would have liked to have. In particular, the language should have the following characteristics.

Scripting Language

A script is a program that manages other programs. The scripting languages are suitable for rapid development and prototyping, because they are good at transferring data from one component to another and relieve the programmer of such troublesome things as memory management.

The user community prefers to call Python a dynamic programming language.

Indent for grouping operators

Python determines whether expressions belong to the same group by indenting them. Such a group is called a code block. Other languages use a different syntax or punctuation for this. For example, in C symbol the symbol {denotes the beginning and} the end of the sequence of commands. The presence of indentation is considered good practice in other languages, but one of the first, in which padding is enforced compulsorily, was Python. What does this give? Indenting makes the code more readable, and code blocks require fewer notations for their start and end and punctuation marks, which can be bypassed casually. All this leads to fewer mistakes.

High-level data types

Computers store data in units and zeros, but people need more complex forms, such as text. A language that supports complex data is said to support high-level data types. These types of data are easy to operate. For example, in Python, strings can be separated, merged, translated into upper or lower case, they can be searched, etc. High-level data types, such as lists and dictionaries that can store other data, have much greater functionality, Than other languages.

Expandability

An extensible programming language can be extended. Such languages are very powerful, because additions make them suitable for a variety of applications and operating systems. Extensions can add data types or concepts, modules and plug-ins. The Python language is expanded in several ways. The core group of programmers is working to change it and improve it, and hundreds of others are writing modules for specific purposes.

Interpretation

Interpreted languages are executed directly from the source code written by people, and programs written in compiled languages, such as C ++, must be translated into machine code. Interpreted languages are slower, because the translation is on the fly, but writing programs and debugging is faster, because there is no need to wait for the compiler to finish. They are easier to transfer to different platforms.

You can argue about whether Python is an interpretable or compiled language. Although in many ways it works as interpreted, before executing it, the code is compiled (as in Java), and many of its components work at full machine speed, since they are written in C.

Guido began writing Python during the Christmas holidays in 1989, and over the next year he finalized the language on the basis of feedback from his colleagues. The general public saw the result in February 1991, when it was posted in one of Usenet news groups.

Python for Beginners

In order to start writing programs in Python, you need to install it. The versions of Python 2.7 and Python 3.5 have significant differences, because of which the programs written on them are incompatible.

In computers "Macintosh" this language is preinstalled, and its version depends on the OS age. When working in Windows, you will have to install Python on your own. You can select the files of the installation package on the python.org website.

Two ways of interaction

One of the reasons for the simplicity that is different in Python programming is that it comes with tools that help you develop, write, and debug programs.

In interactive mode, commands are entered one line at a time, almost the same way as the operating system (shell) accepts commands from the command line. You can also create short multi-line programs or import code from text files or built-in Python modules. For beginners, it will be useful to know that interactive mode includes an extensive help system. This is a convenient way to learn the capabilities of a programming language.

The IDLE development environment includes interactive mode and tools for writing and running programs, as well as a name tracking system. The environment is written in Python and demonstrates the extensive capabilities of the language.

Interactive mode

Here you can do almost anything you can do in the program, even write multi-line code. This mode can serve:

  • Sandbox for safe experiments;
  • The environment that allows you to learn programming in Python;
  • Tool for searching and fixing errors.

It should be noted that it is impossible to save the input in interactive mode. To do this, you must write a copy of the code and the results in the file.

Interactive mode can be used as a calculator, manipulate text and assign values to variables. You can also import modules, functions, or parts of programs to test them. This helps to experiment with Python objects without writing long programs and debug programs by importing their parts one at a time.

Work in an interactive mode

After Python starts, the terminal window displays information about the current version of the program, the date of its release, several prompts for further actions and the invitation to enter >>>.

To work in interactive mode, enter a command or expression and press the enter key.

Python interprets the input and responds if the typed requires an answer, or the interpreter does not understand it.

The following command will print a string. Since the print location is not specified, the output is displayed.

  • >>> print "Hello world!"
  • Hello World!

This single line is the whole program! In interactive mode, Python processes each line of input code after pressing the enter key, and the result appears below.

View object information

In interactive mode, there are two ways to view information about an object:

  • Enter the object (or its name) and press the enter key;
  • Enter the print command and the object (or its name) and press Enter.

The result depends on the object.

When using some data types (integers and lists, for example), these two methods yield the same result:

  • >>> x = [3,2]
  • >>> x
  • [3, 2]
  • >>> print x
  • [3, 2]

For strings, the result of typing the command "print name" is slightly different from the result obtained for typing a name. In the first case, the value is enclosed in quotes, and in the second case, not:

  • >>> x = "MyString"
  • >>> x
  • "MyString"
  • >>> print x
  • MyString

When a name refers to a code block (for example, a function, a module, or an instance of a class), entering the name will provide information about the data type, name, and location of the storage.

The following example creates a class named Message and displays information about the

Him:

  • >>> class Message:
  • ... pass
  • ...
  • >>> Message
  • >>> print Message
  • __main __. Message

Rows

In Python, strings are sequences of characters. A string literal is created by enclosing characters in single ('), double ("), or triple (' '' or" "") quotes. The following example assigns the value of the variable x:

  • >>> x = "MyString"

The Python string has several built-in features. One of them is the ability to return your copy with all lowercase letters. These possibilities are known as methods. To call the object method, use the dot syntax. That is, after entering the variable name, which in this case is a reference to the line object, you need to put the dot operator (.), And then the name of the method followed by opening and closing the parenthesis:

  • >>> x.lower ()
  • "Mystery"

You can get a part of the string using the index operator s [i]. Indexing starts from zero, so s [0] returns the first character in the string, s [1] returns the second character, and so on:

  • >>> x [0]
  • 'M'
  • >>> x [1]
  • 'Y'

String methods work both with normal strings, and with "Unicode". They perform the following actions:

  • Change of the register (capitalize, upper, lower, swapcase, title);
  • Count;
  • Change encoding (encode, decode);
  • Search and replace (find, replace, rfind, index, rindex, translate);
  • Check the execution of conditions (startswith, endswith, isalnum, isalpha, isdigit, islower, isspace, istitle, isupper);
  • Unite and share (join, partition, rpartition, split, splitlines);
  • Format (center, ljust, lstrip, rstring, rjust, strip, zfill, expandtabs).

Python: Lists

If Python strings are limited to characters, then the lists do not have any restrictions. They are ordered sequences of arbitrary objects, including other lists. In addition, you can add, delete and replace their elements. A series of objects, separated by commas inside the square brackets, is a list of Python. What it represents is shown below - here are examples of data and operations with them:

  • >>> bases = ['A', 'C', 'G', 'T']
  • >>> bases
  • ['A', 'C', 'G', 'T']
  • >>> bases.append ('U')
  • >>> bases
  • ['A', 'C', 'G', 'T', 'U']
  • >>> bases.reverse ()
  • >>> bases
  • ['U', 'T', 'G', 'C', 'A']
  • >>> bases [0]
  • 'U'
  • >>> bases [1]
  • 'T'
  • >>> bases.remove ('U')
  • >>> bases
  • ['T', 'G', 'C', 'A']
  • >>> bases.sort ()
  • >>> bases
  • ['A', 'C', 'G', 'T']

In this example, a list of individual characters was created. Then an element was added to the end, the order of the elements was reversed, the elements were extracted at the position of their index, an element with a value of 'U' was deleted and the elements were sorted. Removing an item from the list illustrates the situation when the remove () method needs to provide additional information, namely the value to be deleted.

In addition to methods like remove (), Python has another similar feature called a function. The only difference between a function and a method is that the first is not associated with a particular object.

Python: Functions

Functions perform actions on one or more values and return the result. A lot of them are built into Python. Examples of built-in functions:

  • Len () - returns the number of elements in the sequence;
  • Dir () - returns a list of strings representing the attributes of the object;
  • List () - returns a new list initialized from some other sequence.
  • >>> help (round)
  • Help on built-in function round:
  • Round (...)
  • Round (number [, ndigits]) -> floating point number

It is also possible to define your own functions.

User-defined functions

The process of creating your own Python function is as follows. The first line begins with the keyword def, followed by the name of the function and the arguments (expected input values), enclosed in parentheses, and ending with a colon. The subsequent commands form the body of the function and must be indented. If the comment is at the beginning of the function body, it becomes part of its documentation. The last line of the function returns the result:

  • >>> def transcribe (dna):
  • ... "" "Return dna string as rna string." ""
  • ... return dna.replace ('T', 'U')
  • ...
  • >>> transcribe ('CCGGAAGAGCTTACTTAG')
  • 'CCGGAAGAGCUUACUUAG'

In this example, a function was created called transcribe, which expects a string representing the DNA sequence. The replace () method returns a copy of the original string, replacing all occurrences of one character with another. Three lines of code allowed to transcribe DNA into RNA. The inverse function looks like this:

  • >>> def reverse (s):
  • ... "" "Return the sequence string in reverse order." ""
  • ... letters = list (s)
  • ... letters.reverse ()
  • ... return '' .join (letters)
  • ...
  • >>> reverse ('CCGGAAGAGCTTACTTAG')
  • 'GATTCATTCGAGAAGGCC'

The reverse function takes a string, creates a list based on it, and changes its order. Now we need to do the reverse transformation. An object has a join () method that joins the list, separating each element by the value of the string. Since the delimiter is not needed, the method is used on an empty line represented by two quotation marks ("or" ").

Dictionaries

And the Python dictionary - what is it? It has the same advantages as a conventional paper dictionary. Allows you to quickly find the desired value (definition) associated with the key (word). The dictionaries are enclosed in braces and contain a comma-separated sequence of key-value pairs. Dictionaries are not ordered. Instead, dictionary values are accessible through their key, not their position.

  • >>> basecomplement = {'A': 'T', 'C': 'G', 'T': 'A', 'G': 'C'}
  • >>> basecomplement.keys ()
  • ['A', 'C', 'T', 'G']
  • >>> basecomplement.values ()
  • ['T', 'G', 'A', 'C']
  • >>> basecomplement ['A']
  • 'T'

Classes

In order to create your own objects, you need to define a kind of template, called a class. In Python, a class statement is used, followed by a name and a colon. The body of the class definition contains properties and methods that will be available to all instances of objects based on this class.

Benefits

Most programming languages offer convenient functions, but none of them have the combination of convenience and power offered by Python. What are these advantages? Here are some of them:

  • The language can be built into other applications and used to create macros. For example, in Paint Shop Pro 8 and later, it is the scripting language.
  • Python is free to use and distribute, commercially or not.
  • The language has powerful capabilities for processing and searching for text, which is used in applications that work with a lot of textual information.
  • On it you can create large applications without having to check the programs you are running.
  • Python supports testing and debugging of individual modules and entire programs.

Similar articles

 

 

 

 

Trending Now

 

 

 

 

Newest

Copyright © 2018 en.unansea.com. Theme powered by WordPress.