Coding & Robotics - Python Loops PDF

Document Details

HaleChicago

Uploaded by HaleChicago

Henry Alex-Duduyemi Memorial College

N.D. Elroi

Tags

python loops coding robotics programming

Summary

This document is a lesson on loops in Python. It includes various types of loops, examples, and flowcharts to aid understanding. The lesson is likely for a secondary school class.

Full Transcript

Henry Alex-Duduyemi Memorial College Coding & Robotics with N.D. Elroi At the end of this lesson, you’ll: Know various types of loops available in Python. Know the usage, and working of the loop. Will be able to analyze and decide on an appropriate combination of Loop...

Henry Alex-Duduyemi Memorial College Coding & Robotics with N.D. Elroi At the end of this lesson, you’ll: Know various types of loops available in Python. Know the usage, and working of the loop. Will be able to analyze and decide on an appropriate combination of Loops and Conditions. Design simple applications having iterative nature. Create a Python program to identify if a number is a perfect square or not. PERFECT SQUARE DETECTOR LOOPS A loop is an algorithm that runs a block of code multiple times till the time a specified condition is met. In programming, repetition of a line or a block of code is also known as iteration. Therefore, we can say that a loop iterates a block of code multiple times till the mentioned condition is satisfied. To run a block of code in a loop, one needs to set a condition and set its number of iterations. Each time the condition is true, and the block of code executes once, it is counted to be one iteration. If you want more iterations, you will need to increase the number of iterations. This is called Incrementing a Loop. For example, if you need to print numbers 0 to 7, you will execute a block of code with the Print statement in eight iterations. With each passing iteration, you will increment the count by one. Let’s create a flow chart to understand this better: THE LOGICAL FLOW CHART LOOKS LIKE THIS... NOW, LET US ANALYSE THIS FLOWCHART SO THAT WE COULD GET A BETTER UNDERSTANDING. 1. Here every time the condition (Count < 8) is true, “Print count” gets executed. So, we do not have to write the “Print” statement multiple times. The loop takes care of that. 2. What is important to note is that every loop must have an exit condition. In our example, the exit condition is (Count < 8). The loop will exit when the condition becomes false. 3. Also, most loops will have a variable that is called a counter variable in programming terms. The counter variable keeps track of how many times the loop is executed. In this example, the “count” variable is our counter. BENEFITS OF LOOPS Below are the two important benefits of loops: 1. Reduces lines of code instead of long lines of repeated codes. It makes code short and easier to debug. 2. Code becomes easier to understand and manipulate. TYPES OF LOOPS The different types of loops are: 1. While Loop 2. For Loop 3. Nested Loop NOW, LET‘S US EXPLORE THEM 4. IF Loop INDIVIDUALLY The While loop can execute a set of commands till the condition is true. While Loops are also called Conditional Loops. Once the condition is met then the loop is finished. GENERALLY, IT FLOWS LIKE THIS WHILE LOOP SYNTAX The syntax of the while loop is: while condition: # condition is Boolean expression returning True or False STATEMENTs BLOCK 1 1. We can see that while looks like an if statement. The statement begins with a keyword while followed by a boolean condition followed by a colon (:). What follows next is a block of the statement(s). 2. The statement(s) in BLOCK 1 keeps on executing till the condition in while remains True. While loop may not execute even once, if the condition evaluates to false, initially, as the condition is tested before entering the loop. NOW, LET‘SSEE AN EXAMPLE EXAMPLE sprite = Sprite('Tobi') sprite.input("Enter the number") N = int(sprite.answer()) i=1 while (i

Use Quizgecko on...
Browser
Browser