## tutorial Python - Python shell

on 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.

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:

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:

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.

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.

The first operator uses the symbol "//". It allows to obtain the whole part of a division.

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.

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.

>>>

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

>>>

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

>>>

- Code:
`>>> 9.5`

9.5

>>>

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

>>>

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

>>>

The first operator uses the symbol "//". It allows to obtain the whole part of a division.

- Code:
`>>> 10 // 3`

3

>>>

- Code:
`>>> 10%3`

1

>>>

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.

- Problem with brackets[?] in css selector in Selenium-python
- Monster Python: 17ft Snake Caught In Florida
- Chrome driver's ChromeOptions persisting across calls - Python selenium bindings
- Can i automate silverlight web application using selenium driver for python.
- python selenium - using chrome with extensions/ add ons

**Permissions in this forum:**

**cannot**reply to topics in this forum