Python for loops are for iterating through sequences like lists, strings, dictionaries or ranges. In this tutorial you can understand everything.
A comprehensive introductory tutorial to Python loops. Learn and practice while and for loops, nested loops, the break and continue keywords, the range function and more!
Loops are important in Python or in any other programming language as they help you to execute a block of code repeatedly. You will often come face to face with situations where you would need to use a piece of code over and over but you don't want to write the same line of code multiple times.
In this Python loops tutorial you will cover the following topics :
The while loop is one of the first loops that you'll probably encounter when you're starting to learn how to program. It is arguably also one of the most intuitive ones to understand: if you think of the name of this loop, you will quickly understand that the word "while" has got to do something with "interval" or a "period of time". As you already know by now, the word "loop" refers to a piece of code that you execute repeatedly.
With all of this in mind, you can easily understand the following definition of the while loop:
A while loop is a programming concept that, when it's implemented, executes a piece of code over and over again while a given condition still holds true.
The above definition also highlights the three components that you need to construct the while loop in Python:
That's all it takes!
How To Make A While Loop in Python
Now that you know what you need to construct a while loop, all that is left to do now is to look at a real-life example where the while loop is used before you start making exercises on your own! Consider the following example:
# Take user input number = 2 # Condition of the while loop while number < 5 : print("Thank you") # Increment the value of the variable "number by 1" number = number+1
Thank you Thank you Thank you
The code example above is a very simple while loop: if you think about it, the three components about which you read before are all present: the
while keyword, followed by a condition that translates to either
number < 5) and a block of code that you want to execute repeatedly:
print("Thank you") number = number + 1
If you go into detail in the above code, you see that there is a variable
number in which you store an integer
2. Since the value in
number is smaller than 5, you print out
"Thank you" and increase the value of
number with one. While the value in
number stays smaller than 5, you continue to execute the two lines of code that are contained within the while loop:
"Thank you" "Thank you"
You print out "Thank you" two more times before the value of
number is equal to
5 and the condition doesn't evaluate to
True any more. Because the condition now evaluates to
False, you will exit the while loop and continue your program if it contains any more code. In this case, there isn't any more code so your program will stop.
The above example is a bit basic, you can also include conditionals, or, in other words, an if condition, to make it even more customized. Take a look at the following example:
# Take user input number = 2 # Condition of the while loop while number < 5 : # Find the mod of 2 if number%2 == 0: print("The number "+str(number)+" is even") else: print("The number "+str(number)+" is odd") # Increment `number` by 1 number = number+1
The number 2 is even The number 3 is odd The number 4 is even
Now, let's practice!
Write a function
collatz() which lets the user input an integer in a variable named
number. If number is even, then it should print the result of
number/2. If number is odd, then
collatz() should print and return
3 * number + 1. The program should keep calling the function
collatz() on the
number until it returns a
Note that this is actually an implementation of collatz sequence, which in short is a mathematical problem where you choose a number and you keep doing the above calculations until you arrive at a result of 1.
Below, you can find the solution:
def collatz(number): # Is the mod of 2 equal to 0? if number % 2 == 0: print(number // 2) return number // 2 # If the mod of 2 isn't equal to 0, print `3 * number + 1` elif number % 2 == 1: result = 3 * number + 1 print(result) return result # Ask input from the user n = input("Give me a number: ") # As long as `n` is not equal to `1`, run `collatz()` while n != 1: n = collatz(int(n))
Give me a number: 5 16 8 4 2 1
You can tackle the for loop in the same way as the while loop. As you probably would have expected, the "for" component in "for loop" refers to something that you do for a certain number of times.
If you keep all the above in mind, you can easily define the for loop as follows:
A for loop is a programming concept that, when it's implemented, executes a piece of code over and over again "for" a certain number of times, based on a sequence.
In contrast to the while loop, there isn't any condition actively involved - you just execute a piece of code repeatedly for a number of times. In other words, while the while loop keeps on executing the block of code contained within it only till the condition is
True, the for loop executes the code contained within it only for a specific number of times. This "number of times" is determined by a sequence or an ordered list of things.
You'll learn more about the difference between while and for loops in a bit, but for now, concentrate on the following pieces that you need in order to create a for loop:
range()function, which is an built-in function in the Python library to create a sequence of numbers
# Print "Thank you" 5 times for number in range(5): print("Thank you")
Thank you Thank you Thank you Thank you Thank you
As you can see, the components that you saw in the above section return in this small example of a for loop in Python: the
for keyword, the variable
in keyword, the
range() function and the code that you want to execute multiple times,
That isn't so hard, right?
Let's consider another example of a for loop, where you make use of two variables to define your control flow:
languages = ['R', 'Python', 'Scala', 'Java', 'Julia'] for index in range(len(languages)): print('Current language:', languages[index])
Current language: R Current language: Python Current language: Scala Current language: Java Current language: Julia
As you can see, you start off the loop with the
for keyword. Next, you make use of a variables
in keyword, and the
range() function to create a sequence of numbers. Additionally, you see that you also use the
len() function in this case, as the
languages list is not numerical. The piece of code that you want to execute repeatedly is a print statement, namely,
print('Current language :', languages[index]).
In the loop above, you mean to express that for every index in the range
len(languages), you want to print the the data science programming language. Now,
len(languages) is 5, so the statement could also be rewritten as:
for index in range(5): print('Current language:', languages[index])
Current language: R Current language: Python Current language: Scala Current language: Java Current language: Julia
And this once again gives you the same result!While versus For Loops in Python
Let's revisit the very first while loop example once again to determine what now exactly are the differences between while and for loops. You already read above that the difference lies in the condition that is or is not involved, but how does this reflect in the code and how can you maybe switch between the two?
# Take user input number = 2 while number < 5 : print("Thank you") # Increment `number` by 1 number = number+1
Thank you Thank you Thank you
You can use for loop to print the statement "Thank you" in a more controlled manner:
# Print "Thank you" 3 times for number in range(3) : print("Thank you")
Thank you Thank you Thank you
See how easy it was to convert the while loop into an equivalent for loop?
How does it work, you ask? Well it's simple.
In a for loop, the integer mentioned inside the range function is the range or the number of times the control needs to loop and execute the code in the for loop's clause.
Note that the
range() function's count starts from 0 and not from 1. That means that, in the above example, the count should be like 0,1,2 and not 1,2,3. That's how number counting in a computer's memory works. So, while designing a for loop, always keep in mind that you have to consider the count of range from 0 and not from 1.
Tip: this is the same for lists in Python, for example. If you'd like to know more about Python lists, consider checking out DataCamp's 18 Most Common Python List Questions tutorial.
Now, there is another interesting difference between a for loop and a while loop. A for loop is faster than a while loop. To understand this you have to look into the example below.
import timeit # A for loop example def for_loop(): for number in range(10000) : # Execute the below code 10000 times sum = 3+4 #print(sum) timeit.timeit(for_loop)
import timeit # A while loop example def while_loop(): i =0 while i<10000: sum = 3+4 #print(sum) i+=1 timeit.timeit(while_loop)
In the code chunk above, you have two loops with around 10000 iterations. Both look the same at first sight, until you look behind the scenes and understand how these two loops work. Hint: the
timeit() function gives you a clue of what might be the difference!
Remember: all Python code is compiled with a C compiler, which means that the code that you see above is first broken down into byte-codes and then it's processed by the underlying C compiler.
When the execution of the for loop in the above example starts, the Python interpretor talks to the underlying C compiler and then creates a list object of size 10000. Next, it calls an iterator to touch upon the index on each of the 10000 items in the list.
The execution of the while loop, on the other hand, doesn't create any list object. In fact, the underlying C compiler calls the boolean comparison operator for the condition
i<10000 9999 times.
You can already imagine that iterating over an already created list object with 10000 elements is easier for the compiler than performing a boolean operation repeatedly for 9999 times, the time performance of a for loop is better than that of a while loop. This is clearly reflected in the execution time: the time for the for loop to complete is a lot smaller than the time the while loop needs to complete.
# Set `fib_no` to 55, the number until where you want to print fib_no = 55 # Set `first_no` to 0 first_no = 0 # Set `second_no` to 1 second_no = 1 # Set the counter `count` to 0 count = 0 # while loop to print the fibonacci series until the `fib_no` while first_no <= fib_no: # Print `first_no` print(first_no) # Fibonnacci number nth = first_no + __________ # update values of `first_no` and `second_no` first_no = ____________ second_no = ___ # Set counter `count` +1
Now try out the for loop! Note that you want to specify the range from 0 to 11, since you want to display only the numbers in the sequence up to 55, which is the 11th number in the sequence.
# Initialize `first_no` to `0` first_no = 0 # Initialize `second_no` to `1` second_no = 1 # Initialize `numbers` numbers = ___________ # Find and display Fibonacci series for num in _______: if(num <= 1): # Update only `nth` nth = ___ else: # Update the values `nth`, `first_no` and `second_no` nth = first_no + second_no first_no = second_no second_no = __________ # Print `nth` print(nth)
As you can notice in an example above, there is an if-else condition inside the while loop which enables you to introduce further conditions in your code.
This is not the only way that you can customize your loop. You can also include some more while loop inside you existing code and this is known as a nested loop. You can modify the above example to include another while loop like below:
# Take user input number = 2 # condition of the while loop while number < 5 : # condition of the nested while loop while number % 2 == 0: print("The number "+ str(number)+" is even")
In the above example there is another while loop which is "nested" inside the outer loop, this inner loop puts in another check to see if the
number % (mod) 2 is
In other words, it checks if the number is even and then it prints the statement "The number is even".
But there's a catch: if you look closely, you can see that like the previous code the
number=number+1 statement is missing this time. Since you aren't incrementing the variable number anywhere, the value of the variable remains the same every time and the code enters an infinite loop. That means that, once it enters the loop, it never leaves and prints the statement an infinite number of times because the variable
number will always be set to 2. This number is, of course, less than 5 and an even number.
Let's now take a look at what a nested for loop would look like:
# Print the below statement 3 times for number in range(3) : print("-------------------------------------------") print("I am outer loop iteration "+str(number)) # Inner loop for another_number in range(5): print("****************************") print("I am inner loop iteration "+str(another_number))
------------------------------------------- I am outer loop iteration 0 **************************** I am inner loop iteration 0 **************************** I am inner loop iteration 1 **************************** I am inner loop iteration 2 **************************** I am inner loop iteration 3 **************************** I am inner loop iteration 4 ------------------------------------------- I am outer loop iteration 1 **************************** I am inner loop iteration 0 **************************** I am inner loop iteration 1 **************************** I am inner loop iteration 2 **************************** I am inner loop iteration 3 **************************** I am inner loop iteration 4 ------------------------------------------- I am outer loop iteration 2 **************************** I am inner loop iteration 0 **************************** I am inner loop iteration 1 **************************** I am inner loop iteration 2 **************************** I am inner loop iteration 3 **************************** I am inner loop iteration 4
The above code is a modified version of the first for loop example. Note how a second for loop is used inside the outer loop.
Go ahead and execute the code.
You will find out that the control enters the first for loop and the value of the variable
number is initialized as 0. The first print statement is printed, and then control enters the second for loop, where the value of the variable
another_number is initialized to
0. The first print statement in the second for loop is printed once.
Now, the control returns to the inner for loop once again and the value of
another_number is again initialized to the next integer followed by printing the statement inside the
The aforementioned process continues until the control has traversed through the end of the
range() function, which is 5 in this case, and then the control returns back to the outermost loop, initializes the variable
number to the next integer, prints the statement inside the
print() function, visits the inner loop and then repeats all of the above steps until the
range() function is traversed.
This journey of the control traveling from the outermost loop, traversing of the inner loop and then back again to the outer for loop continues until the control has covered the entire range, which is 3 times in your case.
Now that you have read some explanations on nested loops, it's time to get started yourself! Write a Python program to construct the following pattern, using a nested for loop:
# Initialize the first five rows n = _ # Start the loop to print the first five rows for i in range(_): for j in range(i): print('* ', end="") print('') # Start the loop to print the remaining rows in decreasing order of stars for i in range(n,0,-1): for j in range(_): print('* ', end="") print('')
continueKeywords: Creating Infinite Loops
You can use
continue in any loop you create. These keywords aren't restricted to breaking intentional infinite loops, but they should be used carefully. Both keywords make it harder for others -including yourself in a couple of months when you look back to your code- to understand the control flow in the loop and where the condition ends. Additionally, you don't need to use the
continue keywords: as you have seen up until now, you were doing perfectly fine without them!
However, in some cases, it can be more clear to write intentional infinite loops rather than the traditional for and while loops that you have seen up until now. Of course, in those cases the use of these keywords is encouraged!
Fortunately, there is a way to break out of the above situation of infinite loop and that is using the
# Take user input number = 2 # Condition of the while loop while number < 5 : # condition of the nested while loop while number % 2 == 0: print("The number "+str(number)+" is even") break number+=1
When you run the above code it doesn't go inside an infinite loop anymore.
"What is this sorcery!" you scream. - I say relax, it's no sorcery.
What happens is that when the control encounters the
break keyword it understands that it has to exit the loop (inner loop in this case) and executes the next line of code. It therefore breaks out of the inner while loop and proceeds to the outer loop and continues doing it's usual stuff until the condition in the outer while loop holds true.
You should consider the fact that a line of code must exists after the
break keyword or else it won't have any effect. Try to remove the line of code after the
break keyword in the above example and then execute the code.
What if you sneak in the
continue keyword after the
break statement? You can follow the code below:
# Take user input number = 2 while number < 5 : while number % 2 == 0: print("The number "+str(number)+" is even") break continue number+=1
The above code is a slightly modified version of the
break keyword example.
When you run the above code you will again encounter an infinite loop. Before you start pulling your hair in frustration, take a closer look. You will notice that there's a
continue keyword after the break. What the
continue keyword does is that it transfers the control back to the outermost loop even before the variable
number is incremented and because of this the code goes into an infinite loop.
What if you did not want to execute the inner for loop in the above example for the entire range? You can use a
break statement in a similar way that you have done in the case of the while loop.
# Print the below statement 3 times for number in range(3) : print("-------------------------------------------") print("I am outer loop iteration "+str(number)) for another_number in range(3): print("****************************") print("I am inner loop iteration "+str(another_number)) break
In the above example, the
break keyword after the inner loop instructs the control to break away from the inner loop, after which the control travels back to the outer loop.
Run the below code example:
# Print the below statement 3 times for number in range(3) : print("-------------------------------------------") print("I am outer loop iteration "+str(number)) continue for another_number in range(3): print("****************************") print("I am inner loop iteration "+str(another_number)) break
In the above code only the outermost loop is executed and the control doesn't even touch the inner loop.
"Why?" you ask.
Take a closer look at the code. There's a
continue keyword just after the second print statement in the outermost loop. What it does is that it instructs the control to jump back to the outer for loop and start over again and that's why the control never touches the inner loop.
Unlike the while loop, you don't need a piece of code after the
break keyword in the for loop. A break loop alone will work just fine inside a for loop.
These two functions are similar to one another, but if you're using Python 3, you'll only have the
range() function available. In Python 3.x, the
xrange() function is renamed as
range(). That means that if you try to use the
xrange() function with Python 3, you will get
NameError: name 'xrange' is not defined error.
To understand how these two functions are similar, consider the following example:
# Print the below statement 3 times for number in range(5,10,2) : print("I am number : "+str(number))
I am number : 5 I am number : 7 I am number : 9
By running the code above, you will see how the variable
number's value is skipped by a factor of 2, and that's because of the parameters in the
The first parameter in this function is the start parameter. That is the point from where the control starts counting. The second parameter is the stop parameter which is the point where the control has to stop and the last parameter is the step function, which defines the factor by which the control has to jump while counting.
So, in the above case, the control start from 5 then it traverses the loop till 9 while adding 2 during every count. This means 5, 5+2=7, 7+2=9.
You have learned how the
range() function is used to define the number of times your code has to loop. Now, in Python 2.x, you'll also find another way to do this and that's with the
xrange() function. Run the below code:
# Print the below statement 3 times for number in xrange(10) : print("I am number : "+str(number))
I am number : 0 I am number : 1 I am number : 2 I am number : 3 I am number : 4 I am number : 5 I am number : 6 I am number : 7 I am number : 8 I am number : 9
What difference did you see in the output of the above code vs the output of the code without the
xrange() function? No difference?
That's right. You won't see any difference in the output of the code.
So why should you use the
xrange() function? You'll get to this a little later on the tutorial. First run the code below:
xrange(10) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
You can see that in the output of the above code, there is a difference in the output of both the print statements. In the print statement, which contains the
xrange() function, you get the
xrange() function object printed. This is different in the print statement that has the
range() function: there, you get a list of integers from 0 until 9.
Now, this is exactly what the difference between the
range() function and the
xrange() function is.
When you define the
range() function, a list of the entire range is stored in the RAM and presented to you. However, when you define the
xrange() function, a list is not stored in the memory. Instead, the
xrange() function generates integers in the entire range when you need it. This way, the
xrange() function makes helps in conserving memory in resource-starved system.
Congrats, you have made it to the end of this tutorial. Now that you have learned how to use loops in Python, go ahead and practice. The more you practice, the better!
Python GUI Programming Projects using Tkinter and Python 3
Learn Hands-On Python Programming By Creating Projects, GUIs and Graphics
Python is a dynamic modern object -oriented programming language
It is easy to learn and can be used to do a lot of things both big and small
Python is what is referred to as a high level language
Python is used in the industry for things like embedded software, web development, desktop applications, and even mobile apps!
SQL-Lite allows your applications to become even more powerful by storing, retrieving, and filtering through large data sets easily
If you want to learn to code, Python GUIs are the best way to start!
I designed this programming course to be easily understood by absolute beginners and young people. We start with basic Python programming concepts. Reinforce the same by developing Project and GUIs.
The Python coding language integrates well with other platforms – and runs on virtually all modern devices. If you’re new to coding, you can easily learn the basics in this fast and powerful coding environment. If you have experience with other computer languages, you’ll find Python simple and straightforward. This OSI-approved open-source language allows free use and distribution – even commercial distribution.
When and how do I start a career as a Python programmer?
In an independent third party survey, it has been revealed that the Python programming language is currently the most popular language for data scientists worldwide. This claim is substantiated by the Institute of Electrical and Electronic Engineers, which tracks programming languages by popularity. According to them, Python is the second most popular programming language this year for development on the web after Java.
Python Job Profiles
The median total pay for Python jobs in California, United States is $74,410, for a professional with one year of experience
Below are graphs depicting average Python salary by city
The first chart depicts average salary for a Python professional with one year of experience and the second chart depicts the average salaries by years of experience
Who Uses Python?
This course gives you a solid set of skills in one of today’s top programming languages. Today’s biggest companies (and smartest startups) use Python, including Google, Facebook, Instagram, Amazon, IBM, and NASA. Python is increasingly being used for scientific computations and data analysis
Take this course today and learn the skills you need to rub shoulders with today’s tech industry giants. Have fun, create and control intriguing and interactive Python GUIs, and enjoy a bright future! Best of Luck
Who is the target audience?
Anyone who wants to learn to code
For Complete Programming Beginners
For People New to Python
This course was designed for students with little to no programming experience
People interested in building Projects
Anyone looking to start with Python GUI development
Access to a computer
Download Python (FREE)
Should have an interest in programming
Interest in learning Python programming
Install Python 3.6 on your computer
What will you learn
Build Python Graphical User Interfaces(GUI) with Tkinter
Be able to use the in-built Python modules for their own projects
Use programming fundamentals to build a calculator
Use advanced Python concepts to code
Build Your GUI in Python programming
Use programming fundamentals to build a Project
Signup Login & Registration Programs
Job Interview Preparation Questions
& Much More
Guide to Python Programming Language
The course will lead you from beginning level to advance in Python Programming Language. You do not need any prior knowledge on Python or any programming language or even programming to join the course and become an expert on the topic.
The course is begin continuously developing by adding lectures regularly.
Please see the Promo and free sample video to get to know more.
Hope you will enjoy it.
An Enthusiast Mind
Basic Knowledge To Use Computer
What will you learn
Will Be Expert On Python Programming Language
Build Application On Python Programming Language
Python Programming Tutorials For Beginners
Hello and welcome to brand new series of wiredwiki. In this series i will teach you guys all you need to know about python. This series is designed for beginners but that doesn't means that i will not talk about the advanced stuff as well.
As you may all know by now that my approach of teaching is very simple and straightforward.In this series i will be talking about the all the things you need to know to jump start you python programming skills. This series is designed for noobs who are totally new to programming, so if you don't know any thing about
programming than this is the way to go guys Here is the links to all the videos that i will upload in this whole series.
In this video i will talk about all the basic introduction you need to know about python, which python version to choose, how to install python, how to get around with the interface, how to code your first program. Than we will talk about operators, expressions, numbers, strings, boo leans, lists, dictionaries, tuples and than inputs in python. With
Lots of exercises and more fun stuff, let's get started.
Download free Exercise files.
Who is the target audience?
First time Python programmers
Students and Teachers
IT pros who want to learn to code
Aspiring data scientists who want to add Python to their tool arsenal
Students should be comfortable working in the PC or Mac operating system
What will you learn
know basic programming concept and skill
build 6 text-based application using python
be able to learn other programming languages
be able to build sophisticated system using python in the future