=======================================================================
For Loop
- using sequence OR
- using range function
# creating the list of numbers
numbers = [3, 5, 23, 6, 5, 1, 2, 9, 8]
print("given numbers are:",numbers)
# initializing a variable that will store the sum
sum = 0
# using for loop to iterate over the list
for num in numbers:
sum=sum+num**2
print("The sum of squares is sum+num**2: ", sum)
Output:
given numbers are: [3, 5, 23, 6, 5, 1, 2, 9, 8]
The sum of squares is sum+num**2: 774
1.Range (start, stop, step)
- start---- starting value---loop variable
- stop---- end value--- condition
- step-- diff of start and end values --- for updating
program
for i in range(0,6,1):
print(i)
Output:
0
1
2
3
4
5
program
for i in range(0,20,4):
print(i)
Output:
0
4
4
8
12
16
12
16
Program
my_list = [3, 5, 6, 8, 4]
print("my list values are:",my_list)
print(" ")
for iter_var in range( len( my_list ) ):
my_list.append(my_list[iter_var] + 2)
print("my list after append:", my_list )
Output:
my list values are: [3, 5, 6, 8, 4]
my list after append: [3, 5, 6, 8, 4, 5]
my list after append: [3, 5, 6, 8, 4, 5, 7]
my list after append: [3, 5, 6, 8, 4, 5, 7, 8]
my list after append: [3, 5, 6, 8, 4, 5, 7, 8, 10]
my list after append: [3, 5, 6, 8, 4, 5, 7, 8, 10, 6]
2.Sequence
program
str="python"
for letter in str:
print(letter)
Output:
p
y
t
h
o
n
program
i=[10,20,30]
for ele in i:
print(ele)
Output:
10
20
30
Program
# Code to find the sum of squares of each element of the list using for loop
# creating the list of numbers
numbers = [3, 5, 23, 6, 5, 1, 2, 9, 8]
print("given numbers list are:", numbers)
# initializing a variable that will store the sum
sum_ = 0
# using for loop to iterate over list
for num in range( len(numbers) ):
sum_ = sum_ + numbers[num] ** 2
print("The sum of squares is: ", sum_)
Output:
given numbers list are: [3, 5, 23, 6, 5, 1, 2, 9, 8]
The sum of squares is: 9
The sum of squares is: 34
The sum of squares is: 563
The sum of squares is: 599
The sum of squares is: 624
The sum of squares is: 625
The sum of squares is: 629
The sum of squares is: 710
The sum of squares is: 774
Note:
3**2=9
9+5**2=9+25=34
34+23**2=34+529=563
Nested For Loops
import random
numbers = [ ]
for val in range(0, 11):
numbers.append( random.randint( 0, 11 ) )
for num in range( 0, 11 ):
for i in numbers:
if num == i:
print( num, end = " " )
Output:
0 1 2 2 3 4 4 5 8 10
========================================================================
While Loop
Condition:
- true->> will continue
- false-->> stop execute
- loop variable--->initialize
- condition-->> for loop termination
- loop variable will update in every iteration
n=int(input("enter n value: "))
i=0 #loop variable
while i<=n: #loop condition
print(i)
i=i+1 #loop iteration
Output:
enter n value: 5
0
1
2
3
4
5
Loop Control Statements
1. Continue Statement - When the continue statement is encountered, the control transfer to the beginning of the loop. Let's understand the following example.
Example:
# prints all letters except 'a' and 't'
i = 0
str1 = 'javatpoint'
print("str1 value is:", str1)
while i < len(str1):
if str1[i] == 'a' or str1[i] == 't':
i=i+1
continue
print('Current Letter :', str1[i])
i=i+1
otuput:
str1 value is: javatpoint
Current Letter : j
Current Letter : v
Current Letter : p
Current Letter : o
Current Letter : i
Current Letter : n
2. Break Statement - When the break statement is encountered, it brings control out of the loop.
Example:
# The control transfer is transfered
# when break statement soon it sees t
i = 0
str1 = 'javatpoint'
while i < len(str1):
if str1[i] == 't':
i += 1
break
print('Current Letter :', str1[i])
i += 1
Putput:
Current Letter : j
Current Letter : a
Current Letter : v
Current Letter : a
3. Pass Statement - The pass statement is used to declare the empty loop. It is also used to define empty class, function, and control statement
example
# An empty loop
str1 = 'javatpoint'
i = 0
while i < len(str1):
i += 1
pass
print('Value of i :', i)
oputput:
Value of i : 10
====================
#Example-1: Program to print 1 to 10 using while loop
i=0
while(i<=7):
i=i+1
print(i)
1
2
3
4
5
6
7
8
#Example -2: Program to print table of given numbers.
i=1
number=0
b=9
number = int(input("Enter the number:"))
while i<=10:
print("%d X %d = %d \n"%(number,i,number*i)) #(5,1,5*1) , (5,2,5*2)
i = i+1
Enter the number:5
5 X 1 = 5
5 X 2 = 10
5 X 3 = 15
5 X 4 = 20
5 X 5 = 25
5 X 6 = 30
5 X 7 = 35
5 X 8 = 40
5 X 9 = 45
5 X 10 = 50
#example:Using else with while loop
i=1
while(i<=5):
print(i)
i=i+1
else:
print("The while loop exhausted")
1
2
3
4
5
The while loop exhausted
#Example-3 Program to print Fibonacci numbers to given limit
terms = int(input("Enter the terms "))
# first two intial terms
a = 0
b = 1
count = 0
# check if the number of terms is Zero or negative
if (terms <= 0):
print("Please enter a valid integer")
elif (terms == 1):
print("Fibonacci sequence upto",limit,":")
print(a)
else:
print("Fibonacci sequence:")
while (count < terms) :
print(a, end = ' ')
c = a + b
# updateing values
a = b
b = c
count=count+1
enter the terms 10
Fibonacci sequence:
0 1 1 2 3 5 8 13 21 34