This post is the second part of a series on the Python Basics. You can find Part One here. In this series, we’ll teach you about the very basics of the Python programming language. In Part 2, you’ll be learning about defining variables, printing a new line, mixing math and strings, handling single and double quotes, and slicing strings in Python.
If you want to use the Nexus as your Python editor, then you will find instructions at the end of the blog on how to set this up!
Tera Tom here! In our last post, we talked about the popular data types, Arithmetic Operators, and printing and commenting in Python. Now it’s time to kick things up a notch! Let’s dive into defining variables inside your python script!
If you have a Python editor you want to use then you are all ready to go, but if you want an easy way to try these examples then go to www.CoffingDW.com and download the Nexus. Once you have it installed, just look at the picture below and see where the Python editor of Nexus exists.
Defining a Variable
A variable is a place to store values. Below, we have two examples of creating, defining, and printing a variable. Notice in the first example that our variable is called “name” (lowercase) and we set the value to “name” = “Tera-Tom”. Notice we used the print command and the same lowercase “name”. In the second example, we defined the variable named “UPPERName” (a mixture of upper- and lower-case letters), and we set the value to “UPPERName” as “TERA-Tom”. We are then using the print command to print(UPPERName), always using the exact same case.
Printing a Newline
You can place a newline in your answers by using the print(‘\n’) command. You can also use the / (backslash) as an escape operator for quotes.
Math * Strings Actually Works
What is 2 multiplied by 2? The answer is 4, of course. But what’s is 10 multiplied by t? The answer is tttttttttt (10 t’s). This technique is often used to place a certain amount of spaces before a string. Below, we show multiple examples of multiplying a number by a string. Notice how nice this technique is to place a certain amount of spaces before a string!
Mixing Single and Double Quotes
Python is confused by both of the statements below because of the mixing of single and double quotes. When you begin a string with a double quote (“) the next encountered double quote ends the string. When you begin a string with a single quote then the next single quote encountered ends the string. In both cases, Python got confused about where the string ends and responded with an error. The next page will show you how to fix these issues.
Two Ways to Handle Mixing Single and Double Quotes
There are two ways to handle a mixture of single and double quotes. The easiest way is to put three single quotes at the beginning and end of the string. This is what we are doing in the first example below. The second technique involves a backslash. A backslash preceding any single or double quote is an escape character. This escape character tells Python that the quote is not the ending quote, but instead a literal quote inside the string. These escaping backslashes do not print inside the answer set.
Manipulating strings is easy with Python. Check out the examples below. The colon inside the brackets represents slicing. Slicing with a negative number means start at the end and go backward. Slicing with a positive number indicates the starting position and to go up to the nth position, but not include the nth position. Remember, the first position starts with 0.
How to Install and Setup Python on your Nexus
- Download the latest version of Nexus from our website. https://www.coffingdw.com/nexus-trial/
- Once installed download Python for Windows. https://www.python.org/downloads/windows/
Your version number can vary, but you will want to download the Windows installers. So for Python for Windows 3.7.3 located here (https://www.python.org/downloads/release/python-373/) you will select one of these highlighted links:
Note: Nexus comes in three versions: Nexus 64-bit, Nexus 32-bit, and Nexus WOW-64 (For 64-bit machines with 32-bit Microsoft Office installed).X86-64 can be used for the 64-bit or Wow64 version of Nexus and x86 can be used only on the 32-bit version of Nexus.
When installing, if you have the option, select “Add Python to PATH”.
If you are using an older version of Python and do not have this option, then you will need to add the path to your python executable to your PATH environment variable. Be careful when editing this variable as it can affect many Windows functionality issues. If you don’t feel comfortable about this then consult with your desktop support.
Python 3.7.3 was installed in the following directory on our PC.
You can view your Python directory by opening File Explorer and pasting in the following path:
Once you have the path to the python.exe executable then add it to your Environment PATH variable by following the direction in this link:
And that is how you install Python into the Nexus Query Chameleon!