Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/02-Python Statements/05-Useful-Operators.ipynb
Views: 648
Useful Operators
There are a few built-in functions and "operators" in Python that don't fit well into any category, so we will go over them in this lecture, let's begin!
range
The range function allows you to quickly generate a list of integers, this comes in handy a lot, so take note of how to use it! There are 3 parameters you can pass, a start, a stop, and a step size. Let's see some examples:
Note that this is a generator function, so to actually get a list out of it, we need to cast it to a list with list(). What is a generator? Its a special type of function that will generate information and not need to save it to memory. We haven't talked about functions or generators yet, so just keep this in your notes for now, we will discuss this in much more detail in later on in your training!
enumerate
enumerate is a very useful function to use with for loops. Let's imagine the following situation:
Keeping track of how many loops you've gone through is so common, that enumerate was created so you don't need to worry about creating and updating this index_count or loop_count variable
zip
Notice the format enumerate actually returns, let's take a look by transforming it to a list()
It was a list of tuples, meaning we could use tuple unpacking during our for loop. This data structure is actually very common in Python , especially when working with outside libraries. You can use the zip() function to quickly create a list of tuples by "zipping" up together two lists.
To use the generator, we could just use a for loop
in operator
We've already seen the in keyword during the for loop, but we can also use it to quickly check if an object is in a list
not in
We can combine in with a not operator, to check if some object or variable is not present in a list.
min and max
Quickly check the minimum or maximum of a list with these functions.
random
Python comes with a built in random library. There are a lot of functions included in this random library, so we will only show you two useful functions for now.