COURSES
MBAData Science & AnalyticsDoctorate Software & Tech AI | ML MarketingManagement
Professional Certificate Programme in HR Management and AnalyticsPost Graduate Certificate in Product ManagementExecutive Post Graduate Program in Healthcare ManagementExecutive PG Programme in Human Resource ManagementMBA in International Finance (integrated with ACCA, UK)Global Master Certificate in Integrated Supply Chain ManagementAdvanced General Management ProgramManagement EssentialsLeadership and Management in New Age BusinessProduct Management Online Certificate ProgramStrategic Human Resources Leadership Cornell Certificate ProgramHuman Resources Management Certificate Program for Indian ExecutivesGlobal Professional Certificate in Effective Leadership and ManagementCSM® Certification TrainingCSPO® Certification TrainingLeading SAFe® 5.1 Training (SAFe® Agilist Certification)SAFe® 5.1 POPM CertificationSAFe® 5.1 Scrum Master Certification (SSM)Implementing SAFe® 5.1 with SPC CertificationSAFe® 5 Release Train Engineer (RTE) CertificationPMP® Certification TrainingPRINCE2® Foundation and Practitioner Certification
Law
Job Linked
Bootcamps
Study Abroad
Master of Business Administration (90 ECTS)Master in International Management (120 ECTS)Bachelor of Business Administration (180 ECTS)B.Sc. Computer Science (180 ECTS)MS in Data AnalyticsMS in Project ManagementMS in Information TechnologyMasters Degree in Data Analytics and VisualizationMasters Degree in Artificial IntelligenceMBS in Entrepreneurship and MarketingMSc in Data AnalyticsMS in Data AnalyticsMS in Computer ScienceMaster of Science in Business AnalyticsMaster of Business Administration MS in Data ScienceMS in Information TechnologyMaster of Business AdministrationMS in Applied Data ScienceMaster of Business Administration | STEMMS in Data AnalyticsM.Sc. Data Science (60 ECTS)Master of Business AdministrationMS in Information Technology and Administrative Management MS in Computer Science Master of Business Administration MBA General Management-90 ECTSMSc International Business ManagementMS Data Science Master of Business Administration MSc Business Intelligence and Data ScienceMS Data Analytics MS in Management Information SystemsMSc International Business and ManagementMS Engineering ManagementMS in Machine Learning EngineeringMS in Engineering ManagementMSc Data EngineeringMSc Artificial Intelligence EngineeringMPS in InformaticsMPS in Applied Machine IntelligenceMS in Project ManagementMPS in AnalyticsMS in Project ManagementMS in Organizational LeadershipMPS in Analytics - NEU CanadaMBA with specializationMPS in Informatics - NEU Canada Master in Business AdministrationMS in Digital Marketing and MediaMS in Project ManagementMSc Sustainable Tourism and Event ManagementMSc in Circular Economy and Sustainable InnovationMSc in Impact Finance and Fintech ManagementMS Computer ScienceMS in Applied StatisticsMaster in Computer Information SystemsMBA in Technology, Innovation and EntrepreneurshipMSc Data Science with Work PlacementMSc Global Business Management with Work Placement MBA with Work PlacementMS in Robotics and Autonomous SystemsMS in Civil EngineeringMS in Internet of ThingsMSc International Logistics and Supply Chain ManagementMBA- Business InformaticsMSc International ManagementMBA in Strategic Data Driven ManagementMSc Digital MarketingMBA Business and MarketingMaster of Business AdministrationMSc Digital MarketingMSc in Sustainable Luxury and Creative IndustriesMSc in Sustainable Global Supply Chain ManagementMSc in International Corporate FinanceMSc Digital Business Analytics MSc in International HospitalityMSc Luxury and Innovation ManagementMaster of Business Administration-International Business ManagementMS in Computer EngineeringMS in Industrial and Systems EngineeringMSc International Business ManagementMaster in ManagementMSc MarketingMSc Business ManagementMSc Global Supply Chain ManagementMS in Information Systems and Technology with Business Intelligence and Analytics ConcentrationMSc Corporate FinanceMSc Data Analytics for BusinessMaster of Business AdministrationBachelors in International ManagementMS Computer Science with Artificial Intelligence and Machine Learning ConcentrationMaster of Business AdministrationMaster of Business AdministrationMSc in International FinanceMSc in International Management and Global LeadershipMaster of Business AdministrationBachelor of Business
For College Students

