Python interpreter and data types

An interpreter, as the name suggests, is used to interpret instructions so that they are understandable by others. In our case, it is used to convert our Python language to a machine-understandable format that governs the flow of instructions that we gave to the machine.

It is also used to convert the set of values and messages given by a machine to a human-readable format in order to give us insights into how our program is being executed.

As mentioned in Chapter 1, Fundamental Concepts, the interpreter that we are focusing on is Python 3.6. I will be using it on the Windows platform, but the site has clear instructions on how to download and install the same on other OS like Unix or Linux machines. Once we install it by downloading it from the Python community  which can be found at URL https://www.python.org/downloads, we can simply click on the setup file to install it. From the installation directory we just need to invoke python.exe, which will invoke the Python interpreter.

In order to call Python from anywhere in your Command Prompt, just add the Python installation folder in your PATH variable.
Here's an example: set path=%path%;C:python36. This is going to add the Python36 path in the current path. Once this is done, python.exe can be called from anywhere in the Command Prompt.

Once we invoke the interpreter, the first step to take is to create a variable and assign a value to it.

Python, as with any other programming language, supports various data types for the variables. A data type typically defines the type of value that can be stored in a variable, but Python and PowerShell have the ability to auto-evaluate the type of variable based upon the value. Python supports a large number of data types, but typically in our daily usage we refer to native data types multiple times.

The Python data type supports:

  • Numbers: These are integer types, such as 1, 2, 100, and 1,000.
  • String: These are single or multiple characters and possibly every letter of ASCII, such as Python, network, age123, and India. Additionally, a string needs to be stored inside a double quote (") or a single quote (') to specify that a value is a string. Hence, 1 and '1' would be interpreted differently by Python.
  • Boolean: This can be either a true or a false value.
  • Byte: These are typically binary values.
  • Lists: These are an ordered sequence of values.
  • Tuples: These are similar to lists, but the values or length cannot be altered.
  • Sets: These are similar to lists, but not ordered.
  • Dictionary or hash values: These are key-value pairs, like a telephone directory in which one primary value (name) is attached with both phone numbers and addresses.

An example on data types is as follows:

As we can see in the preceding example, we declared the variables with various values, and based upon the value, Python automatically interprets the specific data type. If we just type the variable name again, it prints out the value stored in the variable based upon its data type.

Similarly, the following example specifies other native data types:

Additionally, to see the data type we can use the type() function, which returns the type of a variable based upon the value we gave. The variable is passed as an argument to the type() function to get the data type value:

A PowerShell example of the same Python code is as follows:

#PowerShell code
$value=5
$value="hello"
write-host $value
write-host $value.gettype()
#This is remark
#A variable in powershell is declared with '$' sign in front.
# The gettype() function in powershell is used to get the type of variable.

There are operations, such as addition (+), for specific variables with particular data types. We have to be sure what types of variable we are adding. If we have an incompatible data type variable being added to another one, Python would throw an error stating the reason.

Here in the following code, we see the result of adding two string variables:

Similarly, observe the difference if we use the same addition on integer variables:

As mentioned, let's see what happens when we try to add a string and an integer variable together:

The error clearly specifies that we cannot add two different data types because the interpreter cannot recognize which data type needs to be assigned to the mixed value variable.

Sometimes, if necessary, we can convert the values from one data type to another by calling specific functions that convert the data type to another. For example, int("1") will convert the string value 1 to integer value 1, or str(1) will convert the integer value 1 to the string value 1.

We will be extensively using the various data types depending upon the logic and requirements of the scripts, and also, if necessary, converting one data type to another to achieve certain results.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.205.183