Python for beginners

Learning Python Part 1

learning_python_part_1

"Python is a high-level language intended to be relatively straightforward for humans to read and write and for computers to read and process"

Keywords

Python includes the following keywords:

and del global not with
as elif if or yield
assert else import pass
break except in raise
class finally is return
continue for lambdatry
def from nonlocal while

Interpreter and compiler

Python is a high-level language intended to be relatively straightforward for humans to read and write and for computers to read and process. Other high-level languages include Java, C++, PHP, Ruby, Basic, Perl, JavaScript, and many more. The actual hardware inside the Central Processing Unit (CPU) does not understand any of these high-level languages.

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:

001010001110100100101010000001111
11100110000011101010010101101101
...

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.

So very few programmers ever write machine language. Instead we build various translators to allow programmers to write in high-level languages like Python or JavaScript and these translators convert the programs to machine language for actual execution by the CPU.

programming language translators fall into two general categories: (1) interpreters and (2) compilers.

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.

Python Written in which language

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:

C:\Python35\python.exe

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.

Writing a program

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 quit()

>>> print('Hello world!')
Hello world!
>>> quit()

We will continue the discussion in Learning Python Part 2...

Meanwhile you can check How to download and Install Python on Windows .
read also Python database connection


If you like dEexams.com and would like to contribute, you can also write your article here or mail your article to admin@deexams.com . See your article appearing on the dEexams.com main page and help others to learn.


Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


Python if , elif and else

Python Conditions and If statements

  • 0
Python for beginners

Learning Python Part 1

  • 3
Struct Alignment and Padding

Struct Alignment and Padding in C++ And C

  • 0
Friend function

Friend function C++

  • 0
Pointers

C++ Pointers

  • 0
Structures

C++ Structures

  • 0
Types of Inheritance in C++

Inheritance and access specifiers C++

  • 0
Java date pattern

Java Date Pattern Syntax

  • 0
Java Date and Calendar

Java Date formats

  • 0
JAVA Data Type

Data types in Java

  • 0
Java unreachable code

Unreachable Code Error in Java

  • 0

Post Comment

Comments(3)

Commenter Profilr Pic

Ashish on Jan 07, 2019 at 08:11 am

When will Python Part 2 come??


Commenter Profilr Pic

Admin on Apr 29, 2019 at 03:21 pm

Python learning Part2 has come now.


Commenter Profilr Pic

admin on Jan 07, 2019 at 04:34 pm

Soon.. Please subscribe to dEexams.com so that you will be notified whenever the content is added.


WEB TECHNOLOGY

Articles

×

Forgot Password

Please enter your email address below and we will send you information to change your password.