📜 ⬆️ ⬇️

Python basics in summary

Once upon a time, in one closed forum, I tried to teach Python. In general, the case there has died down. I felt sorry for the written lessons, and I decided to post them for the general public. While the very first, easiest. It goes on more interestingly, but maybe it will not be interesting. In general, this post will be a trial balloon, if you like it, I will spread it further.

Python for beginners. Chapter one. “What are we talking about?”

Just in case, a little boring "evangelism". To whom he is tired, you can skip a few paragraphs.
Python (reads “Python” and not “Python”) is a scripting language developed by Guido van Rossum as a simple, easy-to-learn beginner language.
Nowadays, Python is a widely spoken language that is used in many areas:
- Development of application software (for example, yum, pirut, system-config- * linux-utilities, Gajim IM client and many others)
- Development of web applications (Zope's most powerful Application server and CMS Plone developed on its basis, based on which the CIA website, for example, and a lot of frameworks for rapid application development Plones, Django, TurboGears and many others)
- Use as an embedded scripting language in many games, and not only (in the OpenOffice.org office suite, Blender 3d editor, Postgre DBMS)
- Use in scientific calculations (with SciPy and numPy packages for calculations and PyPlot for drawing graphs. Python becomes almost comparable with MatLab packages)
')
And this is certainly not a complete list of projects using this wonderful language.



1. The interpreter itself, you can take it here (http://python.org/download/).
2. Development Environment. It is not necessary to start, and going to the distribution IDLE suitable for a beginner, but for serious projects need something more serious.
For Windows, I use the wonderful lightweight PyScripter (http://tinyurl.com/5jc63t), for Linux, the Komodo IDE.

Although for the first lesson it will be enough just an interactive shell of Python himself.

Just run python.exe. The input prompt will not take long, it looks like this:

  >>> 


You can also write programs to files with the extension py, in your favorite text editor, which does not add markup to the text (no Word will work). It is also desirable that this editor be able to do “smart tabs” and not replace spaces with a tab character.
To launch files for execution, they can be clicked 2 times. If the console window closes too quickly, insert the following line at the end of the program:

  raw_input () 


Then the interpreter will wait for pressing enter at the end of the program.

Or associate the py files in Far with Python and open it by pressing enter.

Finally, you can use one of the many convenient IDEs for Python, which provide debugging features and syntax highlighting and many other “conveniences”.

A bit of theory.

For a start, Python is a language with strict dynamic typing. What does this mean?

There are languages ​​with strict typing (pascal, java, c, etc.), in which the variable type is predefined and cannot be changed, and there are languages ​​with dynamic typing (python, ruby, vb), in which the variable type is interpreted in depending on the assigned value.
Languages ​​with dynamic typing can be divided into another 2 types. Strict ones that do not allow implicit type conversions (Python) and non-strict ones that perform implicit type conversions (for example, VB, in which you can easily add the string '123' and the number 456).
Having dealt with Python's classification, we will try to play a little with the interpreter.

  >>> a = b = 1
 >>> a, b
 (eleven)
 >>> b = 2
 >>> a, b
 (12)
 >>> a, b = b, a
 >>> a, b
 (2, 1) 


Thus, we see that the assignment is done using the = sign. You can assign a value to several variables at once. When the interpreter specifies the name of a variable in interactive mode, it displays its value.

The next thing you need to know is how basic algorithmic units are built - branches and loops. To get started, you need a little help. In Python there is no special limiter of code blocks, their role is performed by indents. That is, what is written with the same indentation is one command block. At first it may seem strange, but after a little addictive, you understand that this “forced” measure allows you to get a very readable code.
So conditions.

The condition is set using the if statement, which ends with ":". Alternative conditions that will be met if the first “failed” check are specified by the elif operator. Finally, else specifies the branch that will be executed if none of the conditions match.
Please note that after entering the if, the interpreter using the "..." prompt indicates that it expects input to continue. To inform him that we are finished, you must enter an empty line.

(An example with branching for some reason breaks markup on Habré, despite dancing with pre and code tags. Sorry for the inconvenience, I threw it here pastebin.com/f66af97ba , if someone tells you what’s wrong, I will be very grateful)

Cycles

The simplest case of a loop is a while loop. As a parameter, it takes a condition and executes as long as it is true.
Here is a small example.

  >>> x = 0
 >>> while x <= 10:
 ... print x
 ... x + = 1
 ...
 0
 one
 2
 ...........
 ten 


Note that since both print x and x + = 1 are written with the same indentation, they are considered the body of the loop (remember what I said about the blocks? ;-)).

The second kind of cycles in Python is the for loop. It is similar to the foreach loop of other languages. Its syntax is conventionally as follows.

for variable in list:
teams

The variable will be assigned in turn all the values ​​from the list (in fact, there can be not only a list, but any other iterator, but we will not forget about it for now).

Here is a simple example. The list will be a string, which is nothing more than a list of characters.

  >>> x = "Hello, Python!"

 >>> for char in x:
 ... print char
 ...
 H
 e
 l
 ...........
 ! 


In this way, we can expand the string into characters.
What to do if we need a cycle that repeats a certain number of times? Very simple, the range function comes to the rescue.

At the input it takes from one to three parameters, and at the output it returns a list of numbers, by which we can “walk” with the for operator.

Here are some examples of using the range function, which explain the role of its parameters.

  >>> range (10)
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
 >>> range (2, 12)
 [2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
 >>> range (2, 12, 3)
 [2, 5, 8, 11]
 >>> range (12, 2, -2)
 [12, 10, 8, 6, 4] 


And a small example with a cycle.

  >>> for x in range (10):
 ... print x
 ...
 0
 one
 2
 .....
 9


Input Output

The last thing you should know before you start using Python fully - this is how input-output is carried out in it.

For output, use the print command, which prints all its arguments in a readable form.

For input from the console, the raw_input function (prompt) is used, which displays the prompt and waits for user input, returning what the user entered as its value.

  x = int (raw_input ("Type the number:"))
 print "The square of this number is", x * x 


Attention! Despite the existence of the input () function of a similar action, it is not recommended to use it in programs, as the interpreter tries to execute syntactic expressions introduced with it, which is a serious security hole in the program.

That's all for the first lesson.

Homework.

1. Create a program for calculating the hypotenuse of a right triangle. The length of the legs is requested from the user.
2. Create a program for finding the roots of a quadratic equation in general. Odds are requested from the user.
3. Create a program for outputting the multiplication table by the number M. The table is composed from M * a to M * b, where M, a, b are requested from the user. The output should be carried out in a column, one example per line in the following form (for example):
5 x 4 = 20
5 x 5 = 25
And so on.

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


All Articles