Learn how to get a list of numbers using the range() and list() functions. Given two numbers, the range function creates a range instance between those values that holds all the numbers between the first input and the second input. By putting this range instance into the list function, we can create a list that contains each value in the inputted range.
- [Voiceover] Let's learn how to use the range function. The range function takes one whole number as input, and so here it's "x", and it's going to output a range instance that holds all the numbers counting by one between zero and the only input. Say "x" had the value "15", then range(x) would output the value, which is a range that holds all the numbers counting by one between zero and 14, because that's 15 numbers counting from zero all the way to 14. And there are other optional parameters that we're going to learn about later.
Basically you can set a starting point for the range and you can also change how much it counts by so it could count by one, by three, by five, whatever you would like. So here we'll create a variable called "numberedcontestants" and it's going to have the value of range(30). Range(30) will return a range instance that includes all the numbers from zero to 29, because zero to 29 would be 30 numbers. Now to see that this range instance is actually being created, we have to learn about another function called "list." The list function takes one input, a tuple, and outputs a list containing the same data as a tuple.
So this function basically kind of rearranges our data, and it's how we're going to be able to list out the numbers in our range instance. So here, we can do something like print(list(numberedContestants)) because numberedContestants has our range instance value, and it's going to list out all of these numbers, because these are numberedContestants, and so we have zero, one, two, all the way across, all the way to 29 because zero to 29 is 30 values. And so each of our contestants have a number.
Furthermore, we could use a for the open save, for the contestant, or c, in list(numberedContestants), we're going to print the "Contestant" with their contestant number so here converting that contestant number to a string "is here." So this is a very easy way to just get a string of numbers super quickly. Running it here, we get all of these contestant numbers just generated for us. Very easy.
Going back to our range function, we can add some optional parameters in order to format our range instance in a different way. Here, we can create a variable called "luckyWinners" and have it be a range that goes from seven to 30 and counts by five. And so here our range instance is going to go from seven and then to 29, because we're not going to include 30, and we're going to count by five for each number. So here we were counting by ones, now we're going to count by fives.
So now we can go ahead and use the list function to enumerate all of these out, and we'll print it out as well. And so we'll list our luckyWinners here, and there we go. So now we have it starting and including seven, and then counting by five until we reach that endpoint of 30 and then once we hit 30, we just do not list any more. Range and list are super useful if you need to create an ordered list of numbers quickly.
- 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