This post is the third part of a series on Advanced Python. In this series, we’ll take what we learned in our Understanding Python Basic series (Part One of that series found here) and expand on it to understand the more challenging parts of Python. In Part 4, you’ll be learning about Functions 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! Welcome to Part 4 of our lesson on Advanced Python! Before I begin explaining Functions in Python, let’s make sure you have everything you need to follow along with this lesson. 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 and more 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. If you want to install Python so you can practice with Nexus then go to the last page of this blog for detailed instructions.
What are Functions and Why Should They be Created?
We create functions to reduce code duplication and to modularize complex code, so our Python programs are readable.
Below, we created a function, but nothing prints in the definition until we call the function. A function call is a function invocation. Only when Python sees the function name and the parenthesis (colored below in blue) does it execute the function and the print takes place.
Creating Multiple Functions
Below, we created two functions, but nothing prints in the definition until we call the function. Notice that we only called one of the functions, and so only that function prints.
Trouble Shooting Function Indentations
After the function header (def), the remaining lines form the function body and they indent by a consistent amount. The exact indented amount doesn’t matter to the interpreter, though 2 or 4 spaces are common conventions. Below, we did not indent the second def of the function f, and the print happened in a different order than expected.
Passing Strings into Functions
The function named printme expects a string passed to it each time you call the printme. Below, we call it twice with two different string statements.
We have created a function named add_3_numbers. We then called the function with three different examples, and the results show as well. When you name a function, it must start with a letter. Tips –Return Multiple Values from Functions
Python does what most programming languages cannot because Python functions can return multiple values.
A Function that Passes by Reference vs. Value
All parameters (arguments) in Python are passed by reference. Therefore, if you change what a parameter refers to within a function, the change also reflects in the function you are calling.
All parameters (arguments) in Python are passed by reference. Therefore, if you change what a parameter refers to within a function, the change also reflects in the function you are calling. Below, The argument is being passed by reference and the reference is being overwritten inside the called function.
What is Lambda?
What is lambda good for, and why do we need it? We don’t need lambda because we can use def to build function objects, but there are two situations where lambda makes writing code easier. Those are when the functions are simple, and when the function will be of one-time use. With lambda, you don’t need to give the function a name. You can define it right there in the code where you want to use it.
You can utilize the lambda to imitate a print function. The lprint is not anything other than a variable. You can name it anything that is a valid name. The key is the lambda and *args verbiage listed below.
Tips –Using A Dictionary to Store A Switch
Python allows a dictionary to store expressions.
Tips – Calculate the Factorial of Any Number In One Line
Python allows the factorial of any number on a single line. In math, the factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n.
Turning Off Special Characters
If you use an ‘r’ in front of your quoted text, it makes quoted text raw. Raw means that \n no longer means newline, but instead means \n. Below, we have two examples. The first example does not use the r in front so when Python encounters the \n it thinks a newline is in order, but in the second example, the ‘r’ in front tells Python not to treat \n as a newline.
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!
Ready for more Python training? Make sure to check back on Monday for further lessons on Advanced Python! Or, if you’d prefer Tera-Tom to come teach at your organization, contact our team for booking information.