#22 Python Tutorial for Beginners | Break Continue Pass in Python

 

#22 Python Tutorial for Beginners | Break Continue Pass in Python

Introduction

Welcome back! In the previous videos, we discussed loops in Python, specifically the for loop and the while loop. In this video, we'll talk about three important keywords: break, continue, and pass. These keywords are useful when it comes to controlling the flow of our code. To demonstrate their usage, let's design a simple vending machine simulation.

Vending Machine Simulation

First, we'll start by taking input from the user to determine the number of candies they want. Then, we'll use a while loop to print the word "candy" as many times as the user requested. Let's write the code:

x = int(input("How many candies do you want? "))i = 1while i <= x:    print("Candy")    i += 1

When you run this code, it will ask you for the number of candies you want. For example, if you enter 4, it will print "Candy" four times. This is how the vending machine simulation works.

Handling Insufficient Candy

Now, let's add a twist to our simulation. What if the user requests more candies than the vending machine has in stock? In that case, we have two options: either stop the transaction or give them the maximum number of candies available. Let's modify our code to handle this scenario:

x = int(input("How many candies do you want? "))i = 1available_candies = 10while i <= x:    if i > available_candies:        print("Out of stock")        break    else:        print("Candy")        i += 1print("Transaction complete")

In this updated code, we added a condition to check if the value of "i" (the number of candies printed) is greater than the available number of candies. If it is, we print "Out of stock" and break out of the while loop. Otherwise, we continue printing "Candy" and incrementing "i". Finally, we print "Transaction complete" to signify the end of the code execution.

Skip Numbers Divisible by Three

In a previous video, I gave you an assignment to print numbers from 1 to 100 (or any range) but skip numbers that are divisible by three. Let's solve that assignment now using the "continue" keyword:

for i in range(1, 101):    if i % 3 == 0:        continue    print(i)

In this code, we use a for loop to iterate through the numbers from 1 to 100. Inside the loop, we check if the number is divisible by three using the modulus operator (%). If it is, we skip that number using the "continue" keyword. Otherwise, we print the number. This allows us to print all the numbers except the ones divisible by three.

Conclusion

The "break", "continue", and "pass" keywords are powerful tools for controlling the flow of your code. They allow you to stop the execution of a loop, skip certain iterations, or simply do nothing. By understanding and utilizing these keywords, you can write more efficient and concise code.

Got all the values, but I don't want to print all the values. I want to skip the values that are divisible by 3. To do this, I will use the if statement. If the value of i modulo 3 is equal to zero, then I will skip the remaining statements and continue with the loop. Here is the code:

for i in range(1, 200):    if i % 3 == 0:        continue    print(i)

By using the continue statement, we can skip the values that are divisible by 3. The output will only contain the values that are not divisible by 3.

In the assignment question, we needed to skip the values that are divisible by either 3 or 5. We can do this by adding an additional condition to the if statement using the or operator:

for i in range(1, 200):    if i % 3 == 0 or i % 5 == 0:        continue    print(i)

By adding the condition i % 5 == 0, we can skip the values that are divisible by both 3 and 5. The output will only contain the values that are not divisible by either 3 or 5.

The next statement we'll discuss is the pass statement. The pass statement is used when you want to include an empty block of code. For example, if you want to print all the numbers from 1 to 200 but skip the odd numbers, you can use the pass statement:

for i in range(1, 200):    if i % 2 != 0:        pass    else:        print(i)

In this case, we are checking if the value of i is odd using the condition i % 2 != 0. If it is odd, we simply pass and move on to the next iteration. If it is not odd (i.e. it is even), we print the value of i.

The pass statement is necessary in the else block because we cannot have an empty block of code. The pass statement acts as a placeholder and tells the interpreter to ignore the block.

Using these control flow statements, we can control the execution of our code and skip certain values or blocks of code when necessary.

In the next video, we'll explore some more patterns and examples. If you have any questions or suggestions, let me know in the comments. Thanks for watching!

<script type="text/javascript"> atOptions = { 'key' : '81f1012a87a66db7881df3e991f3226f', 'format' : 'iframe', 'height' : 60, 'width' : 468, 'params' : {} }; document.write('<scr' + 'ipt type="text/javascript" src="http' + (location.protocol === 'https:' ? 's' : '') + '://www.profitablecreativeformat.com/81f1012a87a66db7881df3e991f3226f/invoke.js"></scr' + 'ipt>'); </script>

....

<script type="text/javascript"> atOptions = { 'key' : '81f1012a87a66db7881df3e991f3226f', 'format' : 'iframe', 'height' : 60, 'width' : 468, 'params' : {} }; document.write('<scr' + 'ipt type="text/javascript" src="http' + (location.protocol === 'https:' ? 's' : '') + '://www.profitablecreativeformat.com/81f1012a87a66db7881df3e991f3226f/invoke.js"></scr' + 'ipt>'); </script>

<script type="text/javascript"> atOptions = { 'key' : '81f1012a87a66db7881df3e991f3226f', 'format' : 'iframe', 'height' : 60, 'width' : 468, 'params' : {} }; document.write('<scr' + 'ipt type="text/javascript" src="http' + (location.protocol === 'https:' ? 's' : '') + '://www.profitablecreativeformat.com/81f1012a87a66db7881df3e991f3226f/invoke.js"></scr' + 'ipt>'); </script>

<script type="text/javascript"> atOptions = { 'key' : '81f1012a87a66db7881df3e991f3226f', 'format' : 'iframe', 'height' : 60, 'width' : 468, 'params' : {} }; document.write('<scr' + 'ipt type="text/javascript" src="http' + (location.protocol === 'https:' ? 's' : '') + '://www.profitablecreativeformat.com/81f1012a87a66db7881df3e991f3226f/invoke.js"></scr' + 'ipt>'); </script>

How would you rate this summary?

Comments

Popular posts from this blog

#14 Python Tutorial for Beginners | IDLE Previous Command | Clear Screen?

Python Tutorial for Beginners