Python includes the following keywords:
The CPU understands a language we call machine language. Machine language is very simple and frankly very tiresome to write because it is represented all in zeros and ones:
Machine language seems quite simple on the surface, given that there are only zeros and ones, but its syntax is even more complex and far more intricate than Python.
programming language translators fall into two general categories: (1)
An interpreter reads the source code of the program as written by the programmer, parses the source code, and interprets the instructions on the fly. Python is an interpreter and when we are running Python interactively, we can type a line of Python (a sentence) and Python processes it immediately and is ready for us to type another line of Python.
Some of the lines of Python tell Python that you want it to remember some value for later. We need to pick a name for that value to be remembered and we can use that symbolic name to retrieve the value later. We use the term variable to refer to the labels we use to refer to this stored data.
>>> x = 3 >>> print(x) 3 >>> y = x * 4 >>> print(y) 12 >>>
In this example, we ask Python to remember the value three and use the label x so we can retrieve the value later. We verify that Python has actually remembered the value using print. Then we ask Python to retrieve x and multiply it by four and put the newly computed value in y. Then we ask Python to print out the value currently in y.
Now at this point in our discussion of compilers and interpreters, you should be wondering a bit about the Python interpreter itself. What language is it written in? Is it written in a compiled language? When we type "python", what exactly is happening?
The Python interpreter is written in a high-level language called "C". You can look at the actual source code for the Python interpreter by going to www.python.org and working your way to their source code. So Python is a program itself and it is compiled into machine code. When you installed Python on your computer (or the vendor installed it), you copied a machine-code copy of the translated Python program onto your system. In Windows, the executable machine code for Python itself is likely in a file with a name like:
That is more than you really need to know to be a Python programmer, but sometimes it pays to answer those little nagging questions right at the beginning.
To write a program in Python, we use a text editor to write the Python instructions into a file, which is called a script. By convention, Python scripts have names that end with .py. To execute the script, you have to tell the Python interpreter the name of the file. In a command window, you would type python hello.py as follows:
$ cat hello.py print('Hello world!') $ python hello.py Hello world!
The "$" is the operating system prompt, and the "cat hello.py" is showing us that the file "hello.py" has a one-line Python program to print a string.
We call the Python interpreter and tell it to read its source code from the file "hello.py" instead of prompting us for lines of Python code interactively.
The proper way to say "good-bye" to Python is to enter
quit() at the interactive
chevron >>> prompt.
But you will notice that there was no need to have quit() at the end of the Python program in the file. When Python is reading your source code from a file, it knows to stop when it reaches the end of the file.
Below is an example with
>>> print('Hello world!') Hello world! >>> quit()
We will continue the discussion in Learning Python Part 2...
If you like dEexams.com and would like to contribute, you can write your article here or mail your article to email@example.com . See your article appearing on the dEexams.com main page and help others to learn.