Python Data Persistence – Installation

Python Data Persistence – Installation

Python’s official website hosts the official distribution of Python at https:// www.python.org/downloads/. Precompiled installers, as well as source code tarballs for various operating system platforms (Windows, Linux, and Mac OS X) and hardware architectures (32 and 64-bit), are available for download. The bundle contains a Python interpreter and a library of more than 200 modules and packages.

Precompiled installers are fairly straightforward to use and recommended. Most distributions of Linux have Python included. Installation from source code is a little tricky and needs the expertise to use compiler tools.

Currently, there are two branches of Python software versions (Python 2.x and Python 3.x) on the Python website. At the time of writing, the latest versions in both branches are Python 2.7.15 and Python 3.7.2 respectively. Python Software Foundation (PSF) is scheduled to discontinue supporting the Python 2.x branch after 2019. Hence it is advised to install the latest available version of the Python 3.x branch.

It is also desirable to add Python’s installation directory to your system’s PATH environment variable. This will allow you to invoke Python from anywhere in the filesystem.

It is now time to start using Python. Open the Windows Command Prompt terminal (or Linux terminal), type ‘python ’ in front of the prompt as shown below: (figure 1.1)

C:\Users\acer>Python
Python 3 . 7 . 2 (tags/v3 . 7 . 2 : 9a3ffc0492, Dec 23 2018,
23 : 09 : 28) [MSC v . 1916 64 bit (AMD64) ] on win32
Type "help", "copyright" , "credits" or "license" for more information.
>>>

If a Python prompt symbol »> (made up of three ‘greater than characters) appears, it means Python has been successfully installed on your computer.
Congratulations!!!

Most Python distributions are bundled with Python’s Integrated Development and Learning Environment (IDLE). It also presents an interactive shell as shown in Figure 1.1. Additionally, it also has a Python-aware text editor with syntax highlighting and smart indent features. It also has an integrated debugger.

python data presistance - Installation chapter 1 img 2

Interactive Mode

The >>> prompt means that Python is ready in REPL mode. You can now work with Python interactively. The prompt reads user input evaluates if it is a valid Python instruction, prints the result if it is valid or shows an error if invalid, and waits for input again. In this mode, the Python interpreter acts as a simple calculator. Just type any expression in front of the prompt and press Enter. Expression is evaluated with the usual meanings of arithmetic operators used and the result is displayed on the next line.

Example

>>> 5-6/2*3
-4.0
>>> (5-6/2)*3
6.0
>>>

You can assign a certain value to a variable by using the ‘=’ symbol. (What is variable? Don’t worry. I am explaining it also later in this chapter!) However, this assignment is not reflected in the next line before the prompt. The assigned variable can be used in further operations. To display the value of a variable, just type its name and press Enter.Python operators follow the BODMAS order of precedence. There are few more arithmetic operators defined in Python. You will learn about them later in this chapter.

Example

>>> length=20
>>> breadth=30
>>> area=length*breadth
>>> area
600

Scripting ModeType ‘quit( )’ before the prompt to return to the command prompt.

Interactive mode as described above executes one instruction at a time. However, it may not be useful when you have a series of statements to be repetitively executed. This is where Python’s scripting mode is used. The script is a series of statements saved as a file with the ‘.py’ extension. All statements in the script are evaluated and executed one by one, in the same sequence in which they are written.

The script is assembled by using any text editor utility such as Notepad (or similar software on other operating systems). Start Notepad on Windows computer, enter the following lines and save as ‘area.py’

Example

#area.py
length=20
breadth=30
area=length*breadth
print (1area=',area)

Open the command prompt. Ensure that the current directory is the same in which the ‘area.py’ script (you can call it a program) is saved. To run the script, enter the following command (figure 1.2):

C : \ Users \ acer>Python area . py
area = 600

Comments

Any text that follows the ‘#’ symbol is ignored by the Python interpreter. This feature can be effectively used to insert explanatory comments in the program code. They prove to be very useful while debugging and modifying the code. If a symbol appears in a line after a valid Python statement, the rest of the line is treated as a comment.

Example

>>> #this line is a comment
... print ("Hello world!")
Hello, world!
>>> print ("hello world again!") #this also a comment
hello, the world again!

Multiple lines of text which are enclosed within triple quote marks are similar to comments. Such text is called ‘docstring’ and appears in the definition of the function, module, and class. You will come across docstrings when we discuss these features in subsequent chapters.

Python Data Persistence – Getting Started

Python Data Persistence – Program Flow Control