260 likes | 1.37k Views
This Edureka "Python Loops" tutorial (Python Tutorial Blog: https://goo.gl/wd28Zr) will help you in understanding different types of loops used in Python. You will be learning how to implement all the loops in python practically. Below are the topics covered in this tutorial: <br><br>1) Why to use loops? <br>2) What are loops? <br>3) Types of loops in Python: While, For, Nested <br>4) Demo on each Python loop
E N D
EDUREKA PYTHON CERTIFICATION TRAINING Python Loops www.edureka.co/python
Agenda ➢ Why to use loops ➢ What are loops ➢ Types of loops in Python ▪ While ▪ For ▪ Nested ➢ Summary www.edureka.co/python EDUREKA PYTHON CERTIFICATION TRAINING
Why To Use Loops? www.edureka.co/python EDUREKA PYTHON CERTIFICATION TRAINING
Why To Use Loops If a software developer develops a software module for payroll processing that needs to compute the salaries and the bonus of all the employees. Employee - 1 Salary Bonus Total Employee - 2 Software module for payroll processing Employee - 3 www.edureka.co/python EDUREKA PYTHON CERTIFICATION TRAINING
Why To Use Loops Manual Loops Start Start Start Start Logic to calculate the total salary Logic to calculate salary of Employee-1 Logic to calculate salary of Employee-2 Logic to calculate salary of Employee-3 Employee-1: Salary Bonus Total Employee-2: Salary Bonus Total Employee-3: Salary Bonus Total Employee-1: Salary Bonus Total Employee-2: Salary Bonus Total Employee-3: Salary Bonus Total www.edureka.co/python EDUREKA PYTHON CERTIFICATION TRAINING
What are Loops? www.edureka.co/python EDUREKA PYTHON CERTIFICATION TRAINING
What Are Loops Loops allows the execution of a statement or a group of statement multiple times. In order to enter the loop there are certain conditions defined in the beginning. Once the condition becomes false the loop stops and the control moves out of the loop. Start Conditional Code If condition is true Infinite Finite Loops Condition If condition is false Exit www.edureka.co/python EDUREKA PYTHON CERTIFICATION TRAINING
Loops In Python www.edureka.co/python EDUREKA PYTHON CERTIFICATION TRAINING
While Loop While loops are known as indefinite or conditional loops. They will keep iterating until certain conditions are met. There is no guarantee ahead of time regarding how many times the loop will iterate. While Start Syntax: False While Condition? For True Body of the loop Nested Exit www.edureka.co/python EDUREKA PYTHON CERTIFICATION TRAINING
While Loop Example A little guessing game While Let the correct answer be 13 For Input Number: 10 Number is too small Input Number: 15 Number is too large Nested Input Number: 13 Exit: Congratulations. You made it! www.edureka.co/python EDUREKA PYTHON CERTIFICATION TRAINING
For Loop For loop is a Python loop which repeats a group of statements a specified number of times. The for loop provides a syntax where the following information is provided: Boolean condition The initial value of the counting variable Incrementation of counting variable While Start If no more items in the sequence Item from sequence For Next item from sequence Execute Statement (s) Nested End www.edureka.co/python EDUREKA PYTHON CERTIFICATION TRAINING
For Loop Example Lets find factorial Notice we know the number of iterations so for loop is a better option While n n-1 n-2 1 For 3! = 3(2)(1) 4! = 4(3)(2)(1) Example 5! = 5(4)(3)(2)(1) Nested Factorial = n(n-1)(n-2)…..1 www.edureka.co/python EDUREKA PYTHON CERTIFICATION TRAINING
Nested Loops Python programming language allows use of loop inside another loop. This is called Nested Loop. below is the syntax for the same: While Syntax: Syntax: For Nested www.edureka.co/python EDUREKA PYTHON CERTIFICATION TRAINING
Nested Loop Example Lets code a program in Python that effectively simulates a bank ATM. While Check balance Make a withdrawal For Enter the 4-digit pin Pay in Return card Nested www.edureka.co/python EDUREKA PYTHON CERTIFICATION TRAINING
Nested Loop Example Lets write a code to print the Pythagorean numbers. Three integers satisfying a2+b2=c2are called Pythagorean numbers a2 c2 While b2 Enter the maximum number: 20 For 3 4 5 5 12 13 6 8 10 8 15 17 9 12 15 12 16 20 Pythagorean numbers between 1-20 Nested www.edureka.co/python EDUREKA PYTHON CERTIFICATION TRAINING
Nested Loop Example Using a for loop inside while loop While Bulk reservation: Name: Age: Sex: For Nested www.edureka.co/python EDUREKA PYTHON CERTIFICATION TRAINING
Agenda Types of loops What are loops Why loops Nested loop While loop For loop www.edureka.co/python EDUREKA PYTHON CERTIFICATION TRAINING
Thank You … Questions/Queries/Feedback www.edureka.co/python EDUREKA PYTHON CERTIFICATION TRAINING