Operators

With every programming language we have our operators, and Python is no different. Operators in a programming language are used to vaguely describe 5 different areas: arithmetic, assignment, increment/decrement, comparison, logical. There isn’t really much need to explain all of them as I have previously in the other categories, which means we will only cover a few of them.

Arithmetic Operators

Example
print (3 + 4)
print (3 - 4)
print (3 * 4)
print (3 / 4)
print (3 % 2)
print (3 ** 4) # 3 to the fourth power
print (3 // 4) #floor division
Result 7
-1
12
0.75
1
81
0

See, they are pretty standard. I included how to do exponents because it’s a little funky, but other than that, they are fairly normal and work as expected. Don’t forget that you can use the + sign to concatenate strings. The addition, subtraction, multiplication, and division work just like expected. You might have not seen the modulus operator before (%). All the modulus operator does is to divide the left side by the right side and get the remainder. So, that remainder is what is returned and not the number of times the right number went into the left number The double * is just an easy way to provide exponents to Python. Finally, the floor division operator (//) just divides the number and rounds down.

Assignment Operators

These are pretty identical to the previous languages also, but a refresher never hurt anyone. Example please!

Example
a = 0
a+=2
print (a)
Result 2

Of course, you can do this with any of the previous arithmetic operators as an assignment operator followed by the = We just told Python to add 2 to the value of a without having to say something like a = a + 2. We are programmers, and we are proud to be called lazy!

Functions

Functions in Python are super useful in separating your code, but they don’t stop there. Any code that you think you will ever use anywhere else, you should probably put in a function. You can also add arguments to your functions to have more flexible functions, but we will get to that in a little bit. You can define a function by using the keyword def before your function name. Let’s use our first Python function.

Example
def someFunction():
    print("boo")
someFunction()
Result boo

It is necessary to define functions before they are called. Even though we have to skip over the function while reading to see the first statement, someFunction(). This throws us back up into the def someFunction():, again with a colon following it. Then after we acknowledge that the function is being called, we create a new line with four spaces for our simple print statement.

Functions with Arguments

Simple functions like the one above are great and can be used quite often. However, there usually comes a time where we want to have the function act on data that the user inputs. We can do this with arguments inside of the () the follows the function name.

Example
def someFunction(a, b):
    print(a+b)
someFunction(12,451)
Result 463

Using the statement someFunction(12,451), we pass in 12, which becomes a in our function, and 451, which becomes b. Then, we just have a little print statement that adds them and prints them out.

Function Scope

Python does support global variables without you having to explicitly express that they are global variables. It’s much easier just to show rather than explain:

Example
def someFunction():
    a = 10
someFunction()
print (a)

This will cause an error because our variable, a, is in the local scope of someFunction. So, when we try to print a, Python will bite and say a isn’t defined. Technically, it is defined, but just not in the global scope. Now, let’s look at an example that works.

Example
a = 10 
def someFunction():
    print (a)
someFunction()

In this example, we defined a in the global scope. This means that we can call it or edit it from anywhere, including inside functions. However, you cannot declare a variable inside a function, local scope, to be used outside in a global scope.

For Loop

It’s time for an awesome part of Python. Python’s for loops are pretty amazing compared to some other languages because of how versatile and simple they are. The idea of a for loop is rather simple, you will just loop through some code for a certain number of times. I won’t get a chance to show you the flexibility of the for loops until we get into lists, but sure enough its time will come. Onto an example:

Example
for a in range(1,3):
    print (a)
Result 1
2

First, print is indented for a reason. Remember that Python is picky about spacing. It is somewhat complicated to understand exactly what a is in the example. In this instance, a is a variable the increments itself every time we run through the loop. Next, we use the range keyword to set the starting point and the point right after we would finish. This is precisely why the number 3 didn’t print. Python is quite fond of this idea of all the way up to a number, but not including it.

Also, notice the in keyword. This is actually part of the for loop and you will understand it a bit better after we deal with lists and dictionaries. So, basically the above for loop says, “For variable a, which will be incremented at the end of every loop, in the range of 1 up to 3.”

While Loop

While loops in Python can be extremely similar to the for loop if you really wanted them to be. Essentially, they both loop through for a given number of times, but a while loop can be more vague (I’ll discuss this a little bit later). Generally, in a while loop you will have a conditional followed by some statements and then increment the variable in the condition. Let’s take a peek at a while loop really quick:

Example
a = 1
while a < 10:
    print (a)
    a+=1
Result 1
2
3
4
5
6
7
8
9

Fairly straightforward here, we have our conditional of a < 10 and a was previously declared and set equal to 1. So, our first item printed out was 1, which makes sense. Next, we increment a and ran the loop again. Of course, once a becomes equal to 10, we will no longer run through the loop.

The awesome part of a while loop is the fact that you can set it to a condition that is always satisfied like 1==1, which means the code will run forever! Why is that so cool? It’s awesome because you create listeners or even games. Be warned though, you are creating an infinite loop, which will make any normal programmer very nervous.

Where’s the do-while loop

Simple answer, it isn’t in Python. You need to consider this before you are writing your loops. Not that a do-while loop is commonly used anyway, but Python doesn’t have any support for it currently.

Lists

We don’t have arrays; we have Python lists. Lists are super dynamic because they allow you to store more than one “variable” inside of them. Lists have methods that allow you to manipulate the values inside them. There is actually quite a bit to show you here so let’s get to it.

Example
sampleList = [1,2,3,4,5,6,7,8]
print (sampleList[1])
Result 2

The brackets are just an indication for the index number. Like most programming languages, Python’s index starting from 0. So, in this example 1 is the second number in the list. Of course, this is a list of numbers, but you could also do a list of strings, or even mix and match if you really wanted to (not the best idea though). Alright, now let’s see if we can print out the whole list.

Example
sampleList = [1,2,3,4,5,6,7,8]
for a in sampleList:
    print (a)
Result 1
2
3
4
5
6
7
8

I told you we would come back to see how awesome the for loop is. Basically, variable a is the actual element in the list. We are incrementing an implicit index. Don’t get too worried about it. Just remember we are cycling through the list.

Common List Methods

There are number of methods for lists, but we will at least cover how to add and delete items from them. All of the list methods can be found on Python’s documentation website. Methods follow the list name. In the statement listName.append(2), append() is the method.

  • .append(value) – appends element to end of the list
  • .count(‘x’) – counts the number of occurrences of ‘x’ in the list
  • .index(‘x’) – returns the index of ‘x’ in the list
  • .insert(‘y’,’x’) – inserts ‘x’ at location ‘y’
  • .pop() – returns last element then removes it from the list
  • .remove(‘x’) – finds and removes first ‘x’ from list
  • .reverse() – reverses the elements in the list
  • .sort() – sorts the list alphabetically in ascending order, or numerical in ascending order

Try playing around with a few of the methods to get a feel for lists. They are fairly straightforward, but they are very crucial to understanding how to harness the power of Python.