Use of loop in list is highly efficient and do wonders with large lists. So far we have used lists with string elements but we may required to store numbers in list. For example in a game we might want
to keep track of a player’s high scores as well. In data visualizations, we always work with sets of numbers, such as temperatures, distances, population sizes, or latitude and longitude values, among other types of numerical sets.
Lists are ideal for storing sets of numbers, and Python provides a number of tools to help us work efficiently with lists of numbers. With these tools our code works well even when our lists contain millions of items.
The range() function
This function generate a series of numbers. So we may use this function to print a series of numbers between 1 to 10 as shown below-
for num in range(1,10):
print (num)
The screenshot below shows the program and it's output-
As seen in the output range() prints only the numbers 1 through 9. This is another result of the off-by-one behavior you’ll see often in programming languages. The range() function causes Python to start counting at the first value you give it, and it stops when it reaches the second value you provide.
Because it stops at that second value, the output never contains the end value, which would have been 10 in this case. Thus to print numbers from 1 to 10 use range (1,11)
We can use the output from range() function to make a list using the list() function. If we use use the range function from our previous example we can make a list that stores values from 1 to 9.
my_list = list(range(1,10))
The screenshot below shows the program and it's output-
The list() converts that set of numbers obtained from range() into a list. The range() can also be used to tell Python to skip numbers in a given range. For example, here’s how we would list the even numbers between 1 and 10:
my_list = list(range(2,11,2))
The screenshot below shows the program and it's output-
The range() function starts with the value 2 and then adds 2 to that value. It adds 2 repeatedly until it reaches or passes the end value, 11, and produces the result shown in the above screen shot.
Any guesses what would be the output from the following line-
my_list = list(range(1,11,2))
Using the range() function we can create almost any set of numbers we want to, for example lets create a list which contains cubes of first five numbers.
cubes =[]
for num in range (1,6):
cube = num**2
cubes.append(cube)
print(cubes)
The screenshot below shows the program and it's output-
First we created an empty list called cubes. Next we tell Python to loop through each value from 1 to 5 using the range() function. Inside the loop, the current value is raised to the third power and stored in the variable cube and each value of cube is appended to the list cubes. Once the loop has finished running the list of cubes is printed as shown in the above screenshot.
A more concisely written code for the above program is shown below-
cubes =[]
for num in range (1,6):
cubes.append(num**3)
print(cubes)
Here we omit the temporary variable cube and append each new value directly to the list cubes making our code more concise.
Further a list comprehension allows you to generate this same list in just one line of code. A list comprehension combines the for loop and the creation of new elements into one line, and automatically appends each new element.
The screenshot below shows the program and it's output-
To use this syntax, begin with a descriptive name for the list, such as cubes. Next, open a set of square brackets and define the expression for the values you want to store in the new list. In this example the expression is value**3, which raises the value to the third power. Then, write a for loop to generate the numbers you want to feed into the expression, and close the square brackets. The for loop in this example is for value in range(1,6), which feeds the values 1 through 5 into the expression value**3. Notice that no colon is used at the end of the for statement.
List comprehensions are not always presented to beginners, as it takes practice to write your own list comprehensions, but you’ll find them worthwhile once you become comfortable creating ordinary lists.
When you’re writing three or four lines of code to generate lists and it begins to feel repetitive, consider writing your own list comprehensions. Make some lists and use the range() to get maximum, minimum numbers, sum of all the numbers in the list. Try to use list comprehensions whereever possible. Keep exploring Python and remember Python is easy to learn!
to keep track of a player’s high scores as well. In data visualizations, we always work with sets of numbers, such as temperatures, distances, population sizes, or latitude and longitude values, among other types of numerical sets.
Lists are ideal for storing sets of numbers, and Python provides a number of tools to help us work efficiently with lists of numbers. With these tools our code works well even when our lists contain millions of items.
The range() function
This function generate a series of numbers. So we may use this function to print a series of numbers between 1 to 10 as shown below-
for num in range(1,10):
print (num)
The screenshot below shows the program and it's output-
As seen in the output range() prints only the numbers 1 through 9. This is another result of the off-by-one behavior you’ll see often in programming languages. The range() function causes Python to start counting at the first value you give it, and it stops when it reaches the second value you provide.
Because it stops at that second value, the output never contains the end value, which would have been 10 in this case. Thus to print numbers from 1 to 10 use range (1,11)
We can use the output from range() function to make a list using the list() function. If we use use the range function from our previous example we can make a list that stores values from 1 to 9.
my_list = list(range(1,10))
The screenshot below shows the program and it's output-
The list() converts that set of numbers obtained from range() into a list. The range() can also be used to tell Python to skip numbers in a given range. For example, here’s how we would list the even numbers between 1 and 10:
my_list = list(range(2,11,2))
The screenshot below shows the program and it's output-
The range() function starts with the value 2 and then adds 2 to that value. It adds 2 repeatedly until it reaches or passes the end value, 11, and produces the result shown in the above screen shot.
Any guesses what would be the output from the following line-
my_list = list(range(1,11,2))
Using the range() function we can create almost any set of numbers we want to, for example lets create a list which contains cubes of first five numbers.
cubes =[]
for num in range (1,6):
cube = num**2
cubes.append(cube)
print(cubes)
The screenshot below shows the program and it's output-
First we created an empty list called cubes. Next we tell Python to loop through each value from 1 to 5 using the range() function. Inside the loop, the current value is raised to the third power and stored in the variable cube and each value of cube is appended to the list cubes. Once the loop has finished running the list of cubes is printed as shown in the above screenshot.
A more concisely written code for the above program is shown below-
cubes =[]
for num in range (1,6):
cubes.append(num**3)
print(cubes)
Here we omit the temporary variable cube and append each new value directly to the list cubes making our code more concise.
Further a list comprehension allows you to generate this same list in just one line of code. A list comprehension combines the for loop and the creation of new elements into one line, and automatically appends each new element.
The screenshot below shows the program and it's output-
To use this syntax, begin with a descriptive name for the list, such as cubes. Next, open a set of square brackets and define the expression for the values you want to store in the new list. In this example the expression is value**3, which raises the value to the third power. Then, write a for loop to generate the numbers you want to feed into the expression, and close the square brackets. The for loop in this example is for value in range(1,6), which feeds the values 1 through 5 into the expression value**3. Notice that no colon is used at the end of the for statement.
List comprehensions are not always presented to beginners, as it takes practice to write your own list comprehensions, but you’ll find them worthwhile once you become comfortable creating ordinary lists.
When you’re writing three or four lines of code to generate lists and it begins to feel repetitive, consider writing your own list comprehensions. Make some lists and use the range() to get maximum, minimum numbers, sum of all the numbers in the list. Try to use list comprehensions whereever possible. Keep exploring Python and remember Python is easy to learn!
0 comments:
Post a Comment