[vc_row css_animation=”” row_type=”row” use_row_as_full_screen_section=”no” type=”full_width” angled_section=”no” text_align=”left” background_image_as_pattern=”without_pattern”][vc_column][vc_column_text]

Loops in Python

[/vc_column_text][vc_separator type=”normal” up=”1″][vc_column_text css=”.vc_custom_1570788968525{background-color: #f6f6f6 !important;}”]

Loops in Python are a way to repeat an operation any number of times. There are different kinds of loops that are used depending on whether you want an operation to be performed a specific number of times, or if you want the operation to be repeated until a specific condition is met.

[/vc_column_text][vc_empty_space height=”20px”][vc_column_text]When we want to repeat an operation or sequence, we use a loop. With the help of the loop, we can write a program sequence (e.g., a calculation) to be performed while either a particular condition is fulfilled (conditional loop), or a loop that repeats the program sequence a predetermined number of times (counting loop).

 

The three most common structures for repetition within Python that we will, of course, look at more closely are:

  • While loop
  • For loop

[/vc_column_text][vc_empty_space height=”10px”][vc_column_text]

[/vc_column_text][vc_empty_space height=”10px”][vc_separator type=”normal” thickness=”2″][vc_empty_space height=”10px”][vc_column_text]

HOW DOES A LOOP IN PYTHON WORK?

[/vc_column_text][vc_row_inner row_type=”row” type=”full_width” text_align=”left” css_animation=””][vc_column_inner width=”1/2″][vc_empty_space height=”30px”][vc_column_text]We can illustrate a loop using the flow chart on the right that shows in a simple way how loops work.

 

1. The program sequence begins at Start and then checks if the condition is met

 

2. If the condition is met ( Yes ) then the operation is performed, if the condition is not met ( No ) then the program moves on

 

3. The loop then repeats the process and returns back to check if the condition is met[/vc_column_text][/vc_column_inner][vc_column_inner width=”1/2″][vc_single_image image=”19454″ img_size=”large” alignment=”center” onclick=”link_image” qode_css_animation=””][/vc_column_inner][/vc_row_inner][vc_empty_space height=”30px”][vc_column_text]Note that we can end up with so-called “infinity loops“, that is, loops that do not proceed in the program (terminated / finished). The condition in the loop is simply never met, and since the condition must be fulfilled for the loop to complete, it runs on. Fortunately, the compiler shuts down the program and gives you an error message, so there’s nothing you need to worry about. It is also good to know that it is fairly simple (and common) error to do.[/vc_column_text][vc_empty_space height=”30px”][vc_separator type=”normal”][vc_empty_space height=”15px”][/vc_column][/vc_row][vc_row css_animation=”” row_type=”row” use_row_as_full_screen_section=”no” type=”full_width” angled_section=”no” text_align=”left” background_image_as_pattern=”without_pattern”][vc_column][vc_column_text]

Previous Page    |    Next Page

[/vc_column_text][vc_empty_space][/vc_column][/vc_row]