Notes looping structure
Python For Loops
A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).
This is less like the for keyword in other programming languages, and works more like an iterator method as
found in other object-orientated programming languages.
With the for loop we can execute a set of statements, once for each item in a list, tuple, set etc.
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
To loop through a set of code a specified number of times, we can use the range() function,
The Range function
The range() function returns a sequence of num
To loop through a set of code a specified number of times, we can use the range() function,
Note that range(6) is not the values of 0 to 6, but the values 0 to 5.
The range() function defaults to 0 as a starting value, however it is possible to specify the starting value by
adding a parameter: range(2, 6), which means values from 2 to 6 (but not including 6)
The range() function defaults to increment the sequence by 1, however it is possible to specify the increment
value by adding a third parameter: range(2, 30, 3)
else in for loop
The else keyword in a for loop specifies a block of code to be executed when the loop is completed:
Print all numbers from 0 to 10, and print a message when the loop has ended:
for x in range(10):
print(x)
else:
print("program completed!")
Program to print the sum of all even number between 5 to 15
Program to display the sum of all odd number between 12 to 22
Program to find the factorial of a given number
Fibonacci numbers
The break Statement
With the break statement we can stop the loop even if the while condition is true:
Prime number check
Armstrong number check
Perfect number check.
Note:A perfect number is a positive integer that is equal to the sum of its positive proper divisors, that is,
divisors excluding the number itself. For instance, 6 has proper divisors 1, 2 and 3, and 1 + 2 + 3 = 6, so 6 is a
perfect number. The next perfect number is 28, since 1 + 2 + 4 + 7 + 14 = 28.
Series printing with alternating sign
Sum of Series
2x1+ 2x2 + 2x3…… upto n terms
y y y
While loop
The four steps of a while loop in Python are:
● Initialization: A loop control variable or variables are initialized before the while loop begins. This sets
the starting state for the loop.
count = 0 # Initialization
● Condition (Test Expression): The while loop evaluates a boolean expression (the condition). If the
condition is True, the loop body is executed. If it is False, the loop terminates, and the program
continues with the code following the loop.
while count < 5: # Condition
● Loop Body: The block of statements indented within the while loop is executed if the condition
evaluates to True. These are the actions that are repeatedly performed.
while count < 5:
print("Current coun:", count ) # Loop Body
● Update Expression: Inside the loop body, the loop control variable(s) are updated. This step is crucial
to ensure that the condition eventually becomes False, preventing an infinite loop.
while count < 5:
print("Current coun:", count ) # Loop Body
count+=1 # Update Expression
Example:
Print i as long as i is less than 6:
i=1
while i < 6:
print(i)
i += 1
Output
1
2
3
4
5
The break Statement
With the break statement we can stop the loop even if the while condition is true:
Example
Exit the loop when i is 3
i=1
while i < 6:
print(i)
if i == 3:
break
i += 1
Output
1
2