python what is the syntax for loops
# For Loops — In Python, for loops are used for iterating over a
# sequence (a list, a tuple, a dictionary, a set, or a string)
# Basic syntax:
for variable in sequence_youre_iterating_over:
something you want to do each iteration
something else you want to do each iteration
# Note, you don't need to define an indexing variable in Python
# Note, "variable" takes the value of the current item in the
# sequence_youre_iterating_over. If you want it to take on numbers
# (E.g. 0,1,2,3,4...) use: for variable in range(end_number):
# Note, the stuff you do during each iteration is indicated by indenting
# it (tab or 4 spaces) - you don't need to surround it in braces { }
# Note, use break to exit the loop entirely if some condition is true and
# use continue to skip to the next iteration if some condition is true
# Example usage:
my_list = ["some", "crazy", "text"]
for words in my_list:
print(words)
--> some
--> crazy
--> text
# Example usage 2:
for i in range(1, 6, 2): # Range takes arguments (start, stop, step)
print(i)
--> 1
--> 3
--> 5
# Example usage 3:
for i in range(7):
if i == 2:
i += 1 # += is shorthand for: i = i + 1
continue # If i = 2, continue skips to the next iteration of the loop
if i == 4:
break # If i = 4, break exits the loop entirely
print(i)
i += 1
--> 0
--> 1
--> 3
# While Loops
# Basic syntax:
while condition_to_check:
something you want to do as long as condition_to_check is true
something else you want to do as long as condition_to_check is true
# Note, make sure that the condition_to_check will return false at some
# point, otherwise you'll be stuck in an infinite loop
# Note, for more complicated conditions_to_check, you may need to use
# parentheses to clarify the argument. E.g.:
# while (i < 10 and (j >= 5 or k == "some_text")):
# Note, indentation, break, and continue work the same as for for loops
# Example usage:
i = 0
while i < 3: # True as long as i is less than 3
print(i)
i += 1 # += is shorthand for: i = i + 1
--> 0 # Python is 0-indexed, so zero is printed first
--> 1
--> 2
# Example usage 2:
i = 0
while i < 3:
print(i)
i += 1
else:
print("Something to do once, immediately after the loop terminates")
--> 0
--> 1
--> 2
--> Something to do once, immediately after the loop terminates
# Quality 101 with for loops. It's basically a for each loop.
# I know how frustrating they can be at first so just work with them as mutch as possible
# I use different variables to show that you can use different outputs methods
def forloops():
list_ = ["a", "b", "c", "d", "e"]
# Normal for loops
output_list = []
for i in list_:
output_list.append(i)
print(f"Normal list output: {output_list}")
# Enumerate [extracting tuples]
output_list = []
for i in enumerate(list_):
output_list.append(i)
print(f"Enumerate output: {output_list}")
# Enumerate [extracting seperate values]
output_list = {}
for x, y in enumerate(list_):
output_list[x] = y
print(f"Enumerate output: {output_list}")
# Range [Can be used in multiple ways]
# range(<from>, until, <skips>)
output_list = []
for i in range(5):
output_list.append(i)
print(f"Range output: {output_list}")
output_list = []
for i in range(3, 10, 3):
output_list.append(i)
print(f"Range output: {output_list}")
forloops()