Image for post
Image for post

Getting Started with Python:
how python works

First steps in the world of Python

Behind the scenes of Python

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

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

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”)
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
>>> fruit
>>> print(fruit)

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.

Mechanical engineer and technology enthusiast

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store