Python Important Operators

Updated: Aug 23, 2020



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 article, 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:


Example:


print(range(0,11))


Output:



range(0,11)



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


Example:



# Notice how 11 is not included, up to but not including 11, just like slice notation!
print(list(range(0,11)))

output:-




 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]



  
  print(list(range(0,12)))
  

Output:-




  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]




# Third parameter is step size!
# step size just means how big of a jump/leap/step you 
# take from the starting number to get to the next number.
print(list(range(0,11,2)))

output:-



  [0, 2, 4, 6, 8, 10]


 
print(list(range(0,101,10)))
   

Output:-




  [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]


enumerate


enumerate is a very useful function to use with for loops. Let's imagine the following situation:


index_count = 0

for letter in 'abcde':
    print("At index {} the letter is {}".format(index_count,letter))
    index_count += 1
    

Output:



At index 0 the letter is a
At index 1 the letter is b
At index 2 the letter is c
At index 3 the letter is d
At index 4 the letter is e



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


# Notice the tuple unpacking!

for i,letter in enumerate('abcde'):
    print("At index {} the letter is {}".format(i,letter))
    

Output:



At index 0 the letter is a
At index 1 the letter is b
At index 2 the letter is c
At index 3 the letter is d
At index 4 the letter is e



zip


Notice the format enumerate actually returns, let's take a look by transforming it to a list()


print(list(enumerate('abcde')))

Output:




[(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd'), (4, 'e')]



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.


mylist1=[1,2,3,4,5]
mylist2=['a','b','c','d','e']

# This one is also a generator! We will explain this later, but for now let's transform it to a list 
print(zip(mylist1,mylist2))

Output:


 
 <zip at 0x1d205086f98>
 



print(list(zip(mylist1,mylist2)))

Output:



  [(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e')]



To use the generator, we could just use a for loop



for item1, item2 in zip(mylist1,mylist2):
    print('For this tuple, first item was {} and second item was {}'.format(item1,item2))

Output:




For this tuple, first item was 1 and second item was a
For this tuple, first item was 2 and second item was b
For this tuple, first item was 3 and second item was c
For this tuple, first item was 4 and second item was d
For this tuple, first item was 5 and second item was e



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


print('x'in['x','y','z'])

Output:



True


 
print('x' in [1,2,3])
   

Output:



False



not in


We can combine in with a not operator, to check if some object or variable is not present in a list.


 print(