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 Python while loop: you'll learn how you can construct and use a while loop in data science applications. You'll do this by going over some interactive coding challenges.
Next, you'll move on to the for loop: once again, you'll learn how you can construct and use a for loop in a real-life context.
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:
The while keyword;
A condition that transates to either True or False; And
A block of code that you want to execute repeatedly
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 loopwhile number <5:print("Thank you")# Increment the value of the variable "number by 1"
number = number+1# Thank you# Thank you# Thank you제공
Learn-by-Example 에디터, Tab을 눌러 입력하세요. Escape를 누른 후 Tab 또는 Shift+Tab을 눌러 탐색하세요.
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 True or False (number < 5) and a block of code that you want to execute repeatedly:
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:
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 loopwhile number <5:# Find the mod of 2if number%2==0:print("The number "+str(number)+" is even")else:print("The number "+str(number
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 :
breakandcontinuekeywords.xrange()andrange()functionsWhile Loop
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:
whilekeyword;TrueorFalse; AndThat'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:
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
whilekeyword, followed by a condition that translates to eitherTrueorFalse(number < 5) and a block of code that you want to execute repeatedly:If you go into detail in the above code, you see that there is a variable
numberin which you store an integer2. Since the value innumberis smaller than 5, you print out"Thank you"and increase the value ofnumberwith one. While the value innumberstays smaller than 5, you continue to execute the two lines of code that are contained within the while loop:You print out "Thank you" two more times before the value of
numberis equal to5and the condition doesn't evaluate toTrueany more. Because the condition now evaluates toFalse, 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: