by BehindJava

What are useful operators in Python with Syntax and Examples

Home » python » What are useful operators in Python with Syntax and Examples

In this tutorial we are going to learn about useful operators in Python with Syntax and Examples.

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 tutorial,

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:

range(0,11)

range(0, 11)

Note: 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!

# Notice how 11 is not included, up to but not including 11, just like slice notation!
list(range(0,11))
output-
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
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.

list(range(0,11,2))
output-
[0, 2, 4, 6, 8, 10]

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 indexcount or loopcount 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().

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
zip(mylist1,mylist2)
output-
<zip at 0x1d205086f08>

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))
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.

'x' in ['x','y','z']

True

'x' in [1,2,3]

False

not in

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

'x' not in ['x','y','z']

False

'x' not in [1,2,3]

True

min and max

Quickly check the minimum or maximum of a list with these functions.

mylist = [10,20,30,40,100]

min(mylist)

10

max(mylist)

100

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.

  1. from random import shuffle

    # This shuffles the list "in-place" meaning it won't return
    # anything, instead it will effect the list passed
    shuffle(mylist)
    mylist
    [40, 10, 100, 30, 20]
  2. from random import randint

    # Return random integer in range [a, b], including both end points.
    randint(0,100)
    25
    # Return random integer in range [a, b], including both end points.
    randint(0,100)
    91

    input

    input('Enter Something into this box: ')
    Enter Something into this box: great job!

    Output:

    'great job!'