Python for Beginners [Smooth Operators]

Python for Beginners [Smooth Operators]

Introduction

Operators are in every programming language (at least every language that I have used), and I don't think you can get by without using them.

What are operators?

Operators are used to execute certain mathematical operations, assign values to variables (*like we saw in the last beginner tutorial on strings here), and check equality.

Arithmetic

The following (Addition, Subtraction, Multiplication, Division, and Exponents) will be easy to grasp. You folks learned all this in the 4th grade; I will not re-hash it too much. If you, by chance, don't know what these are, seriously, take an introductory math class.

Addition

Subtraction

Multiplication

Exponents

Division

This division might wind up to be a little tricky at first. In regular math, you can use divide with this operator '/' and this '%,' not in Python. Doing this will turn around and bite you. You can only use the forward slash, as the percentage sign is called a modulus, and it is used for the remainder.

Modulus

As stated above, modulus will get you the remainder of your division.

Floor Division

In my opinion, floor division is pretty neat (not that modulus isn't). Floor division will give you how many times a number will 'go into' another number during the division operation. Here are some examples.

Assignment

Assignment operators are a lot like the above because there is a mathematical operation taking place, but there is also assignment included. After the operation takes place, it gets assigned to the variable. The '=' operator is what assigns a string, a formula, or a number to a variable. Easy, we have done that before, right? Well, here are some others.

Membership

Membership operators are very helpful if you are looking through data. I have used these handy little things when looking through server logs. Let's look at some fun examples.

We can also look for 'not in':

Now isn't that handy-dandy?

Comparison

These operators essentially look at one number, variable, or string and compare it with the other number, variable, or string defined. A == will look to see if x is the same as y, and if it is will return a bool of True. If x does not equal y, it will return False.


The 'not equal' operator, this example is illustrated below.

There are greater than, less than, greater than or equal to, and less than or equal to left. I will wrap these up in a single example. We will assign x to a value of 5 at the top, and work all examples off of that variable.

Other operators exist that I am not going over, such as conditional and identity. I will go over those when I go through the 'Conditionals' portion of the series.

Conclusion

Operators are part of the backbone of any language, and you will need to understand and retain information on them. Don't beat yourself down if you have to Google some here and there, and ALL developers do this. If a developer says they don't, they are a Lying McLiar Pants.

As always, if you want to learn more, please go to the Python docs to check out all the goodies that Python has to offer.