Getting Started with Python:
how python works
Behind the scenes of Python
Whether you run Python code as a script or run it in a shell, the interpreter does a lot of work to complete the execution of the program’s instructions, which can be divided into a series of steps:
1. The interpreter reads a Python expression or sentence, the so-called source code, and verifies that it is syntactically well formulated. In this phase the interpreter behaves like a rigorous English teacher who rejects any sentence that does not conform to the syntax, that is, to the grammatical rules of the language itself. As soon as the interpreter detects an error, it stops the translation with an error message.
2. If a Python expression is well-formulated, the interpreter translates it into its equivalent form in a low-level language, called the byte code. When the interpreter executes a script, it translates it completely into byte code.
3. This byte code is passed to another piece of software, called the Python virtual machine (PVM), where it runs. If an error occurs during this phase, execution stops with an error message.
Write, save and run a program
Although it is easy to do interactive experiments with the short Python expressions and instructions proposed here using the shell prompt, in the case of longer and more complex programs it is preferable to write them and save them in a file, and then possibly also modify them. Later we can run these “program files” or scripts either within IDLE or directly at the operating system prompt, without even opening IDLE. Script files are also vehicles for distributing Python programs to users. Besides, files allow you to permanently and securely archive many hours of work.
Here are the steps to write and run programs in this way:
1. Select the New File option from the IDLE File menu.
2. In the new window, write your Python expressions or utterances, line after line, in the order you want them executed by Python.
3, You can “save” the file at any time by selecting File / Save and using the .py extension for the file name.
4. To run this code file as a Python script, select Run Module from the Run menu.
The command provided in step 4 reads the code from the file and reads it. If python executes the print function somewhere in the code, you will see the information in the shell window as usual. If the code requires input data, the interpreter will pause execution to allow users to type it, otherwise, program execution will continue without visible effect, behind the scenes. When the interpreter has executed the last statement, it ends its execution and returns control to the shell prompt.
Input and Output
Most programs acquire input data through some sort of information, then process it, and finally display the processing results in some output device. In interactive programs based on the terminal, ie without a graphical user interface, the input source (most common) is the keyboard, while the output device is usually the terminal window, on the screen.
The programmer can also explicitly request the display of a value using the print function, the simplest form of which is this:
>>> print(“Hi there”)
When designing programs in Python, you will often want programs to ask the user to provide input data: this can be done using the input function. This function suspends program execution, causing it to wait for a value typed by the user on the keyboard. After the user has pressed the “Return” or “Enter” key, the function acquires the value and makes it available to the program, which stores it for processing.
>>> fruit = input(“Enter the name of your favorite fruit: ”)
Enter the name of your favorite fruit: Apple
This is the behavior of the input function:
1. Displays a prompt, which in this example is “Enter the name of your favorite fruit:”.
2. It acquires a string (which is a sequence of characters, corresponding to the keys typed by the user on the keyboard) and makes it available.
How does the input function know what to use as a prompt? the text in brackets “Enter the name of your favorite fruit:” is an argument given to the input function that specifies what it should use as a prompt. An argument or parameter is information that a function needs to perform its task.
In our example, the string returned by the function is stored by assigning it to the variable fruit.
A variable is simply a name given to a value. After a variable has received its value during the execution of an input data acquisition statement, it will be used to refer to that value. The input function always constructs a string containing the characters corresponding to the keys typed by the user and returns it to the program. After acquiring strings representing numbers, the programmer must convert them to values of appropriate numeric types. For this purpose, there are two functions in the Python language for converting numeric types: int for integers and float for real numbers.
>>> one = int(input (“Enter a number: ”))
Enter a number: 10
>>> two = int(input (“Enter a number: ”))
Enter a number: 34
>>> print (“The sum is: ”, one + second)
The sum is: 44
Observe how the int function has been invoked providing as its argument the string returned by the input function, after which the two numbers are added, displaying the result.