📜 ⬆️ ⬇️

We master Python. Ounce Zero. Introduction

Prehistory



I join MaxElc , DarwinTenk and Devgru :) I start a series of articles on Python. I myself have some experience with PHP and Java. But each time, with the relative development of a tool, there remained a certain dissatisfaction with it related to something concrete, and the search continued. Today, Python is closest to the ideal in my eyes. The ideal is unattainable - this is understandable, therefore, and Python has flaws. First of all, this is the speed of implementation, however, this problem can be solved in several ways, and we will definitely discuss this later.
I myself began to learn Python just recently. Starting this series of articles - I have several goals. Firstly, this is an additional self motivation + interactivity, and secondly, experience. Thirdly, wandering across the expanses of the runet - I see that Python is much less popular than in the world. The situation needs to be corrected :)
In accordance with the Python ideology, namely the fact that one of its main trump cards is speed of development and speed of development, we will quickly and practically take a brief look at the basics of syntax and building programs and move on to the main goal of this cycle.
So we start.

Architecture



Python is an interpretable, scripting programming language. When you first run the script, the interpreter translates the language instructions into byte-code, saving them in a file with the extension .pyc. Next, the bytecode runs on a Python virtual machine (PVM). When the script is run again, the interpreter keeps track of whether changes have been made to it. If not, the .pyc file starts immediately.
Python implementations:

')

Options for running programs



On Habré, a good guide for newbies has already been written, how to quickly organize a working environment for developing in Python and django. If you have already done the sequence of actions described in it, this means that the python interpreter is already installed on your machine. In most Linux distributions, it is installed by default.

Option 1. Interactive mode


To enter the interactive mode, you must enter the python command on the command line.

$ python
Python 2.5.2 (r252:60911, Oct 5 2008, 19:24:49)
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print 'Hello World!'
Hello World!


In interactive mode, instructions are executed line by line. In order to execute a block of code, you can, for example, type the following two commands in an empty text file:

str1 = 'Hello World!'
str2 = " It's my second script"
print 'import has been done successfully'


And save them in the hi.py file. Next, on the command line, use the cd command to go to the directory where you saved the file and type the python command.
To import instructions, you can use the import statement <filename without the .py> extension. Files in Python are modules that contain a namespace within themselves. Importing the module, we get access to the top-level namespace.

$ python
Python 2.5.2 (r252:60911, Oct 5 2008, 19:24:49)
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import hi
import has been done successfully
>>> print hi.str1+hi.str2
Hello World! It's my second script


Using the following construction in the instruction: hi.str1, we refer to the name str1, defined inside the hi module.
Strings, numbers in the Python language are also objects. Here the inheritance of the paradigm originating from the SmallTalk language “Everything - Objects” is traced. However, if my memory serves me, it was about the fact that the operators were also objects. In python, the “+” operator is an overloaded string operator that performs concatenation.
You can use the from statement to import specific names:

>>> from hi import str1
>>> print str1
Hello World!


After which we will be able to access the imported name directly.
After making any changes to the file in order for the changes to take effect, you must reload the module using the reload () function.

>>> reload(hi)
<module 'hi' from 'hi.pyc'>


To get a list of all available module names, you can use the dir () function:

>>> dir(hi)
['__builtins__', '__doc__', '__file__', '__name__', 'str1', 'str2']


Option 2. Run from the command line.


You can run the script from the command line as follows:

$ python hi.py

You can also pass command line parameters to the script. They can be accessed from the script by importing the built-in sys module. Modify the hi.py file as follows:

import sys
print sys.argv


Then call it from the command line, passing several arbitrary parameters:

$ python hi.py 1 2 3 'param-pam-pam'
['hi.py', '1', '2', '3', 'param-pam-pam']


As a result, we get a list containing the passed parameters. The list is also a Python language construct. Lists will be discussed in the following articles.
Script input and output streams can be redirected using shell commands, like this:

$ python hi.py 1 2 3 'param-pam-pam' > text.txt

Option 3. IDE.


Interactive mode is useful in some cases, for example, when you need to experiment with some specific instruction. However, it is inconvenient to constantly work in it. You need to constantly remember about the reloading of modules.
Therefore, it is much more convenient to use the IDE setup procedure described above and launch programs from it. Also, when developing programs in Python, the option of writing code in a notebook with syntax highlighting and running the program from the console is quite acceptable.

That's all for today. In the following articles we will proceed directly to the study of language constructs. No doubt, the responses will be and I will be guided by them.
Thanks for attention!

Source: https://habr.com/ru/post/49428/


All Articles