In this video, Kathryn teaches you how to use the built-in len() function to calculate a given input's length.
- [Presenter] Let's figure out how to use the len function. Len takes one input, a list, and it outputs a whole number representing the length of that list. Lists can take many different formats. Let's consider strings to start. So say we have a variable called first name, and we'll make the first name be Taylor, have that value. And then we'll go ahead and use the len function, and we'll do a print statement, and we'll say len first name. And this will give us the length of the first name. And for strings, length means the number of characters.
Here we get six, because Taylor has six characters in it. We can also use this for the last name, and so we'll say last name equals Swift. Then we'll go print len, again printing out the length of the last name here. And Swift has five characters, and that's why we get five characters in the output. And so why does this work for strings? Well strings have a special property. We go first name dot len. We see that it has the length property, and so because it has the length property, we can use the len built in function.
Now since this is a built in function, we can use different types of data as input. And so let's try it for official lists in Python. Create another variable called ages, and we'll just have some ages in here. 12, 10, just some random ages there. And we can print up the length of this list. We could go print len ages, and it will print out how many items are in the list. So in this case it's five items. Now why would this be helpful? Well let's create another variable, and we'll say i equals zero, and now we can actually iterate through this list using a range, and so we could say four i in range zero len ages.
We can go ahead and do something. And so in this case, let's go ahead and print out the ages at index i. What's happening here is we have this variable called i, and it's going to change it's value to zero, one, two, three, four, until we hit the end of the length of ages. And for each data point, it's going to go ahead and print it out here. Accessing ages, and then the appropriate index. So if we go ahead and run this, it prints out each individual age.
We have zero, 11, 43, all of these ages. We can also do this for lists that do not contain numbers. And so say we want to print out the length of a list that has Bob, Mary, and Sam. We could do this. It has three items, and we get three. And notice it doesn't really take into account how many letters are in each input. You would have to call len on each individual input if you wanted that, but it gives us the length of the entire data structure here which is a list that has three items.
And we can also use len on a dictionary. So it isn't just for strings and lists, it's really for any iterable object. And so here we could have a candy collection. And we could say Bob has ten candies. We could say Mary has seven candies. And we could say Sam has 18 candies. And we could go ahead and print out the length of the candy collection. And when we print this out, we will also get three because there's three items in our dictionary.
And then based on the fact that there's three items, we could go ahead and access each item appropriately. Knowing how to get the length of a string list and dictionary will be very helpful in your later projects.
- Working with logical and comparison operators
- Getting a list of numbers with the range() and list() functions
- Using mathematical functions such as round(), abs(), and pow()
- Calculating a given input's length
- Importing and using the math module
- Reading a user's command-line arguments
- Getting the current time
- Formatting dates and times with datetime
- Creating a timer
- Using urllib to get content from the Internet
- Using the JSON module to decode content