Iterations in Python

$$/$$

In the previous session, you learnt about lists and various other data structures. Let’s look at a small example where you have a person’s income and expense data across five months in the form of a list, and you want to compute his savings across these five months. You may be thinking to do this manually by taking the first elements from the two lists and subtracting them, then again taking the second elements and subtracting, and so on. This may look simple, but let’s say it is across 10 or 20 years. Would you do the same? 

 

This is where the concept of iteration comes in handy, as you are repeating the same operation multiple times. With this in mind, let’s learn more about it.

$$/$$

By now, you have understood the syntax of a for loop. 

for val in seq :
    statements

 

A few things to note in the syntax include:

  1. seq, which represents a sequence; it can be a list, a tuple or a string. To put it in a simple way it can be any iterable object.

  2. The in operator, which, as you may recall, is a membership operator that helps to check for the presence of an element in a sequence. 

 

But what role is the in operator playing here?

When you say for val in seq, it means that for a value in seq, it executes the set of statements under for block once and returns to for again and shifts the position to the next value. Let’s say you are at the last element in the seq. Here, the for block executes, and now, when it again goes to for val in seq, there are no more elements, and hence, val in seq returns a false and stops the execution.

 

Now that you have learnt about the for-statement, can you answer our initial question of calculating savings using a person’s income and expenses data? Let’s try this out.

 

Assume you have L1 = [10,20,30,24,18] (in thousands)

L2 = [8,14,15,20,10]

 

What you were doing manually is subtracting the first element of each list and then the second element, and so on. In other words, it is L1[i] - L2[i]. 

 

Since you need these indexes, let’s create a dummy array of five elements that represent the index positions L3 = [0,1,2,3,4]. 

 

Let’s implement the for loop using the list L3:

L1 = [10,20,30,24,18]
L2 = [8,14,15,20,10]
L3 = [0,1,2,3,4]

for i in L3:
    L3[i] = L1[i] - L2[i]

 

Here, you are updating elements of L3 at each iteration. Now think whether you can use the same approach for a list with 1000 elements? To answer this, let's look at our next video:

$$/$$

The syntax of the range function is simple. Different implementations of range function include:

  • range(n): This creates a range object that has elements from 0 to n-1 [ both inclusive].

  • range(m,n): This creates a range object that has elements from m to n-1 [ both inclusive].

  • range(m,n,t): This creates a range object that has elements from m to n-1 with a step count of t. In other words, the range object has elements m, m+t, m+2t…so on. If t is negative, the elements would be decreasing, and that’s exactly what happens in the range (100,0,-1).

 

An important thing to note here is that you saw a way to create lists using the list function on the range object. list(range(0,10) would create a list of 10 elements.

$$/$$

Suppose a situation arises where the developer has no idea about the number of iterations he needs to have. Such iterations are called as Event-based iterations. Let's learn about them in detail.

$$/$$

In the video above, you learnt the concept of a while loop and its syntax. A while loop is used when you are not sure of the number of iterations that you need to have, and it should be used when your results depend on the value of a variable.

 

Let’s see how a while loop would be useful by taking our initial example defined at the start of the segment. 

L1 = [10,20,30,24,18]
L2 = [8,14,15,20,10]
i =0
while i<len(L1):
    print(L1[i]-L2[i], end=' ')
    i=i+1

 

In the given code, a while loop and the len() function are being used, and the loop executes until the condition i < len(L1) fails. This condition is important, as it ensures whether or not you will be able to access all the elements.