# Beginning Python Programming — Part 3

Beginning Python Programming — Part 3Familiarizing ourselves with using operators and noneBob RoeblingBlockedUnblockFollowFollowingMay 20Photo by Antoine Dautry on UnsplashIf you are just stumbling onto this series, be sure to check out part’s 1 and 2.

For the rest of you, today we will be talking about operators and none.

Beginning Python Programming Part 2 — Reference Types, Pointers, and Collection TypesPreviously we talked about variables, constants, and types.

medium.

comWhile we are still covering the basics, the good news is after today you will be able to write a program that will solve math problems, whether it’s from Kindergarten or advanced financial calculations using a formula.

Preliminary StuffYou’ll need to understand the concept of order of operations (PEMDAS).

If math class was too long ago for you here’s the order:Parenthesis (from inside to outside)Exponents from left to rightMultiplication from left to rightDivision from left to rightAddition from left to rightSubtraction from left to rightThat’s it!OperatorsAs long as you made it to 3rd grade, you’ve already seen most operators in programming.

Let’s take a look at the ones you already know:# Math Operators# Additionsum = 10 + 20 # 30# Subtractiondifference = 100 – 75 # 25# Multiplicationproduct = 20 * 5 # 100# Divisionquotient = 15 / 3 # 5# Floor Divisionquotient = 15 // 2 # 7, a single '/' results in 7.

5# Exponentresult = 3 ** 5 # 243# Comparison Operators# Greater thanresult = 3 > 4 # False# Less thanresult = 3 < 4 # True# Equal toresult = 3 == 4 # False# Greater than or equal toresult = 3 >= 4 # False# Less than or equal toresult = 3 <= 4 # True# Not equal toresult = 3 != 4 # True# Inverse (minus)result = -(-100) # 100result = -(100) # -100# Logical Operators# And – all elements must be True, not 0, or an empty string.

result = True and False # False# Or – any element must be True, not 0, or an empty string.

result = True or False # True# Notresult = not True # False# Is – Identity (points to the same object)result = 1 is True # True# In – Membership (resides in range, list, or collection)my_list = ['eggs', 'milk', 'cheese']result = 'milk' in my_list # TrueEasy stuff, right?.Now let’s look at one that you probably aren’t familiar with:# Remainder – also known as moduloresult = 15 % 10 # 5O.

K.

so that last one was weird.

It’s my favorite because it allows us to do all kinds of cool math that would normally cause problems.

For example, if we wanted to assign a number to every letter of the alphabet and we wanted the 28th letter (wait, what?).

Yes, the 28th letter.

Well, the 28th letter would be “B” assuming we looped back around to the beginning.

We can accomplish this with %.

We could write it out like value = 28 % 26.

value would then be equal to 2, which the second letter of the alphabet is “B”.

This is how the Caesar cipher was made; the Vigenère cipher was developed in the same way later on.

Operators are used frequently in programming.

It’s how programs know how to do stuff.

We can even use the addition operator for more than just math.

For example, we can use it to concatenate strings together.

first_name = "John"last_name = "Doe"full_name = first_name + " " + last_name # John DoeNotice that I used the variable name, while I could have just typed the following:full_name = "John" + " " + "Doe"It shows how we can use variables and constants to hold values, so we don’t have to keep typing them.

I’m sure you noticed that both first_name and last_name are longer than the value they hold.

I could change the example to have a last name of Supercalifragilisticexpialidocious, but let’s keep things simple.

Both Visual Studio Code and PyCharm have built-in autocomplete that is absolutely a lifesaver, but beyond that, we don’t always know what the value of first_name or last_name will be.

When we use the variable name in place, we can always be sure that we have the first name, followed by a space, followed by the last name.

A shorthand way of adding, subtracting, multiplying, and dividing is to append the operator to the assignment operator (you can still concatenate strings using this method using the addition operator):first_number = 1second_number = 2first_number += second_number # 3first_number *= second_number # 2first_number -= second_number # -1first_number /= second_numberfirst_number /= second_number # Guess the valuename = "John"last_name = "Doe"name += " " + last_name # John DoeHopefully, you’ve got that down by now and maybe have tried exploring on your own.

Practicing is always good.

Photo by Mike Tinnion on UnsplashNoneNone is special.

It means doesn’t exist.

If you are coming from other languages it is synonymous with null or nil.

None doesn’t mean 0, it means without value.

To illustrate this, let’s consider you have a notebook sitting on a desk.

This notebook can have any number of pages in it.

Let us assume it has 200 pages in it.

As you make notes, you tear pages out of the notebook.

When you run out of pages, the notebook is all that remains.

Since we don’t have any pages in it, we throw the notebook away.

While the notebook could be representative of a page_count variable, holding the number of pages that currently reside in the notebook.

When the notebook hit 0 pages, it was no longer useful to us, so we removed the notebook, in Python, it looks like:del page_countAll we have now is an empty desk which represents our program.

We can ask the following to determine if the notebook (page_count) still exists:notebook_status = page_count is not None # FalseWe could have checked if page_count is None , but that wouldn’t have made sense for a notebook_status.

By adding not, we make the world right by asking if page_count exists.

Now I want to add something important about None using an example:page_count = None# is NOT the same asdel page_countWhy?.None is a type named NoneType.

It’s about like assigning null or nil to a variable, except we will still hold a reference to the value so garbage collection will not free up the memory.

While this could be an improvement to keep your code fast by keeping the reference to memory around, if you want to allow garbage collection to free this memory, you must use del as this method also removes the reference to the variable.

SummaryWhew, that’s a lot for today!.Take a deep breath.

You made it through another part.

Hopefully, you learned a little about some of the operators Python has to offer.

We also learned about None type, why we might use this type and an alternative method when you want to free memory instead.

What’s NextIn the next post, we will cover decision making and loops.

This is where things start coming together, and we get to have a little bit of fun.

Don’t forget to practice in between the lessons.

In programming, practicing goes a long way.