top of page
learn_data_science.jpg

Data Scientist Program

 

Free Online Data Science Training for Complete Beginners.
 


No prior coding knowledge required!

Loops in python

Writer's picture: Gehad HishamGehad Hisham

This is less like the for a keyword in other programming languages and works more like an iterator method as found in other object-orientated programming languages.

1- while loop: Repeats a statement or group of statements while a given condition is TRUE. It tests the condition before executing the loop body.

2- for loop: Executes a sequence of statements multiple times and abbreviates the code that manages the loop variable.

For loop


A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).

With the for loop, we can execute a set of statements, once for each item in a list, tuple, set etc.

Example:


fruits = ["apple", "banana", "cherry"]
for x in fruits:
  print(x)

Output:

apple
banana 
cherry

Looping Through a String:

Even strings are iterable objects, they contain a sequence of characters:

Example Loop through the letters in the word "Gehad":


for x in "Gehad":
  print(x)

Output:



G
e
h
a
d

The break Statement

With the break statement we can stop the loop before it has looped through all the items:


Example Exit the loop when x is "banana":


fruits = ["apple", "banana", "cherry"]
for x in fruits:
  print(x)
  if x == "banana":
    break

output:

apple
banana 

The continue Statement With the continue statement we can stop the current iteration of the loop, and continue with the next:

Example Do not print banana:


fruits = ["apple", "banana", "cherry"]
for x in fruits:
  if x == "banana":
    continue
  print(x)

Output:



apple 
cherry 

The range() Function:

To loop through a set of code a specified number of times, we can use the range() function, The range() function returns a sequence of numbers, starting from 0 by default, increments by 1 (by default), and ends at a specified number.

Example Using the range() function:


for x in range(6):
  print(x)

Output:

0 
1
2
3
4
5

Thank you..

0 comments

Recent Posts

See All

Comments


COURSES, PROGRAMS & CERTIFICATIONS

 

Advanced Business Analytics Specialization

Applied Data Science with Python (University of Michigan)

Data Analyst Professional Certificate (IBM)

Data Science Professional Certificate (IBM)

Data Science Specialization (John Hopkins University)

Data Science with Python Certification Training 

Data Scientist Career Path

Data Scientist Nano Degree Program

Data Scientist Program

Deep Learning Specialization

Machine Learning Course (Andrew Ng @ Stanford)

Machine Learning, Data Science and Deep Learning

Machine Learning Specialization (University of Washington)

Master Python for Data Science

Mathematics for Machine Learning (Imperial College London)

Programming with Python

Python for Everybody Specialization (University of Michigan)

Python Machine Learning Certification Training

Reinforcement Learning Specialization (University of Alberta)

Join our mailing list

Data Insight participates in affiliate programs and may sometimes get a commission through purchases made through our links without any additional cost to our visitors.

bottom of page