Suppose we have a list of numbers and we want to print them out only if they are even. Another option is to simply Google ‘Python + [command or function]’. The easiest way to do this is using both for loops and an if-else statement. As the for loop is executed, Python goes through each element in this list. Try some of these courses below to get started: Get a subscription to a library of online courses and digital learning tools for your organization with Udemy for Business. for x in range(1,5): for y in range(1,5): print(x*y) Python’s zip() function creates an iterator that will aggregate elements from two or more iterables. As we mentioned earlier, the Python for loop is an iterator based for loop. In this case, we’ll have to use nested for loops. As an example, suppose you have to go grocery shopping. An iterable object is returned by open() function while opening a file. In other words, we don’t have to worry about knowing how many items we have before we create our list. Just type in the following at the command prompt: Q. As with any programming exercise, it helps to list down the basic requirements before writing any code. If, however, you were to remove the ‘i = i + 1’ part, the while condition will never be fulfilled, i.e. Following code fails: One-Liner #1: To input space separated integers in a list: However, we should avoid writing very long list comprehensions in one line … For those of us who work in languages like Java or C, we’re us… and perform the same action for each entry. In later examples, we’ll use for loops and if-else statements to create useful, complicated programs. We nested loop is basically a loop within a loop. sed command to replace $Date$ with $Date: 2021-01-06. It is a smart and concise way of creating lists by iterating over an iterable object. This is called an infinite loop. Here are some homework questions you can work on: Hint: Look up the isalpha() function in Python. We used int() to make sure that the entered input is a number. Making statements based on opinion; back them up with references or personal experience. This is a great example that utilizes nested loops, if-else statements and the break command. Run this program with "Sea Rose" as input: $ python word_count.py