tutorial net
*********WOOOOOOW *********
We're happy to see you're enjoying our races (already 5 pages viewed today)! You can keep checking out by becoming a member of the tutorial net community. It's free!
You will also be able to keep track of your race progress, practice on exercises, and chat with other members.

Join the forum, it's quick and easy

tutorial net
*********WOOOOOOW *********
We're happy to see you're enjoying our races (already 5 pages viewed today)! You can keep checking out by becoming a member of the tutorial net community. It's free!
You will also be able to keep track of your race progress, practice on exercises, and chat with other members.
tutorial net
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Go down
avatar
Admin
Admin
Posts : 207
Join date : 2017-11-11
Age : 33
Location : algeria
http://www.tutorial-net.com

tutorial Python - Python shell Empty tutorial Python - Python shell

Fri Mar 30, 2018 10:47 am
After the first theoretical notions and the installation of Python, it is time to discover a little the shell of this language. Even if these small tests seem innocuous, you will discover in this chapter the first rudiments of the language syntax and I strongly advise you to follow me step by step, especially if you are facing your first programming language.

Like any programming language, Python has a clear syntax: you can not send it any information in any order. We'll see here what Python eats ... and what he does not eat.

Where are we?
First, I'll ask you to go back to the Python shell (I showed you, at the end of the previous chapter, how to access it depending on your operating system).

I remind you of the information in this window, although they may be different at home depending on your version and your operating system.

Code:
Python 3.4.0 (v3.4.0: 04f714765c13, Mar 16 2014, 19:24:06) [MSC v.1600 32bit (In
tel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
In its own way, Python welcomes you to its shell.

Wait, wait. What is this interpreter?

Remember, in the previous chapter, I gave you a brief explanation of the difference between compiled languages ​​and interpreted languages. Well, this shell will allow us to test code directly. I grab a line of instructions, I press the key Entréeon my keyboard, I look at what Python tells me (if he tells me something), then I enter a second, a third ... This interpreter is particularly useful to understand the basics of Python and realize our first small programs. The main disadvantage is that the code you enter is not saved (unless you save it manually, but everything in due course).

In the window you have before you, information that does not change from one operating system to another is the series of three chevrons located at the bottom left of the information >>>. These three signs mean: "I am ready to receive your instructions".

As I said, programming languages ​​follow a clear syntax. You can not expect the computer to understand if in this window you start by asking, "I would like you to code me a great video game." And as far as you know right now (although in my opinion, you do not care), we are very far from getting such spectacular results at our level.

All this to say that, if you enter anything in this window, the probability is high that Python tells you, clearly and firmly, that he did not understand anything.

If, for example, you enter "first test with Python", you get the following result:
Code:
>>> premier test avec Python
File "<stdin>", line 1
premier test avec Python
^
SyntaxError: invalid syntax
>>>
Yes, the interpreter speaks in English and the instructions you will enter, as for the overwhelming majority of programming languages, will also be in English. But for now, nothing complicated: the interpreter tells you that he found a problem in your line of instruction. It tells you the number of the line (in this case the first), that it repeats you obligingly (this is very useful when one works on a program of several hundreds of lines). Then he tells you what stops him here SyntaxError: invalid syntax. Clear, is not it? What you have entered is incomprehensible to Python. Finally, the proof that he is not resentful is that he shows you again a series of three chevrons, showing that he is ready to retry the adventure.

Well, it's nice to receive an error message at the first test but I doubt that you would like to see things that work now. So here we go.

Your first instructions: a little mental math for the computer
It's pretty trivial when you think about it, but I think it's a great way to step through Python's syntax. We will therefore try to obtain the results of more or less complicated calculations. I remind you once again that running the tests at the same time as me on your machine is a very good way to realize the syntax and especially, to retain it.

Enter a number
You could see on our first (and to this day our last) test that Python did not particularly like the sequences of letters that he does not understand. On the other hand, the interpreter loves numbers. Besides, he accepts them without flinching, without a single error:
Code:
>>> 7
7
>>>
Okay, it's not extraordinary. We enter a number and the interpreter sends it back. But in many cases, this simple return indicates that the interpreter has understood and that your input is in agreement with its syntax. Similarly, you can enter decimal numbers.
Code:
>>> 9.5
9.5
>>>
Attention: we use here the Anglo-Saxon notation, that is to say that the point replaces the comma. The comma has a different meaning for Python, so take this habit now.

It goes without saying that one can just as easily enter negative numbers (you can also try).

Current operations
Well, it's time to learn how to use the main Python operators, who will serve you for the vast majority of your programs.

Addition, subtraction, multiplication, division
To carry out these operations, the symbols +, -, * and / are used respectively.
Code:
>>> 3 + 4
7
>>> -2 + 93
91
>>> 9.5 + 2
11.5
>>> 3.11 + 2.08
5.1899999999999995
>>>
Why this last approximate result?

Python is not for much. In fact, the problem comes largely from the way the decimal numbers are written in
your computer's memory . This is why, in programming, one prefers to work as much as possible with integers. However, you will notice that the error
is small and that it will have no real impact on the calculations. Applications that require mathematical precision foolproof try to overcome these defects in other ways but here it will not be necessary.

Also do tests for subtraction, multiplication and division: there is nothing difficult.

Whole division and modulo
If you took the time to test the division, you realized that the result is given with a floating point.
Code:
>>> 10 / 5
2.0
>>> 10 / 3
3.3333333333333335
>>>
There are two other operators that make it possible to know the result of an entire division and the rest of this division.

The first operator uses the symbol "//". It allows to obtain the whole part of a division.
Code:
>>> 10 // 3
3
>>>
The operator "%", which we call the "modulo", allows to know the rest of the division.
Code:
>>> 10%3
1
>>>
These concepts of whole part and remainder of division are not very difficult to understand and will serve you very probably later.

If you have trouble grasping the meaning, then know that:

The integer part of the division of 10 by 3 is the result of this division, without taking into account the numbers beyond the comma (in this case, 3).

To obtain the modulo of a division, one "recovers" its rest. In our example, 10/3 = 3 and it remains 1. Once we understand this, it is not very complicated.

Remember these two operators, and especially the modulo "%", which you will need in your future programs.

In summary
The Python shell is used to test code as it is written.

The Python interpreter accepts numbers and is able to perform calculations.

A decimal number is written with a period and not a comma.

Calculations involving decimals sometimes give approximate results, so we will prefer to work with integers whenever possible.
Back to top
Permissions in this forum:
You cannot reply to topics in this forum