Class Xi For Loop Students 2021-22 - Colaboratory
Class Xi For Loop Students 2021-22 - Colaboratory
Looping: As the name suggests, the statement which runs in loop (goes on from start to end to
start) are termed as looping statements. These statements are also known as Flow Control
statements because the flow of these loops are controlled with the help of certain conditions
which help in proper iterations.
For loop is the most basic yet versatile flow control statement. Let us look at its syntax:
Syntax:
for val in sequence:
Statement 1
Statement 2
Statement 3
...
Here, as you see, the first statement talks about the initiation and termination of the loop.
Subsequent statements which shall be executed inside the loop are indented under it.
range(min,max+1,stepvalue)
1-10 range(1,11,1) OR range(1,11)
for var in range(1,11):
print(var, end= "-----")
1-----2-----3-----4-----5-----6-----7-----8-----9-----10-----
In the above code, i is a variable which would run for the range mentioned. Here, the rangehas
been mentioned as (q,qq) which means that the loop initiates at 1 and terminates as soon as it
encounters 11.
In other words, we can say that 1 is the starting point for the loop and 11 is the exit point for the
loop.
Here, in the output we can see numbers printed from 0 to 10. Initially i starts with 1.
1 gets printed as the print statement is inside the loop statement. Once print statement has
been executed, i gets incremented to 1. Thus 1 gets printed in the next line. Similarly i gets
incremented everytime till it reaches 11. Once i becomes 11, it reaches the exit point hence the
loop terminates. Which means the print statement is not executed for 11. Therefore we get
numbers from 1 to 10 as output.
The range(1,11) can be described as the sequence for which loop runs.
# 1) sequence = range(n)
#The loop will print no from 0 to 9
for i in range(10): # If initial point of loop is not mentioned, it is by default
print(i,end= " ")
0 1 2 3 4 5 6 7 8 9
print("To print numbers from 1 to 10",end="\t\t")
for i in range(1,11):
print(i, end="-")
print()
print("To print numbers from 10 to 1",end="\t\t")
for i in range(10,0,-1):
print(i, end=" ")
print("\nTo print even numbers from 2 to 10",end="\t\t")
for i in range(2,12,2):
print(i, end="-")
print("\nTo print odd numbers from 1 to 10",end="\t\t")
for i in range(1,11,2):
print(i, end="-")
print("\nTo print first 10 multiples of n (n entered by user)",end="\t\t")
n=int(input("enter the number")) # e.g.5
for i in range(n,n*10+1,n): # range(5,51, 5)
print(i, end="-")
n=int(input("\nEnter no of terms\t"))
print("\nTo print numbers from 1 to",n,end=" ")
for i in range(1,n+1):
print(i,end=" ")
#1.1 LOOP TO DISPLAY NUMBERS
print("To print numbers from 1 to 10",end="\t\t")
for i in range(1,11):
print(i, end="-")
print()
print("To print numbers from 10 to 1",end="\t\t")
for i in range(10,0,-1):
print(i, end="-")
print("\nTo print even numbers from 2 to 10",end="\t\t")
for i in range(2,11,2):
print(i, end="-")
print("\nTo print odd numbers from 1 to 10",end="\t\t")
for i in range(1,11,2):
print(i, end="-")
print("\nTo print first 10 multiples of 5",end="\t\t")
for i in range(5,51,5):
print(i, end="-")
n=int(input("\nEnter no of terms\t"))
print("\nTo print numbers from 1 to",n,end=" ")
for i in range(1,n+1):
print(i,end=" ")
'''
# loop 1+2+3+4+5
sum=0
sum=sum+ loop(1,2,3,4,5)
0+ 1=1
1+ 2=3
3+ 3=6
6+ 4=10
10+ 5=15
when loop is over the program will print 15
'''
sum=0
print("Displaying")
for i in range(1,6):
sum=sum+i
print("Sum of numbers\t",sum)
Displaying
Sum of numbers 63
#PROGRAM 2
print("\n Sum of even numbers from 2 to 10",end="\t\t")
#complete the code
sum=0
for i in range(2,11,2):
sum=sum+i
# write your code here
print("Sum of even nos from 2 to 10", sum, end="-")
#PROGRAM 3
print("\nTo find product of numbers from 1 to 10",end="\t\t")
product=1
for i in range(1,11,2):
product=product*i
# write your code here
print("Product from 1 to 10", product)
#PROGRAM 4
print("\n Sum of even and odd numbers from 1 to 10",end="\t\t")
#complete the code
sum_even=0
sum_odd=0
for i in range(1,11):
if i%2==0:
sum_even=sum_even+ i
else:
sum_odd=sum_odd+i
print("Sum of even numbers",sum_even)
print("Sum of odd numbers",sum_odd)
sum_even=0
for i in range(2,11,2):
sum_even=sum_even+i
sum_odd=0
for i in range(1,11,2):
sum_odd=sum_odd+i
print("Even sum",sum_even)
print("odd sum",sum_odd)
Even sum 30
odd sum 25
2+4+6+8+10
1+3+5+7+9
25
#PROGRAM 4
#input two numbers n1 and n2 and find sum of evenand odd nos from n1 to n2
print("\n Sum of even and odd numbers from 1 to 10",end="\t\t")
#complete the code
n1=int(input("Enter start\t"))
n2=int(input("Enter end vale\t"))
sum_even=0
sum_odd=0
for i in range(n1,n2+1):
if i%2==0:
sum_even=sum_even+ i
else:
sum_odd=sum_odd+i
print("Sum of even numbers",sum_even)
print("Sum of odd numbers",sum_odd)
#1.2 LOOP TO DO CALCULATIONS
#PROGRAM 1
print("To find the sum of numbers from 1 to 10",end="\t\t")
sum=0
for i in range(1,11):
sum=sum+i
print("sum of nos from 1 to 10",sum, end="-")
print()
#PROGRAM 2
print("\n Sum of even numbers from 2 to 10",end="\t\t")
#complete the code
sum=0
for i in range(2,11,2):
sum=sum+i
# write your code here
print("Sum of even nos from 2 to 10", sum, end="-")
#PROGRAM 3
print("\nTo find product of numbers from 1 to 10",end="\t\t")
product=1
for i in range(1,11,2):
product=product*i
# write your code here
print("Product from 1 to 10", product, end="-")
#PROGRAM 4
print("\n Sum of even and odd numbers from 1 to 10",end="\t\t")
#complete the code
sum_even=0
sum_odd=0
for i in range(1,11):
if i%2==0:
sum_even=sum_even+i
else:
sum_odd=sum_odd+i
print("Sum of even numbers",sum_even)
print("Sum of odd numbers",sum_odd)
# 2) sequence = list of numbers
test_list = [2,4,6,8,10] # Sample list
print(test_list)
for i in test_list: # Here, the sequence is of test_list hence the elements o
print(i)
[2, 4, 6, 8, 10]
10
# 3) sequence = list with multiple datatypes
test_list = ["one",2,'*',True]
for i in test_list: # Irrespective of the type of elemets inside the list, they can
print(i, end= " ")
one 2 * True
Now that we know different types of sequences, lets try some examples!
# Code to add 2 to each element of a list.
test_list = [1,4,8,16,-1]
print("Original List", test_list)
for num in range(len(test_list) ): # len(list_name) gives us the length of the list.
test_list[num] = test_list[num] + 2
print("New list", test_list)
# ON THE BASIS OF INDEX
# Code to add 2 to each element of a list.
test_list = [1,4,8,16,-1]
print("Original List", test_list)
for i in range(len(test_list) ): # len(list_name) gives us the length of the list.
if i%2==0:
test_list[i] = test_list[i] + 2
print("New list", test_list)
In the example above, length of test_list acts as range for the loop. Each element of test_list is
then called using the loop and 2 is added to them. The result overwrites the elements of the
original list.
Code Problem: 15 students are asked to enter their weight. Find the lightest of all.
#list with 15 students -> enter the value in list-> append()
#loop run 0 to 14
#min=list[0]
#inside loop compare
#How to add elements in the list
lst=[]
for i in range(15):
val=int(input("Enter weight"))
lst.append(val)
print(lst)
#how to store the first element of list into a variable min
min_weight=min(lst)
print("Lightest weight",min_weight)
Enter weight45
Enter weight34
Enter weight23
Enter weight1
Enter weight45
Lightest weight 1
a = [] # Empty List
for i in range(15): # 15 students need to enter their weight. Therefore, the loop runs f
a.append( int( input("Enter weight of student " + str(i + 1) + ":") ) ) # Weights en
print(a) # Print the entered values
minimum = a[0] # Initiate a variable min with the first entry
for i in range(1,15): # The loop again iterates for 15 times to check which element is
if a[i] < minimum: # If the element is less than the min value,
minimum = a[i] # min value is updated with the new smaller value.
print("Lightest weight, minimum)
[12, 13, 45, 7, 89, 90, 6, 67, 45, 34, 88, 67, 49, 90, 100]
print("To find the sum of numbers from 1 to 10",end="\t\t")
sum=0
for i in range(1,11):
sum=sum+i
print("sum of nos from 1 to 10",sum)
# Code to find sum of 10 numbers in a list
test_list = [1,8,29,-54,-77,29,15,38,49,56] # Sample List
sum1 = 0 # Variable to store the sum of list element
for i in range(0,len(test_list)):
sum1=sum1 + test_list[i]
print("Sum of list elements", sum1)
# Code to find sum of 10 numbers in a list
test_list = [1,8,29,-54,-77,29,15,38,49,-38] # Sample List
sum = 0 # Variable to store the sum of list elements
for num in test_list: # Each element of the list is called and the loop iterates for len
sum = sum + num # The element called is added to the sum variable. Like this, all
print("sum of the list = ",sum)
Task : Complete the code given below. Fill in the blanks to find the average
percentage of a class with 15 students whose marks are given in the list
marks.
marks = [40,54,81,95,76,69,35,63,99,58,73,85,56,62,93]
sum = ____ # Variable Intitiation
average = _____ # Variable Intitiation
for ____ in ______: # Loop statement to run through 15 student's marks
__________ # Statements to calculate average % of a class of 15 students
__________
__________
average = ______ # Calculate average percentage of the class
print(average) # Print average percentage
'''
Write a program to input a number and find the factorial of that number.
For example, if number is 5 then factorial will be 120.
Factorial = 5x4x3x2x1
[Hint:- Reverse the loop, find the product of number]
'''
# write your code here
factorial=1
num=int(input("Enter number\t"))
for i in range(num,0,-1):
factorial=factorial*i
print("Factorial of Number is\t", factorial)
Enter number 5
'''
Write a program to input a number and
generate the table of that number upto 10 in the following format.
5x1=5
5x2=10 and so on upto
5x10=50
'''
num=int(input("Enter number\t"))
for i in range(1,11):
prod=num*i
print(num, "*",i, "=", prod)
Enter number 29
29 * 1 = 29
29 * 2 = 58
29 * 3 = 87
29 * 4 = 116
29 * 5 = 145
29 * 6 = 174
29 * 7 = 203
29 * 8 = 232
29 * 9 = 261
29 * 10 = 290
Enter limit 38
output
-5 10 -15 20 -25 30 -35
'''
Write a program to generate the following sequence
-5, 10, -15, 20, -25, upto n
'''
# Write your code here
n=int(input("Enter limit\t"))
sign=-1
for i in range(5, n,5):
term=i*sign
sign=sign*-1
print(term, end= " ")
Enter a number 38
Input
Enter limit 5
Output
1+ 1/8 + 1/27+ 1/64+ 1/125=1.185662037
'''
Write a program to find the sum of
1+ 1/8 + 1/27 .... 1/n3
where n is the number input by the user (its n power 3)
'''
#Write your code here
n=int(input("Enter Limit\t"))
sum1=0
print("1",end="")
sum1+=1
for i in range(2, (n+1)):
cube=i**3
term=1/cube
sum1=sum1+term
print("+ 1/",cube,end= " ")
print("=", sum1)
Enter Limit 5
Nested Loops
Many times, the data which we work upon might not have just one dimension. It can be multi-
dimensional. Hence, we use the concept of Nested Loops in which one or multiple loops come
under the initial loop.
# This is formatted as code
for i in range(start,end+1): #outer loop
for j in range(start, end+1): #inner loop
print("*")
print()
# 1) sample code of nested for loops
for i in range(5): # Initial loop running from 0 to 4 OUTER LOOP
print("start of i = ",i)
for j in range(5): # This is a nested loop as tand comes under the other loop
print ("j = ",j) # This print statement is under the nested loop
print("end of i = ",i)
print("end of nested for loops")
start of i = 0
j = 0
j = 1
j = 2
j = 3
j = 4
end of i = 0
start of i = 1
j = 0
j = 1
j = 2
j = 3
j = 4
end of i = 1
start of i = 2
j = 0
j = 1
j = 2
j = 3
j = 4
end of i = 2
start of i = 3
j = 0
j = 1
j = 2
j = 3
j = 4
end of i = 3
start of i = 4
j = 0
j = 1
j = 2
j = 3
j = 4
end of i = 4
In the above code, for each iteration of for i block, a whole for j block is ran. this can be further
nested to as many nests as we want. Note that indentation plays a very important role in this.
Changing the indentation will result in unexpected
answers or might throw an error.
# 2) Sample way for nested loop
for i in range (1,4): #OUTER LOOP
for j in range(1,5): #INNER LOOP
print(i, ".", j, sep="", end="\t")
print()
#program to print * pattern
for i in range(1,4):
for j in range(1,i+1):
print("*",end="")
print()
**
***
if i =1 then j=1
CONCLUSION
j<=i
#program to print * pattern
for i in range(1,4):
for j in range(1,4):
if j<=i:
print("*",end="")
print()
**
***
#program to print * pattern
for i in range(1,4):
for j in range(1,4):
if j<=i:
print("*",end="")
else:
print("$", end="")
print()
*$$
**$
***
#program to print * pattern
for i in range(1,4):
for j in range(1,4):
if j>=i:
print("*",end="")
else:
print(" ", end="")
print()
***
**
#Code to print the following pattern:
"""
1 2
1 2 3
1 2 3 4
1 2 3 4 5
"""
# A matrix is formed using 2 dimensions. To work in 2 dimensions, we need two loops wor
# Outer loop handles the number of rows
for i in range(1,6): # Loop runs from 1 to 5
# Inner loop to handle number of columns
for j in range(1, i + 1): # Loop runs from 1 to i+1
print(j, end = " ") # Print statement executes inside the ne
print() # Once the inner loop gets executed, cod
1 2
1 2 3
1 2 3 4
1 2 3 4 5
TRY YOURSELF
2 2
3 3 3
4 4 4 4
5 5 5 5 5
i=1 j=1
i=2 j=1,2
i=3 j=1,2,3
CONCLUSION j<=i
# Print the following pattern
"""
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
"""
# In this code, two nested loops will be used: one for the increasing triangle and othe
for i in range(1,6):
for j in range(1, i + 1):
print("*", end = " ")
print()
for i in range(1,5):
i = 5 - i
for j in range(1, i + 1):
print("*",end = " ")
print()
Challenge:
Your school has found that there was an error in AI question paper. To compensate for this
mistake, the school wants to add 5 grace marks to each students' marks. But since it is a time
consuming process, the school has come to you to take help of your programming knowledge.
They have given you a 2-D list with marks of 10 students per class from 5 classes in a list
named marks and asked you to help them for tow challenges:
Grading system:
Below 25 - F
25 to 40 - E
41 to 60 - D
61 to 75 - C
76 to 90 - B
Above 90 - A
marks = [[87, 91, 48, 53, 65, 79, 57, 85, 94, 82], # marks of class A
[61, 73, 37, 85, 45, 85, 92, 18, 18, 89], # marks of class B
[87, 94, 35, 65, 84, 64, 25, 61, 46, 28], # marks of class C
[56, 19, 55, 77, 52, 84, 47, 32, 88, 68], # marks of class D
[75, 95, 58, 52, 65, 77, 65, 62, 91, 45]] # marks of class E
# Write your code here to complete the challenges given
Note: While using for loop works fine to perform the task, it is a long process, which can be
replaced by a very small code while working with NumPy Arrays, which will be covered in the
next chapter.
While loops
[ ] ↳ 7 cells hidden
Grand Challenge
Your society is installing a security system at the society's entrance. As you have started
working with python, you decide to help them in creating passwords for everyone with the help
of a python script.
The society's secretary is impressed by you and is ready to help you with all
the information you need.
Therefore, your challenge now is to create unique passwords for 50
people according to their names, Age and House number which has been provided by the
secretary.
Example:
Name=Akshay Chawla
Age=24
House no=701
# defining the list called details containing the desired information
details=[["Nitika Balasubramanian","Anjana Pandya","Iqbal Modi","Ajeet Hari","Anil Lalla
"Manpreet Murty","Anusha Shanker","Sapna Prasad","Jatin Nagy","Aishwarya Thaker",
"Charandeep Kothari","Kabeer Konda","Habib Saraf","Ankita Baria","Nitika Menon",
"Anil Naruka","Deep Barad","Sumit Anthony","Feroz Koshy","Radhe Gulati",
"Umar Saran","Hina Murthy","Wafa Amble","Neerendra Saini","Jiya Warrior",
"Aadil Kapoor","Ghanshyam Gill","Prabhat Tank","Akanksha Vyas",
"Sheetal Chand","Sirish Parikh","Sukriti Srivastava","Nandini Chowdhury",
"Uday Bath","Kabeer Raj","Riddhi Dube","Aarushi Varma","Naseer Mathai","Urmi Jha",
"Urmila Tiwari","Chirag Rajagopalan","Naseer Badal","Kushal Dalal","Aayushi Bath",
"Gunjan Raju","Urmi Mangat","Trishana Pardeshi","Rajesh Shukla","Usha Das","Shivam Saxen
[32,39,48,37,58,46,27,19,54,55,45,47,33,38,58,33,61,26,63,39,56,36,49,64,36,
18,56,23,57,22,38,42,40,51,47,18,50,24,32,46,37,38,48,52,27,63,33,23,26,39],
[101,102,103,104,105,106,201,202,203,204,205,206,301,302,303,304,305,306,
401,402,403,404,405,406,501,502,503,504,505,506,601,602,603,604,605,606,
701,702,703,704,705,706,801,802,803,804,805,806,901,902]]