Skip to content

[Python Part 4] Data Science | Studi Independen | MyEduSolve X Kampus Merdeka

Notifications You must be signed in to change notification settings

nurulauliyas/Python-Part-4_Looping

Repository files navigation

Python-Part-4_Looping

For

  • The for loop in Python is used to iterate over a sequence (list, tuple, string) or other iterable objects. Iterating over a sequence is called traversal.
  • Here, val is the variable that takes the value of the item inside the sequence on each iteration.
  • Loop continues until we reach the last item in the sequence. The body of for loop is separated from the rest of the code using indentation.

Python Nested For Loop

A nested loop is a loop inside the body of the outer loop. The inner or outer loop can be any type, such as a while loop or for loop. For example, the outer for loop can contain a while loop and vice versa.

the outer loop can contain more than one inner loop. There is no limitation on the chaining of loops.

In the nested loop, the number of iterations will be equal to the number of iterations in the outer loop multiplied by the iterations in the inner loop.

While

  • The while loop in Python is used to iterate over a block of code as long as the test expression (condition) is true.
  • We generally use this loop when we don't know the number of times to iterate beforehand.

While loop with else

  • Same as with for loops, while loops can also have an optional else block.
  • The else part is executed if the condition in the while loop evaluates to False.

Break

  • The break statement terminates the loop containing it. Control of the program flows to the statement immediately after the body of the loop.
  • If the break statement is inside a nested loop (loop inside another loop), the break statement will terminate the innermost loop.

Continue

  • The continue statement is used to skip the rest of the code inside a loop for the current iteration only. Loop does not terminate but continues on with the next iteration.

Pass

  • The pass statement in Python is used when a statement is required syntactically but you do not want any command or code to execute.
  • The pass statement is a null operation; nothing happens when it executes. The pass is also useful in places where your code will eventually go, but has not been written yet.

List Comprehension

  • List comprehension offers a shorter syntax when you want to create a new list based on the values of an existing list.

About

[Python Part 4] Data Science | Studi Independen | MyEduSolve X Kampus Merdeka

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages