Digression: the name “terminal” derives from the fact that this software performs the same function as the old physical terminals, i.e. machines of the early computer age, whose only purpose was to act as a link between the human operator and the main-frame, receiving commands (the input) from the operator to the mainframe, and getting the result of the command (the output) back from the mainframe to the operator. In our context, we call “terminal” the interface that we use to interact with our operating system. With that, we can make use of the same functionalities (and more) as those provided by the graphical interface – for example, starting applications, copying and handling files, changing the configuration of our system, and so on.The command line includes a flashing cursor after a line similar to C:\Users\Angelo>, which indicates that it is ready to receive our commands. This line is called the current directory of the command line. The current directory is crucial as it determines how we can indicate which programs to run. To run the IDLE editor, we type the word idle followed by the Enter key and wait.
(C:\Users\Angelo\Anaconda3\) C:\Users\Angelo>idleIn this way, we ask the operating system to start the program under the name “idle.exe”. A white window will open with a different prompt from the terminal one.
BEWARE!! If the idle invocation returns a “Command not found” error, then we have not executed the Anaconda command line. This is because the programs we installed with Anaconda are NOT known from the Windows command line, but must be manually included – that is, they are included in the Anaconda prompt. More precisely, the path in the filesystem where the executable files are located is not included in the PATH environment variable, which contains the list of paths used by the command line to search for executable files.This command line is now our interface with the Python interpreter! Here we can enter our Python commands and see the result of their execution. To open the editor, select from the File → New File menu entry to open the text editor. The text editor is different from a document editor (word processor): Microsoft Word or Google Docs or Apple Pages and the like can produce and format beautiful documents, but they are not designed to show and access the actual content of a file. When we create a document, we use a lot of characters that, in reality, are not shown on the screen: the so-called special characters and formatting information. Our Python interpreter cannot run programs that include such characters: Python (and in general any programming language) needs files with pure text (raw text), i.e., sequences of characters. Text editors are in fact able to generate such file types.
C:\Users\Angelo\Documents\Python Scripts>meaning that the current directory is the "Python Scripts" directory, within the "Documents" directory, within the "Angelo" directory, within the "Users" directory of the "C:" disk.
mypc:Documents/Python Scripts angelo$ pwd
/home/angelo/Documents/Python Scripts
mypc:Documents/Python Scripts angelo$
A very useful tip to know about the command line is that it provides many keyboard shortcuts that make it quick to use. In particular:To use the command line properly, two more commands are fundamental:
- the auto-completion with the TAB key (the one just above the Caps Lock key of your keyboard), which helps you to automatically complete file names and commands. For example if you start writing "cd Doc" and press the TAB key, the command line will automagically complete the command in "cd Documents"). Imagine the convenience if the file name we want to write is something like user_assignments_settings_with_fixed_names_v.1.4.05-rev3.12.json or alike...
- the history navigation, which consists of caching all the executed commands, and allowing you to recall any previously entered command to be executed again or altered before execution
C:\Users\Angelo> cd DocumentsTo go back into the outer directory, we use a special directory name that is ".." (two dots), as in the following:
C:\Users\Angelo\Documents>
C:\Users\Angelo\Documents> cd ..For Linux and MacOS, the cd command has the same syntax. Why is it important to know the current command line directory? Because when we want to run a python program we must be sure that the file to run is in the current directory. Alternatively, we must specify to Python the full sequence of the directory, that is the absolute file path, that includes all the subdirectories. Example: to run the hello.py file we created before, we could use the following commands (which are equivalent)
C:\Users\Angelo>
C:\Users\Angelo\Documents\Python Scripts> python hello.py
C:\Users\Angelo>python "C:\Users\Angelo\Documents\Python Scripts\hello.py"
WARNING!! Since the hello.py file we created before does NOT contain Python code, we will get an error... Moreover, observe that in the second version of the command we had to use double quotes (" ") because there is a whitespace character in the name of one of the directories, namely the Python Scripts one. Finally, in Linux or MacOS the two commands would be slightly different, since the directories are indicated with the slash (/), instead of the backslash (\), that is typical of Windows command lines.
>>> 7*3We continue discovering that Python provides the concept of function, which is a very practical way to define a series of instructions to be repeated and to give it a name. Moreover, Python provides a lot of functions that help you write useful programs. One of the functions that we will use very often is the print function, which is the function that prints something on screen. When we want to use a function in Python we must always use round brackets. So we can ask Python to print on screen the string "Hello world!" using round brackets and quotes to define a string in the following way:
21
>>> 5/2
2.5
>>> 5//2
2
>>> 5%2
1
>>> 5**2
25
>>> 10**100
10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
>>> 5*(4+3)/2
17.5
>>>
>>> print("Hello world!")We can also ask Python to print a string and the result of an operation, inserting the two elements separated by a comma:
Hello world!
>>>
>>> print ("Zero", 4+4-4-4)
Zero 0
>>>
print("Hello world!")We already know that the above command essentially prints "Hello world!" on screen. We can observe that the characters we entered are colored differently, because the IDLE editor helps usto distinguish the syntax of python: function names have one color, symbols have another and strings have another. We can save the file from the IDLE File->Save menu. Our program should be saved as C:/Users/Angelo/Documents/Python Scripts/hello.py (your path should be consistent with your username and home directory). Let us go in the operating system command line and let us make sure we are in the right directory —in case, remember to use the cd command to change the directory— and type dir (or ls) to make sure the file is there. And now we can run the hello.py program by using the command python hello.py in the command line of the operating system:
C:\Users\Angelo\Documents\Python Scripts> python hello.pyRight after we press the Enter key, the program is ran and, as expected, the program we wrote simply prints the "Hello world!" string on screen. We can now create any other python program by simply selecting from our Python editor or Python interpreter the command File->New File.
Hello world!
C:\Users\Angelo\Documents\Python Scripts>
# Four fours challengeand saving it with a name like fourfours.py. If you run the program as above described, you should obtain as an output the string "Zero is 0". You can notice that in the program we inserted a line from math import : this is a special line for Python that allows us to use some special mathematical function like *factorial (to evaluate the factorial of a number) and sqrt (to evaluate the squared root of number): for example, factorial(5) evaluates 5!=5*4*3*2*1=120 while sqrt(9) evaluates the square root of 9, namely 3:
# Filename: fourfours.py
# Problem description: The four fours challenge!
from math import *
print("Zero is", 4+4-4-4)
>>> sqrt(9)
3
>>> factorial(5)
120
>>>
Questo sito usa cookies, usandolo ne accettate la presenza. (CookiePolicy)
Torna al Dipartimento di Informatica |
|