Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Operators and Built-In Functions

In this lesson, you’ll see how operators and Python’s built-in functions can be applied to lists. Several Python operators can be used with lists. in and not in are membership operators and can be used with lists. A membership operator used on a list:

  • Returns True if the first operand is contained within the second
  • Returns False otherwise
  • Can also be used as not in

Here’s an example:

Python
>>> a = ['spam', 'egg', 'bacon', 'tomato', 'ham', 'lobster']
>>> a
['spam', 'egg', 'bacon', 'tomato', 'ham', 'lobster']

>>> 'spam' in a
True
>>> 'egg' in a
True
>>> 'kiwi' in a
False
>>> 'kiwi' not in a
True

Here’swhat happens with the concatenation (+) and replication (*) operators:

  • The concatenation (+) operator concatenates the operands.
  • The replication (*) operator creates multiple concatenated copies.

Here’s an example:

Python
>>> a 
['spam', 'egg', 'bacon', 'tomato', 'ham', 'lobster']

>>> a + ['kiwi', 'mango']
['spam', 'egg', 'bacon', 'tomato', 'ham', 'lobster', 'kiwi', 'mango']

>>> a
['spam', 'egg', 'bacon', 'tomato', 'ham', 'lobster']
>>> a * 2
['spam', 'egg', 'bacon', 'tomato', 'ham', 'lobster', 'spam', 'egg', 'bacon', 'tomato', 'ham', 'lobster']
>>> 2 * a
['spam', 'egg', 'bacon', 'tomato', 'ham', 'lobster', 'spam', 'egg', 'bacon', 'tomato', 'ham', 'lobster']

Several Python built-in functions can also be used with lists:

  • len() returns the length of the list.
  • min() returns the object from the list with the smallest value.
  • max() returns the object from the list with the highest value.

Here’s an example:

Python
>>> a = ['spam', 'egg', 'bacon', 'tomato', 'ham', 'lobster']
>>> len(a)
6
>>> b = [4, 99, 55, 23, 5, 6]
>>> b
[4, 99, 55, 23, 5, 6]
>>> len(b)
6
>>> min(b)
4
>>> max(b)
99

>>> min(a)
'bacon'
>>> max(a)
'tomato'

>>> c = ['apple', 'bacon', 'Asparagus', 'Zebra']
>>> c
['apple', 'bacon', 'Asparagus', 'Zebra']
>>> min(c)
'Asparagus'
>>> max(c)
'bacon'

>>> ord('A')
65
>>> ord('a')
97
>>> ord('z')
122
>>> ord('Z')
90
>>> max(c)
'bacon'

>>> d = [2, 3, 5, 8, 'apple', 'bacon', 'spam']
>>> len(d)
7
>>> min(d)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
    min(d)
TypeError: '<' not supported between instances of 'str' and 'int'

00:00 This video is about using operators with lists and some of the Python built-in functions that also work with lists. First up, it’s the in operator.

00:09 It’s a membership operator that can be used with lists. It’s going to return True if the first operand is contained within the second and False otherwise. Also it can be written as not in. I’ll have you try out the membership operator.

00:23 Make a list.

00:26 To check if a particular object is within your list, you’d write it like this. 'spam' in a—that’s True. 'egg' in a? Yep.

00:38 'kiwi' in a? No. 'kiwi' not in a, yep, that’s True. The next two are the concatenation operator, which is the plus sign (+), and the replication operator, which is the multiplication sign or the asterisk (*).

00:57 Both are fairly straightforward. For the concatenation operator, it takes the operands and glues them together in the order that they’re listed. And the replication operator will create multiple concatenated copies. For the concatenation operator, you start with a and you add items to it.

01:20 You see they get added to the list there. Now that’s just returning that. a has been unaffected unless you were to reassign it.

01:31 If you use the multiplication operator, the replication operator (*), you’d say a * 2. You see it repeat the list twice. Or even 2 * a, you’d get the same thing. Or you could do something larger, a * 99.

01:48 There are a few built-in Python functions that work with lists. len(), which is short for length, is going to return the length of the list—a count, if you will, of the objects within it.

01:59 min() is going to return the object from the list with the minimum value. And its reverse, max(), which will return the object from the list that has the maximum value.

02:10 len() returns the number of items in a container. In this case, the container being a. It says there are 6 items. Pretty straightforward.

02:17 Let’s create another list. Let’s make a list of integers. This will be called b.

02:27 Here’s b. The length of b is 6 again. In this case, min(b)—well, that would be 4. That’s the lowest number.

02:36 Okay. What about max(b)? That’d be 99 in this case. For numbers or integers, that is pretty straightforward. What happens when it’s strings?

02:44 If you pause for a moment and look at the min() function, it’s going to return the smallest item. But that’s a little confusing here with strings.

02:50 It’s going to return the object with the first letter that is the lowest value. Okay, so then max() would be what? Well, 'tomato'. Okay. What if your list was a little different, and it had a mix of characters?

03:07 So you have here 'apple', 'bacon', but then you have 'Asparagus', and for whatever reason, 'Zebra'. Okay. So here’s c. What’s the minimum of c? Should it be 'apple' or 'Asparagus'?

03:24 Well, 's' comes after 'p', so what’s going on here? It’s focusing initially on the first letter. In fact, max(c) is going to return 'bacon'.

03:33 Why wouldn’t it be the 'Z' from 'Zebra'? It has to do with something that I’ve covered in the Strings and Character Data tutorial.

03:41 The reason that the minimum value from c—and it’s looking at this first letter. The difference between uppercase letters and lowercase letters is something that’s unseen unless you’re familiar with this language ASCII.

03:57 But you can check the characters using another built-in Python function called ord() (ordinal), and it returns the Unicode code point for a one-character string.

04:07 So, what’s the difference between the capital 'A' and lowercase 'a'? Well, the lowercase values are actually higher numerically than the uppercase ones.

04:18 So uppercase actually comes first. Just be aware of that when you’re doing sorting or, in this case, finding a minimum value, is that it is going to grab the uppercase values if you’re using min() before the lowercase.

04:32 And just to show you, even a lowercase 'z' is higher than the uppercase. So it gets all the way to 'Z', which is 90, then it starts these lowercase letters here.

04:45 That’s why max(c) is 'bacon'. I’ll have you make one more just to show this example a little further. Let’s say you make another list and it’s mixed.

05:01 So len(d)? 7, 7 different objects. And the min(d)? Well, that’s going to raise an exception. You can’t use the operand less than (<) with instances between strings and integers.

05:18 In the next video, you’ll check out nesting within lists.

mnemonic6502 on Feb. 5, 2020

This should be the learning template all video tutorials should adopt on this site, great job!

techsukenik on Sept. 13, 2021

Good video on the basics. For the min and max, it would be useful to show a fix for comparing strings by using lambda functions. For example:

string_list = ['apple','bacon','Zebra']
print("Zebra is min ", min(string_list))
print('apple is min using lambda',
      min(string_list,key=lambda x: x.upper()))

Become a Member to join the conversation.